直接用URLEncoder.encode(fileName,"UTF-8"),得到的文件名长度会被截断。
解决方法:
文件名先用“GB2312”编码,然后用“ISO8859_1”解码.
代码例子:
filename=URLEncoder.encode(filename,"GB2312");
filename=URLDecoder.decode(filename, "ISO8859_1");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="+filename);
// 直接用IE打开
//response.setHeader("Content-disposition", "filename=" + filename);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(
Constants.listFilePath + directoryname + "\\" + filename1));
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);
}
bos.flush();
} catch (final IOException e) {
log.error(e.toString());
}
解决方法:
文件名先用“GB2312”编码,然后用“ISO8859_1”解码.
代码例子:
filename=URLEncoder.encode(filename,"GB2312");
filename=URLDecoder.decode(filename, "ISO8859_1");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="+filename);
// 直接用IE打开
//response.setHeader("Content-disposition", "filename=" + filename);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(
Constants.listFilePath + directoryname + "\\" + filename1));
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);
}
bos.flush();
} catch (final IOException e) {
log.error(e.toString());
}