download 文件下载

@Controller
public class FileDownload extends BaseController{

@RequestMapping("download/{path}/{fileName}.{suffix}")
public void download(HttpServletRequest request,HttpServletResponse response,@PathVariable String path,@PathVariable String fileName,@PathVariable String suffix){
try{
String localBasePath = request.getSession().getServletContext().getRealPath("");
String filePath = localBasePath+SysPropertiesCache.getValue(path)+"/"+fileName+"."+suffix;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\""+fileName+"."+suffix+"\""); 
File file = new File(filePath);
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
   fis.read(buffer);
   fis.close();
   
   OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
   toClient.write(buffer);
       toClient.flush();
       toClient.close();
}catch(Exception e){
throw new RuntimeException("资源不存在!");
}
}

你可能感兴趣的:(download 文件下载)