KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > presentation > FileDownloadBO


1
2 package org.enhydra.snapper.presentation;
3
4 import java.io.BufferedOutputStream JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8
9 import org.enhydra.snapper.Log;
10
11
12
13
14 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
15 import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
16
17
18 public class FileDownloadBO{
19
20   private File JavaDoc fDown = null;
21   private HttpPresentationComms comms = null;
22   private boolean isForDelete = false;
23
24   public static final String JavaDoc EXCEL_CONTENT_TYPE = "application/vnd.ms-excel";
25   public static final String JavaDoc WORD_CONTENT_TYPE = "application/vnd.ms-word";
26   public static final String JavaDoc HTML_CONTENT_TYPE = "text/html";
27   public static final String JavaDoc PDF_CONTENT_TYPE = "application/pdf";
28   public static final String JavaDoc ZIP_CONTENT_TYPE = "application/x-zip";
29   public static final String JavaDoc RTF_CONTENT_TYPE = "application/rtf";
30   public static final String JavaDoc PPT_CONTENT_TYPE = "application/vnd.ms-powerpoint";
31   public static final String JavaDoc PPS_CONTENT_TYPE = "application/vnd.ms-powerpoint";
32   public static final String JavaDoc EML_CONTENT_TYPE = "application/vnd.ms-outlook";
33   
34   public static final String JavaDoc SXW_CONTENT_TYPE = "application/vnd.sun.xml.writer";
35   public static final String JavaDoc STW_CONTENT_TYPE = "application/vnd.sun.xml.writer.template";
36   public static final String JavaDoc SXG_CONTENT_TYPE = "application/vnd.sun.xml.writer.global";
37   public static final String JavaDoc SXC_CONTENT_TYPE = "application/vnd.sun.xml.calc";
38   public static final String JavaDoc STC_CONTENT_TYPE = "application/vnd.sun.xml.calc.template";
39   public static final String JavaDoc SXI_CONTENT_TYPE = "application/vnd.sun.xml.impress";
40   public static final String JavaDoc STI_CONTENT_TYPE = "application/vnd.sun.xml.impress.template";
41   public static final String JavaDoc SXD_CONTENT_TYPE = "application/vnd.sun.xml.draw";
42   public static final String JavaDoc STD_CONTENT_TYPE = "application/vnd.sun.xml.draw.template";
43   public static final String JavaDoc SXM_CONTENT_TYPE = "application/vnd.sun.xml.math";
44   
45   /*
46    application/vnd.sun.xml.writer sxw
47    application/vnd.sun.xml.writer.template stw
48    application/vnd.sun.xml.writer.global sxg
49    application/vnd.sun.xml.calc sxc
50    application/vnd.sun.xml.calc.template stc
51    application/vnd.sun.xml.impress sxi
52    application/vnd.sun.xml.impress.template sti
53    application/vnd.sun.xml.draw sxd
54    application/vnd.sun.xml.draw.template std
55    application/vnd.sun.xml.math sxm
56   */

57   
58   public FileDownloadBO(File JavaDoc file, HttpPresentationComms comms) {
59     this.fDown = file;
60     this.comms = comms;
61   }
62
63   public void setContentType(String JavaDoc type) throws HttpPresentationException {
64       this.comms.response.setContentType(type);
65       this.comms.response.setHeader("Content-Disposition",
66       "inline; filename=\"" + fDown.getName() + "\"");
67   }
68
69   public void download() {
70     try {
71       FileInputStream JavaDoc fi = new FileInputStream JavaDoc(fDown);
72
73       byte[] buffer = new byte[4096];
74       BufferedOutputStream JavaDoc bu = new BufferedOutputStream JavaDoc(comms.response.getOutputStream());
75       int no = fi.read(buffer);
76       while (no > -1) {
77         bu.write(buffer, 0, no);
78         no = fi.read(buffer);
79       }
80       bu.flush();
81       comms.response.flush();
82       fi.close();
83       fi = null;
84     }catch (FileNotFoundException JavaDoc e) {
85         Log.log(e.getMessage());
86  
87     }catch (Exception JavaDoc e) {
88     }
89   }
90  
91 }
92
Popular Tags