【问题标题】:Is there a common way to download all types of files in jsp?有没有一种通用的方法可以在jsp中下载所有类型的文件?
【发布时间】:2012-05-28 23:34:51
【问题描述】:

我想根据用户选择下载office文件、pdf文件、图像文件、zip文件、dll文件、exe文件。所以,我想从jsp页面下载这些文件类型。

这是jsp代码片段:

<% 
String filename = "Sample1.docx"; 
String filepath = "e:\\temp\\"; 
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();
%>

但在下载办公文件、图片文件时会出现一些错误。当我打开下载的文件时,它会显示“文件可能已损坏”。

有没有通用的方法可以下载jsp中所有类型的文件?

【问题讨论】:

标签: java html jsp jakarta-ee


【解决方案1】:

问题出在 JSP 的“Out.write”,无法写入字节流...

用servlet替换了jsp文件...

代码sn-p是:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        String filename = (String) request.getAttribute("fileName");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment;filename="+filename);

        File file = new File(filename);
        FileInputStream fileIn = new FileInputStream(file);
        ServletOutputStream out = response.getOutputStream();

        byte[] outputByte = new byte[(int)file.length()];
        //copy binary contect to output stream
        while(fileIn.read(outputByte, 0, (int)file.length()) != -1)
        {
        out.write(outputByte, 0, (int)file.length());
        }
     }

现在你可以下载所有类型的文件....

【讨论】:

    【解决方案2】:

    好的,从不同浏览器下载文件时存在一些问题。我的示例关注 MSIE 和 Mozilla 类型的浏览器所需的处理

    public HttpServletResponse getFile (HttpServletRequest request ,HttpServletResponse httpServletResponse, .......){
              HttpServletResponse response = httpServletResponse;
              InputStream in =/*HERE YOU READ YOUR FILE AS BinaryStream*/
    
              String filename = "";
              String agent = request.getHeader("USER-AGENT");
              if (agent != null && agent.indexOf("MSIE") != -1)
              {
                filename = URLEncoder.encode(/*THIS IS THE FILENAME SHOWN TO THE USER*/, "UTF8");
                response.setContentType("application/x-download");
                response.setHeader("Content-Disposition","attachment;filename=" + filename);
              }
              else if ( agent != null && agent.indexOf("Mozilla") != -1)
              {
                response.setCharacterEncoding("UTF-8");
                filename = MimeUtility.encodeText(/*THIS IS THE FILENAME SHOWN TO THE USER*/, "UTF8", "B");
                response.setContentType("application/force-download");
                response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
              }
    
    
              BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
              byte by[] = new byte[32768];
              int index = in.read(by, 0, 32768);
              while (index != -1) {
                  out.write(by, 0, index);
                  index = in.read(by, 0, 32768);
              }
              out.flush();
    
              return response;
    }
    

    看看这个

    更新

    【讨论】:

      【解决方案3】:

      您的问题是 JSP 中的 out 变量是 JspWriter,这是一个字符流,因此您的二进制文件会被更改。您最好直接使用 servlet 来实现这个特定目的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-21
        • 2011-12-03
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        • 2021-11-21
        • 1970-01-01
        • 2023-03-06
        相关资源
        最近更新 更多