videoAppName: "xxx.exe"//配置文件中配置信息
@Value("${videoAppName}")
private String videoAppName;
/** * 返回exe文件 * @return */ @ApiOperation("返回exe文件") @GetMapping(value = "/getFile") public void getFile(HttpServletResponse response){ InputStream is = null; OutputStream os = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //文件地址 String fileDownload = videoBasePath.replace("/",File.separator) + File.separator + videoAppName; response.setContentType("applicaiton/x-download;charset=utf-8"); response.addHeader("Content-Disposition", "attachment;filename=" + videoAppName); is = new FileInputStream(new File(fileDownload)); bis = new BufferedInputStream(is); os = response.getOutputStream(); bos = new BufferedOutputStream(os); byte[] b = new byte[1024]; int len = 0; while((len = bis.read(b)) != -1){ bos.write(b,0,len); } bos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally { try { if(bis != null)bis.close(); if(is != null)is.close(); if(bos != null)bos.close(); if(os != null)os.close(); } catch (IOException e) { e.printStackTrace(); } } }
来源:oschina
链接:https://my.oschina.net/u/3141521/blog/4411648