⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attachmenthelper.java

📁 国外的一套开源CRM
💻 JAVA
字号:
/*
 * 
 * Copyright (c) 2004 SourceTap - www.sourcetap.com
 *
 *  The contents of this file are subject to the SourceTap Public License 
 * ("License"); You may not use this file except in compliance with the 
 * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 */

package com.sourcetap.sfa.attachment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;


/**
 * DOCUMENT ME!
 *
 */
public class AttachmentHelper {
    public AttachmentHelper() {
    }

    /**
 * Downloads a file to the client.
         *
         * @param response                the client connection that will be used for download
         *
* @param fileId                the rowId of the file stored in the FileAttachment table
         *
* @exception IOException        if an input or output exception has occurred
         */
    public static void downloadFile(HttpServletResponse response,
        GenericDelegator delegator, String fileId)
        throws IOException, ServletException, GenericEntityException {
        GenericValue fileGV = delegator.findByPrimaryKey("FileAttachment",
                UtilMisc.toMap("fileId", fileId));
        String filePath = fileGV.getString("fileLocation");
        String destName = fileGV.getString("fileName");

        downloadFile(response, filePath, destName);
    }

    /**
         * Downloads a file to the client.
    *
    * @param response                the client connection that will be used for download
    *
     * @param sourceFile                the full abosulute path to the file to be downloaded
    *
    * @param destFileName                the default destination name of the file
    *
     * @exception IOException        if an input or output exception has occurred
    */
    public static void downloadFile(HttpServletResponse response,
        String sourceFile, String destFileName)
        throws IOException, ServletException {
        downloadFile(response, sourceFile, null, destFileName, 2048);
    }

    /**
         * Downloads a file to the client.
    *
    * @param response                the client connection that will be used for download
    *
     * @param sourceFile                the full abosulute path to the file to be downloaded
    *
    * @param contentType                 the type of document to send to the client
    *
    * @param destFileName                the default destination name of the file
    *
     * @param blockSize                the chunksize used to send the file down
    *
    * @exception IOException        if an input or output exception has occurred
    */
    public static void downloadFile(HttpServletResponse response,
        String sourceFile, String contentType, String destFileName,
        int blockSize) throws IOException, ServletException {
        if (sourceFile == null) {
            throw new IllegalArgumentException(String.valueOf((new StringBuffer(
                        "File '")).append(sourceFile).append("' not found.")));
        }

        if (sourceFile.equals("")) {
            throw new IllegalArgumentException(String.valueOf((new StringBuffer(
                        "File '")).append(sourceFile).append("' not found.")));
        }

        //        if(!isVirtual(sourceFilePathName) && m_denyPhysicalPath)
        //            throw new SecurityException("Physical path is denied (1035).");
        //        if(isVirtual(sourceFilePathName))
        //            sourceFilePathName = m_application.getRealPath(sourceFilePathName);
        File file = new File(sourceFile);
        FileInputStream fileIn = new FileInputStream(file);
        long fileLen = file.length();
        int readBytes = 0;
        int totalRead = 0;
        byte[] b = new byte[blockSize];

        if ((contentType == null) || (contentType.length() == 0)) {
            response.setContentType("application/x-msdownload");
        } else {
            response.setContentType(contentType);
        }

        response.setContentLength((int) fileLen);

        if (destFileName == null) {
            response.setHeader("Content-Disposition",
                "attachment; filename=".concat(String.valueOf(file.getName())));
        } else if (destFileName.length() == 0) {
            response.setHeader("Content-Disposition", "attachment;");
        } else {
            response.setHeader("Content-Disposition",
                "attachment; filename=".concat(String.valueOf(destFileName)));
        }

        while ((long) totalRead < fileLen) {
            readBytes = fileIn.read(b, 0, blockSize);
            totalRead += readBytes;
            response.getOutputStream().write(b, 0, readBytes);
        }

        fileIn.close();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -