第一种: 设置 response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));这里将文件名编码成UTF-8的格式,就不会出现URL出错了。IE6下注意中文文字不能超过超过17个。
第二种:设置response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) );将中文名编码为ISO8859-1的方式。不过该编码只支持简体中文.
例子:
else if("photos".equals(action))
{
File file=new File("f://陈少军.jpg");
String ss=file.getName();
response.setCharacterEncoding("UTF-8");
response.setContentType("multipart/form-data;charset=Utf-8");
response.setHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode(ss,"UTF-8"));
// response.addHeader("content-type", "application/x-msdownload;");
// response.addHeader("Content-Disposition","attachment;filename="+ss);
// response.addHeader("content-length", "");
InputStream in=new FileInputStream(file);
OutputStream out=response.getOutputStream();
int len=0;
byte [] buf=new byte[1024];
while((len=in.read(buf))!=-1)
{
out.write(buf,0,len);
}
in.close();
out.close();
return null;
}