我做了一个download.jsp与要下载文件放到同一个文件夹
英文文件名上传下载一切正常,中文文件上传后,文件名不变还是原来的中文名,但下载的时候,被重命名,且下载同一个文件(中文名)每次下载的名字还不一样,如:CAOLEZKD..doc ,CA2J0JXE..doc,CAN6EXN3..doc,CA89EF8L..doc
每次都是类似的名字。
不知你们是否也遇到过?我就是想要下载的名字和原来上传的一样(这点很重要)。
下面的我的download代码:
<%@ page contentType= "text/html; charset=GBK " import= "disc.*,javax.servlet.jsp.jstl.sql.Result " %>
<%@ include file= "/chk.jsp " %>
<%@page import= "java.util.* "%>
<%@page import= "java.io.* "%>
<%@page import= "java.net.* "%>
<html>
<head>
<title> 管理 </title>
</head>
<body leftmargin= "2 " topmargin= "0 " marginwidth= "0 " marginheight= "0 ">
<%
String thefilename = " ";
if (request.getParameter( "file ") != null) {
thefilename = request.getParameter( "file ");
}
String filename=new String(thefilename.getBytes( "ISO-8859-1 "));
response.setContentType( "application/msword ");
response.setHeader( "Content-disposition ", "attachment; filename= "+filename);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath( "\\uploadfiles\\ " + filename)));
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) {
//System.out.println ( "出现IOException. " + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
%>
</body>
</html>
------解决方案--------------------
你的文件名需要转码
------解决方案--------------------
String filename=new String(thefilename.getBytes( "ISO-8859-1 "));
已经转了!
------解决方案--------------------
String filename=new String(thefilename.getBytes( "ISO-8859-1 "),GBK)?