English 中文(简体)
在jsp下载所有类型的文件是否有常见方式?
原标题:Is there a common way to download all types of files in jsp?

我想下载办公室文件、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下载所有类型的文件有什么共同的方法吗?

最佳回答

OK, 从不同的浏览器下载文件时会有一些问题 。 我的示例会关注 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;
}

检查这个出来

< 强度 > Uped

问题回答

您的问题是 JSP 中的 out 变量是 < a href=" http://docs.oracle.com/javaee/1.4/api/javax/servlet/jsp/JspWriter.html" rel=“nofollow” >JspWriter ,这是一个字符流,因此您的二进制文件会被更改。您最好直接为此使用服务器。

问题在于 JSP s "Out. write" 无法写字节流...

将jsp 文件替换为 servlet...

代码片断是:

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());
        }
     }

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签