下載中文名的文件顯示文件不存在,下載不帶中文的能成功
String path = getServletContext().getRealPath("/") + "material/";
String filename = req.getParameter("filename");
File file = new File(path + filename);
if(file.exists()){
//設(shè)置相應(yīng)類型application/octet-stream
resp.setContentType("application/x-msdownload");
//設(shè)置頭信息
resp.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
InputStream inputStream = new FileInputStream(file);
ServletOutputStream ouputStream = resp.getOutputStream();
byte b[] = new byte[1024];
int n ;
while((n = inputStream.read(b)) != -1){
ouputStream.write(b,0,n);
2017-05-19
需要設(shè)置編碼: ? ?
resp.setHeader("Content-Disposition", ? ? ? ? ? ? ? ? ? ? ? ?"attachment;filename="+URLEncoder.encode(filename,"utf-8"));
需要導入 ? ? ? ?import java.net.URLEncoder;
2017-04-27
如果沒有對客戶端發(fā)送的中文參數(shù)進行轉(zhuǎn)碼,filename從客戶端發(fā)過來的值就是亂碼,導致在你服務(wù)器目錄下找不到你要的文件,你可以打印一下filename的值到控制臺,我覺得就是編碼問題設(shè)置一下(request.setContextType(),request.setEncoding()....不知道有沒有寫錯,你百度一下亂碼問題。)