文件已经生成,直接给定url即可下载。前提是生成的文件在web目录中。

解决方案 »

  1.   

    public class FileDownloadServlet extends BaseHttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doPost(request, response);
    }
    public void init() throws ServletException {
    }

    private static HashMap<String,String> _MIME_TYPE_MAP = new HashMap<String,String>();
    static {
    _MIME_TYPE_MAP.put("jpeg", "image/jpeg");
    _MIME_TYPE_MAP.put("jpg", "image/jpeg");
    _MIME_TYPE_MAP.put("jpe", "image/jpeg");
    _MIME_TYPE_MAP.put("png", "image/png");
    }

    private void setResponseContentType(HttpServletResponse response,
    String filetype, String description) throws Exception {
    String lowerFileType = filetype.toLowerCase();
    String contentType = (String) _MIME_TYPE_MAP.get(lowerFileType);
    //不是图片格式信息
    response.setHeader("Content-Type", contentType);
    response.setHeader("Content-Disposition", "attachment; filename=\""
    + java.net.URLEncoder.encode(description,"UTF-8") +"\"");
    }
    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    try {
    setResponseContentType(response, filetype, "hhhhhhhhh");
    OutputStream out = response.getOutputStream();
    java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(
    out);
    InputStream in = new FileInputStream(new File(filepath));
    java.io.BufferedInputStream bin = new java.io.BufferedInputStream(
    in);
    while (bin.available() > 0) {
    byte[] b = new byte[512];
    int nResult = bin.read(b);
    if (nResult == -1)
    break;
    bout.write(b, 0, nResult);
    }
    bout.flush();
    } catch (Exception e) {
    log.error("", e);
    }
    }
    }
      

  2.   

    String filetype,chinese wholesalers
    uggdescription) throws Exception {
    String lowerFileType = filetype.toLowerCase();
    String contentType = (String) _MIME_TYPE_MAP.get(lowerFileType);