用jsp实现中文名文件下载,可是下载到的文件都是空白文件,为什么呢?要怎么解决?
我的代码如下:<%java.io.BufferedInputStreambis=null;java.io.BufferedOutputStreambos=null;Str...
我的代码如下:
<%
java.io.BufferedInputStream bis=null;
java.io.BufferedOutputStream bos=null;
String filename=request.getParameter("downname");
filename=new String(filename.getBytes("iso8859-1"),"gb2312");
try{
//String filename=request.getParameter("filename");
//filename=new String(filename.getBytes("iso8859-1"),"gb2312");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));
bis=new java.io.BufferedInputStream(new java.io.FileInputStream(config.getServletContext().getRealPath("resource/" + filename)));
bos=new java.io.BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
}
catch(Exception e){
e.printStackTrace();
}
finally {
if (bis != null)bis.close();
if (bos != null)bos.close();
}
%>
没有悬赏了真的很对不起,但是真的很想学到这方面的知识,多谢了 展开
<%
java.io.BufferedInputStream bis=null;
java.io.BufferedOutputStream bos=null;
String filename=request.getParameter("downname");
filename=new String(filename.getBytes("iso8859-1"),"gb2312");
try{
//String filename=request.getParameter("filename");
//filename=new String(filename.getBytes("iso8859-1"),"gb2312");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));
bis=new java.io.BufferedInputStream(new java.io.FileInputStream(config.getServletContext().getRealPath("resource/" + filename)));
bos=new java.io.BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
}
catch(Exception e){
e.printStackTrace();
}
finally {
if (bis != null)bis.close();
if (bos != null)bos.close();
}
%>
没有悬赏了真的很对不起,但是真的很想学到这方面的知识,多谢了 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询