File文件下载
videoAppName: "xxx.exe"//配置文件中配置信息
@Value("${videoAppName}")
private String videoAppName;
/**
* 返回exe文件
* @return
*/
@ApiOperation("返回exe文件")
@GetMapping(value = "/getFile")
public CommonResult<String> getFile(HttpServletRequest request, HttpServletResponse response){
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//文件地址
String fileDownload = ResourceUtils.getURL("classpath:").getPath() + "static/" + videoAppName;
response.setContentType("applicaiton/x-download");
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[2048];
int len = 0;
while((len = bis.read(b)) != -1){
bos.write(b,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return CommonResult.failed("下载失败:" + e.getMessage());
}catch (IOException e) {
e.printStackTrace();
return CommonResult.failed("下载失败:" + e.getMessage());
}finally {
try {
bis.close();
is.close();
bos.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return CommonResult.success("下载成功");
}
以上是 File文件下载 的全部内容, 来源链接: utcz.com/z/518546.html