Spring 文件下载
程序员文章站
2022-04-09 09:24:12
...
/**
* 文件下载
*/
public static ResponseEntity<byte[]> downloadApp(String driName,String name) throws IOException {
//获取文件路径
String disk=AppDownload.class.getResource("").getPath().substring(1,3)+"\\update\\"+driName+"\\"+name;
//创建文件
File file=new File(disk);
byte[] body;
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=" + file.getName());
HttpStatus statusCode = HttpStatus.OK;
return new ResponseEntity<byte[]>(body, headers, statusCode);
}