java文件下载时中文文件名显示不出来
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletE...
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String file = new String(request.getParameter("file_path").getBytes("ISO8859-1"),"GBK");
//String file = request.getParameter("file_path");
file = file.substring(file.lastIndexOf("/"), file.length());
String file_path = new String(request.getParameter("file_name").getBytes("ISO8859-1"),"GBK");
//String file_path = request.getParameter("file_name");
String fileName = file;
file = request.getRealPath("/")+"uploadfile\\" + File.separator + file;
fileName = fileName.replace('+', ' ');
file_path = file_path.replace('+', ' ');
file = URLDecoder.decode(file, "GBK");
file_path = URLDecoder.decode(file_path, "GBK");
System.out.println("file--->"+file);
System.out.println("filepath-->"+file_path);
response.reset();
response.setContentType("application/x-download;charset=GBK");
//response.setContentType("application/octet-stream;charset=GBK");
response.setHeader("Content-Disposition", "attachment;filename=" + file_path);
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception ex) {
} finally {
try {
bis.close();
bos.close();
} catch (Exception e) {
}
}
response.flushBuffer();
bis = null;
bos = null;
} 展开
throws ServletException, IOException {
String file = new String(request.getParameter("file_path").getBytes("ISO8859-1"),"GBK");
//String file = request.getParameter("file_path");
file = file.substring(file.lastIndexOf("/"), file.length());
String file_path = new String(request.getParameter("file_name").getBytes("ISO8859-1"),"GBK");
//String file_path = request.getParameter("file_name");
String fileName = file;
file = request.getRealPath("/")+"uploadfile\\" + File.separator + file;
fileName = fileName.replace('+', ' ');
file_path = file_path.replace('+', ' ');
file = URLDecoder.decode(file, "GBK");
file_path = URLDecoder.decode(file_path, "GBK");
System.out.println("file--->"+file);
System.out.println("filepath-->"+file_path);
response.reset();
response.setContentType("application/x-download;charset=GBK");
//response.setContentType("application/octet-stream;charset=GBK");
response.setHeader("Content-Disposition", "attachment;filename=" + file_path);
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception ex) {
} finally {
try {
bis.close();
bos.close();
} catch (Exception e) {
}
}
response.flushBuffer();
bis = null;
bos = null;
} 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询