jsp下载文件代码-解决中文文件名的乱码办法
jsp下载中文的介绍有很多,但是很多都不能彻底解决中文名的乱码问题,有的在ie中正常,在火狐中乱码,下面的代码京测试在两种浏览器中都 能正常显示中文文件名.
- <%@page contentType="text/html;charset=UTF-8" %>
- <%@page import="java.util.*"%>
- <%@page import="java.io.*"%>
- <%@page import="java.net.*"%>
- <%
- String filename = "亲亲宝宝.pdf";
- response.setContentType("application/x-download");//设置为下载
- String filenamedisplay = new String(filename.getBytes(),"iso8859-1");
- response.setHeader("Content-disposition","attachment; filename=" + filenamedisplay);
- System.out.println(filename);
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- try {
- FileInputStream file =new FileInputStream(getServletContext().getRealPath("" + filename));
- bis = new BufferedInputStream(file);
- bos = new 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(final IOException e) {
- } finally {
- if (bis != null)
- bis.close();
- if (bos != null)
- bos.close();
- }
- return;
- %>
欢迎转载,请注明出处:亲亲宝宝