001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.attachment;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022:
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.ofbiz.base.util.UtilMisc;
027: import org.ofbiz.entity.GenericDelegator;
028: import org.ofbiz.entity.GenericEntityException;
029: import org.ofbiz.entity.GenericValue;
030:
031: /**
032: * DOCUMENT ME!
033: *
034: */
035: public class AttachmentHelper {
036: public AttachmentHelper() {
037: }
038:
039: /**
040: * Downloads a file to the client.
041: *
042: * @param response the client connection that will be used for download
043: *
044: * @param fileId the rowId of the file stored in the FileAttachment table
045: *
046: * @exception IOException if an input or output exception has occurred
047: */
048: public static void downloadFile(HttpServletResponse response,
049: GenericDelegator delegator, String fileId)
050: throws IOException, ServletException,
051: GenericEntityException {
052: GenericValue fileGV = delegator.findByPrimaryKey(
053: "FileAttachment", UtilMisc.toMap("fileId", fileId));
054: String filePath = fileGV.getString("fileLocation");
055: String destName = fileGV.getString("fileName");
056:
057: downloadFile(response, filePath, destName);
058: }
059:
060: /**
061: * Downloads a file to the client.
062: *
063: * @param response the client connection that will be used for download
064: *
065: * @param sourceFile the full abosulute path to the file to be downloaded
066: *
067: * @param destFileName the default destination name of the file
068: *
069: * @exception IOException if an input or output exception has occurred
070: */
071: public static void downloadFile(HttpServletResponse response,
072: String sourceFile, String destFileName) throws IOException,
073: ServletException {
074: downloadFile(response, sourceFile, null, destFileName, 2048);
075: }
076:
077: /**
078: * Downloads a file to the client.
079: *
080: * @param response the client connection that will be used for download
081: *
082: * @param sourceFile the full abosulute path to the file to be downloaded
083: *
084: * @param contentType the type of document to send to the client
085: *
086: * @param destFileName the default destination name of the file
087: *
088: * @param blockSize the chunksize used to send the file down
089: *
090: * @exception IOException if an input or output exception has occurred
091: */
092: public static void downloadFile(HttpServletResponse response,
093: String sourceFile, String contentType, String destFileName,
094: int blockSize) throws IOException, ServletException {
095: if (sourceFile == null) {
096: throw new IllegalArgumentException(String
097: .valueOf((new StringBuffer("File '")).append(
098: sourceFile).append("' not found.")));
099: }
100:
101: if (sourceFile.equals("")) {
102: throw new IllegalArgumentException(String
103: .valueOf((new StringBuffer("File '")).append(
104: sourceFile).append("' not found.")));
105: }
106:
107: // if(!isVirtual(sourceFilePathName) && m_denyPhysicalPath)
108: // throw new SecurityException("Physical path is denied (1035).");
109: // if(isVirtual(sourceFilePathName))
110: // sourceFilePathName = m_application.getRealPath(sourceFilePathName);
111: File file = new File(sourceFile);
112: FileInputStream fileIn = new FileInputStream(file);
113: long fileLen = file.length();
114: int readBytes = 0;
115: int totalRead = 0;
116: byte[] b = new byte[blockSize];
117:
118: if ((contentType == null) || (contentType.length() == 0)) {
119: response.setContentType("application/x-msdownload");
120: } else {
121: response.setContentType(contentType);
122: }
123:
124: response.setContentLength((int) fileLen);
125:
126: if (destFileName == null) {
127: response.setHeader("Content-Disposition",
128: "attachment; filename=".concat(String.valueOf(file
129: .getName())));
130: } else if (destFileName.length() == 0) {
131: response.setHeader("Content-Disposition", "attachment;");
132: } else {
133: response.setHeader("Content-Disposition",
134: "attachment; filename=".concat(String
135: .valueOf(destFileName)));
136: }
137:
138: while ((long) totalRead < fileLen) {
139: readBytes = fileIn.read(b, 0, blockSize);
140: totalRead += readBytes;
141: response.getOutputStream().write(b, 0, readBytes);
142: }
143:
144: fileIn.close();
145: }
146: }
|