记得是后台输出文件流的话可以正常下载。但是如果是url跳转的方式就会出现这种情况。

解决方案 »

  1.   

    <%
    // fetch the file
    String filename = request.getParameter("filename");
    String filepath = application.getRealPath("/download/");
    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition","attachment; filename=\""+ filename + "\"");
    java.io.FileInputStream fileInputStream =new java.io.FileInputStream(filepath +"\\"+filename);
    int i;
    while ((i=fileInputStream.read()) != -1) {
    out.write(i);
    }
    fileInputStream.close();
    out.close();
    fileInputStream.close();
    %>
    这是download.jsp里的代码。
      

  2.   


            response.setContentType("application/x-download");
      

  3.   

    之前写过的一个例子参考一下
    public String download() throws Exception {
    String tid = getRequest().getParameter("templateId");
    String path = ServletActionContext.getServletContext().getRealPath("/common/scan/template/" + tid);
    String fileName = getRequest().getParameter("fileName");
    File f = new File(path + "\\" + fileName);
    if (!f.exists()) {
    getResponse().sendError(404, "File not found!");
    return ERROR;
    }
    BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
    byte[] buf = new byte[1024];
    int len = 0; getResponse().reset();
    getResponse().setContentType("application/x-msdownload");
    getResponse().setHeader("Content-Disposition", "attachment; filename=" + f.getName());
    OutputStream out = getResponse().getOutputStream();
    while ((len = br.read(buf)) > 0)
    out.write(buf, 0, len);
    br.close();
    out.close();
    return null;
    }