package com.sailing.office.workflow.servlet;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import com.sailing.office.workflow.app.AppFileUtil;
import com.agou.ss.SSTrace;
import com.agou.ss.SSLogiException;
import com.agou.ss.SSDeregulationException;
import com.agou.util.UtFile;
import com.agou.util.UtFormat;/**
 * <p>Title: 文件下载</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
public class SeFileDownload
        extends HttpServlet {        /**
         * 错误码定义
         */
        /*下载的文件不存在*/
        private static final int de40096 = 40096;        /**/
        private static final String CONTENT_TYPE = "application/octet-stream";
        /*文件缓存*/
        private static final int bufferSize = 4096;        /**
         * 初始化
         * @throws ServletException
         */
        public void init() throws ServletException {
        }        /**
         * doGet方法
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                String strFileId = request.getParameter("FILEID");
                String strMode = request.getParameter("MODE");
                if (UtFormat.isEmpty(strMode)) {
                        strMode = "attachment";
                }
                else if (UtFormat.compareStr(strMode, "attachment") == 0) {
                        strMode = "attachment";
                }
                else {
                        strMode = "inline";
                }
                if (UtFormat.isEmpty(strFileId)) {
                        SSDeregulationException de = new SSDeregulationException(de40096, "下载的文件不存在", "SeFileDownload", "doPut()");
                        de.addDetail("要下载的文件ID是:" + strFileId);
                        throw de;
                }
                AppFileUtil aFileUpload = new AppFileUtil();
                //UtFile.setFileId(strFileId);
                UtFile uFile = new UtFile();
                try {
                        uFile = aFileUpload.loadFile(strFileId);
                        if (uFile == null) {
                                SSDeregulationException de = new SSDeregulationException(de40096, "下载的文件不存在", "SeFileDownload", "doPut()");
                                de.addDetail("要下载的文件ID是:" + strFileId);
                                throw de;                        }
                }
                catch (SSLogiException e) {
                        SSTrace.error(e);
                        SSDeregulationException de = new SSDeregulationException(de40096, "下载的文件不存在", "SeFileDownload", "doPut()");
                        de.addDetail("要下载的文件ID是:" + strFileId);
                        throw de;
                }
                //设置要下载的文件长度
                response.reset();
                response.setContentType(CONTENT_TYPE);
                response.setContentLength( (int) uFile.getFileSize());
                //表示在浏览器中打开(inline)/表示另存为(attachment)
                response.setHeader("Content-Disposition", strMode + ";filename=\"" + new String(uFile.getClientFileName().getBytes(), "ISO-8859-1") + "\"");
                //设置缓冲
                byte[] buffer = new byte[bufferSize];
                BufferedOutputStream output = null;
                BufferedInputStream input = null;
                // 写缓冲区:
                try {
                        output = new BufferedOutputStream(response.getOutputStream());
                        input = new BufferedInputStream(new ByteArrayInputStream(uFile.getFileContent()));                        int n = ( -1);
                        do {
                                n = input.read(buffer, 0, bufferSize);
                                if (n != ( -1)) {
                                        output.write(buffer, 0, n);
                                }
                        }
                        while (n != ( -1));
                        response.flushBuffer();
                }
                catch (java.net.SocketException e) {
                        SSTrace.error("中止下载文件", e);
                }
                catch (Exception e) {
                        SSTrace.error("文件下载时出现异常:", e);
                }
                finally {
                        if (input != null) {
                                input.close();
                        }
                        if (output != null) {
                                output.close();
                        }
                }        }        /**
         * 执行doPut方法
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
        public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                doGet(request, response);
        }        /**
         *
         */
        public void destroy() {
        }        /**
         * 根据后缀名取得当前Content_TYPE
         * @param extFileName
         * @return
         * @throws java.lang.Exception
         */
        public String findFilePostfix(String extFileName) {
                String contentType = "contentType/octet-stream";
                if (extFileName.equals("txt")) {
                        contentType = "text/plain";                }
                if (extFileName.equals("html") || extFileName.equals("htm")) {
                        contentType = "text/html";                }
                if (extFileName.equals("doc")) {
                        contentType = "application/msword";                }
                if (extFileName.equals("xls")) {
                        contentType = "application/vnd.ms-excel";                }
                if (extFileName.equals("ppt")) {
                        contentType = "application/vnd.ms-powerpoint";                }
                if (extFileName.equals("pdf")) {
                        contentType = "application/pdf";                }
                if (extFileName.equals("jpg") || extFileName.equals("gif") || extFileName.equals("bmp") || extFileName.equals("png") ||
                    extFileName.equals("psd")) {
                        contentType = "image/" + extFileName;                }
                return contentType;
        }
}

解决方案 »

  1.   

    AppFileUtil aFileUpload = new AppFileUtil();
                    //UtFile.setFileId(strFileId);
                    UtFile uFile = new UtFile();
                    try {
                            uFile = aFileUpload.loadFile(strFileId);
    得到的是一个文件对象。里边。其实也就是要下载的文件信息,和文件内容
      

  2.   

    你们在使用时。将AppFileUtil换成你们自己的就行了
      

  3.   

    谢谢!不过简单的HTML代码能否实现这个功能呢?
      

  4.   

    其实简单的<a href=后面直接跟文件的地址就可以了,因为我要求的比较简单.