¥0.1
java下载文件中文名字乱码
2个回答
0
采纳
下载文件的代码为:
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
FileInputStream fis = null;
BufferedInputStream buff = null;
OutputStream myout = null;
File file = new File(download_file_path + uuid, filename);
try {
if (!file.exists()) {
response.sendError(404, "File not found!!");
return "";
} else {
response.setContentType("APPLICATION/OCTET-STREAM");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
}
fis = new FileInputStream(file);
buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];
long k = 0;
myout = response.getOutputStream();
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
myout.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (buff != null)
buff.close();
if (myout != null)
myout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
撰写回答