有没有一种常见的方式来下载jsp中的所有类型的文件? [英] Is there a common way to download all types of files in jsp?

查看:71
本文介绍了有没有一种常见的方式来下载jsp中的所有类型的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是jsp代码snippt:

 <%
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; ((i = fileInputStream.read())!= -1){
out.write(i);
while
}
fileInputStream.close();
%>

但是当下载office文件,图像文件时会引发一些错误。当我打开下载的文件时, 文件可能已损坏。



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

解决方案

好的,从不同浏览器下载文件时会出现一些问题。我的例子在MSIE和Mozilla类型的浏览器中需要处理

  public HttpServletResponse getFile(HttpServletRequest请求,HttpServletResponse httpServletResponse,.. .....){
HttpServletResponse response = httpServletResponse;
InputStream in = / *这里您将您的文件读取为BinaryStream * /

String filename =;
String agent = request.getHeader(USER-AGENT);
if(agent!= null&& agent.indexOf(MSIE)!= -1)
{
filename = URLEncoder.encode(/ *这是FILENAME所示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(/ *这是文件名显示给用户* /,UTF8,B);
response.setContentType(application / force-download);
response.addHeader(Content-Disposition,attachment; filename = \+ filename +\);
}


BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
字节乘以[] =新字节[32768];
int index = in.read(by,0,32768);
while(index!= -1){
out.write(by,0,index);
index = in.read(by,0,32768);
}
out.flush();

返回响应;
}

检查一下



已更新

I want to download office files,pdf files,image files,zip files,dll files,exe files based on the user selection.So, I want to download these file types from jsp page.

This is jsp code snippt:

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

But it raising some error when downloading office files, images files.When i open the downloaded files it says "The File may be Corrupted".

Is there any common way to download all types of files in jsp?

解决方案

ok, there are some issues when downloading files from different browsers. My example takes care the handling needed in MSIE and Mozilla type of browsers

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

check this out

UPDATED

这篇关于有没有一种常见的方式来下载jsp中的所有类型的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆