下載docx文件不識別,下載文件名變成下劃線


/**
* 下載文件  頁面直接用
*/
@RequestMapping("/download")
@RequiresPermissions("sys:oss:all")
public void download(String id,HttpServletResponse response,HttpServletRequest request) throws Exception {
SysOssEntity ossEntity = new SysOssEntity();
ossEntity=sysOssService.queryObject(Long.valueOf(id));
String fileName=URLDecoder.decode(ossEntity.getDocName(),"UTF-8");
String path =ossEntity.getUrl();
File file = new File(path+File.separator+fileName);
if(file.exists()){

response.setContentType("multipart/form-data"); 

//2不同瀏覽器對編碼識別判斷

if(request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {  
    response.setHeader("Content-Disposition","attachment;"+ "filename="+ new   
String(fileName.getBytes("GBK"),"ISO8859-1"));  
}else{//firefox、chrome、safari、opera  
  response.setHeader("Content-Disposition","attachment;"+  
"filename="+ new String(fileName.getBytes("UTF8"), "ISO8859-1") );  
}  

// IOUtils.write(data, response.getOutputStream());  
byte[] buff = new byte[1024];
BufferedInputStream bis =null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int  i = bis.read(buff) ;
while(i != -1){
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bis != null){
try{
bis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章