@RequestMapping(value ="/downloadFile", method ={RequestMethod.GET,RequestMethod.POST},produces ="text/html;charset=UTF-8")
@ResponseBody
publicvoid download(@RequestParam(value="bzbg_wsm",defaultValue="", required=true) String bzbg_wsm,
@RequestParam(value="bzbg_wslj",defaultValue="", required=true) String bzbg_wslj,
HttpServletRequestrequest, HttpServletResponse response) throws UnsupportedEncodingException {
//bzbg_wsm=newString(bzbg_wsm.getBytes("iso-8859-1"),"utf-8");
response.addHeader("Content-Disposition","attachment;filename=" + new String(bzbg_wsm.getBytes("gb2312"), "ISO8859-1" ) );
FileInputStream fis=null;
OutputStream os=null;
byte bytes[]=new byte[1024];
int len=0;
try {
fis=new FileInputStream(bzbg_wslj);
os=response.getOutputStream();
while((len=fis.read(bytes))>0){
os.write(bytes,0,len);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
os.close();
fis.close();
}catch (Exception e2) {
e2.printStackTrace();
}
}
}
@RequestMapping(value ="/downloadFile", method ={RequestMethod.GET,RequestMethod.POST},produces ="text/html;charset=UTF-8")
@ResponseBody
publicvoid download(@RequestParam(value="bzbg_wsm",defaultValue="", required=true) String bzbg_wsm,
@RequestParam(value="bzbg_wslj",defaultValue="", required=true) String bzbg_wslj,
HttpServletRequestrequest, HttpServletResponse response) throws UnsupportedEncodingException {
//bzbg_wsm=newString(bzbg_wsm.getBytes("iso-8859-1"),"utf-8");
response.addHeader("Content-Disposition","attachment;filename=" + new String(bzbg_wsm.getBytes("gb2312"), "ISO8859-1" ) );
FileInputStream fis=null;
OutputStream os=null;
byte bytes[]=new byte[1024];
int len=0;
try {
fis=new FileInputStream(bzbg_wslj);
os=response.getOutputStream();
while((len=fis.read(bytes))>0){
os.write(bytes,0,len);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
os.close();
fis.close();
}catch (Exception e2) {
e2.printStackTrace();
}
}
}
页面传输中文乱码解决方式
页面wslj=encodeURIComponent(wslj);
后台:wslj=java.net.URLDecoder.decode(wslj,"UTF-8");
xmmc=newString(xmmc.trim().getBytes("ISO-8859-1"),"UTF-8");
第一种:设置 response.setHeader("Content-Disposition","attachment; filename=" + java.net.URLEncoder.encode(fileName,"UTF-8"));这里将文件名编码成UTF-8的格式,就不会出现URL出错了。IE6下注意中文文字不能超过超过17个。
第二种:设置response.setHeader( "Content-Disposition","attachment;filename=" + newString( fileName.getBytes("gb2312"), "ISO8859-1" ) );将中文名编码为ISO8859-1的方式。不过该编码只支持简体中文.
按照上诉方式,可以综合一下两种方式解决绝大部分中文问题。
fileName =URLEncoder.encode(fileNameSrc,"UTF-8");