/**
* 下載(傳附件的id下載附件,適合後臺多個附件上傳,前臺對應下載)
* @throws IOException
*/
@ResponseBody
@RequestMapping(value="/font/annex/download",produces="text/plain;charset=UTF-8")
public void downloadAnnexById(@RequestParam Integer id, Model model, HttpServletRequest request, HttpServletResponse response) throws IOException{
List<Annex> annexList = null;
annexList = annexService.queryAnnexListById(20, id);
if(GeneralUtil.isNotNull(annexList) && annexList.size() > 0){
Annex annex = annexList.get(0);
if(annex.getPath() != null &&! annex.getPath().equals("")){
//下載
//設置文件MIME類型
String fileName = annex.getName();
String filePath = annex.getPath();
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
fileName = URLEncoder.encode(fileName, "UTF-8");
} else {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}
response.setContentType(request.getServletContext().getMimeType(fileName));
//設置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
//讀取目標文件,通過response將目標文件寫到客戶端
//獲取目標文件的絕對路徑
String fullFileName = request.getServletContext().getRealPath(filePath);
File file = new File(fullFileName);
if(file.exists()){
//讀取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
//寫文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
}
}
}
}