01: package org.enhydra.snapper.presentation;
02:
03: import java.io.BufferedOutputStream;
04: import java.io.File;
05: import java.io.FileInputStream;
06: import java.io.FileNotFoundException;
07:
08: import org.enhydra.snapper.Log;
09:
10: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
11: import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
12:
13: public class FileDownloadBO {
14:
15: private File fDown = null;
16: private HttpPresentationComms comms = null;
17:
18: public static final String EXCEL_CONTENT_TYPE = "application/vnd.ms-excel";
19: public static final String WORD_CONTENT_TYPE = "application/vnd.ms-word";
20: public static final String HTML_CONTENT_TYPE = "text/html";
21: public static final String PDF_CONTENT_TYPE = "application/pdf";
22: public static final String ZIP_CONTENT_TYPE = "application/x-zip";
23: public static final String RTF_CONTENT_TYPE = "application/rtf";
24: public static final String PPT_CONTENT_TYPE = "application/vnd.ms-powerpoint";
25: public static final String PPS_CONTENT_TYPE = "application/vnd.ms-powerpoint";
26: public static final String EML_CONTENT_TYPE = "application/vnd.ms-outlook";
27:
28: public static final String SXW_CONTENT_TYPE = "application/vnd.sun.xml.writer";
29: public static final String STW_CONTENT_TYPE = "application/vnd.sun.xml.writer.template";
30: public static final String SXG_CONTENT_TYPE = "application/vnd.sun.xml.writer.global";
31: public static final String SXC_CONTENT_TYPE = "application/vnd.sun.xml.calc";
32: public static final String STC_CONTENT_TYPE = "application/vnd.sun.xml.calc.template";
33: public static final String SXI_CONTENT_TYPE = "application/vnd.sun.xml.impress";
34: public static final String STI_CONTENT_TYPE = "application/vnd.sun.xml.impress.template";
35: public static final String SXD_CONTENT_TYPE = "application/vnd.sun.xml.draw";
36: public static final String STD_CONTENT_TYPE = "application/vnd.sun.xml.draw.template";
37: public static final String SXM_CONTENT_TYPE = "application/vnd.sun.xml.math";
38:
39: public FileDownloadBO(File file, HttpPresentationComms comms) {
40: this .fDown = file;
41: this .comms = comms;
42: }
43:
44: public void setContentType(String type)
45: throws HttpPresentationException {
46: this .comms.response.setContentType(type);
47: this .comms.response.setHeader("Content-Disposition",
48: "inline; filename=\"" + fDown.getName() + "\"");
49: }
50:
51: public void download() {
52:
53: FileInputStream fi = null;
54: BufferedOutputStream bu = null;
55: try {
56: fi = new FileInputStream(fDown);
57: byte[] buffer = new byte[4096];
58: bu = new BufferedOutputStream(comms.response
59: .getOutputStream());
60: int no = fi.read(buffer);
61: while (no > -1) {
62: bu.write(buffer, 0, no);
63: no = fi.read(buffer);
64: }
65: bu.flush();
66: comms.response.flush();
67:
68: } catch (FileNotFoundException e) {
69: Log.log(e.getMessage());
70: } catch (Exception e) {
71: } finally {
72: if (fi != null) {
73: try {
74: fi.close();
75: } catch (Exception e) {
76: }
77: fi = null;
78: }
79: if (bu != null) {
80: try {
81: bu.close();
82: } catch (Exception e) {
83: }
84: bu = null;
85: }
86: }
87: }
88:
89: }
|