public ResponseEntitytestResponseEntity(HttpSession session) throws IOException{
//獲取ServletContext對象
ServletContext servletContext = session.getServletContext();
//獲取賦予其中文件的真實(shí)路徑
String realPath = servletContext.getRealPath("img");
realPath=realPath+ File.separator+"yuanyuan.jpg";
//創(chuàng)建輸入流
FileInputStream is =new FileInputStream(realPath);
//創(chuàng)建字節(jié)數(shù)組
byte[] bytes=new byte[is.available()];
//將流讀到字節(jié)數(shù)組中
is.read(bytes);
//創(chuàng)建HttpHeaders對象設(shè)置響應(yīng)頭信息
MultiValueMapheaders = new HttpHeaders();
//設(shè)置要下載方式以及下載文件的名字
headers.add("Content-Disposition", "attachment;filename=1.jpg");
//設(shè)置響應(yīng)狀態(tài)碼
HttpStatus statusCode = HttpStatus.OK;
//創(chuàng)建ResponseEntity對象
ResponseEntityresponseEntity = new ResponseEntity<>(bytes, headers,
statusCode);
//關(guān)閉輸入流
is.close();
return responseEntity;
}
文件下載功能:只需要修改RealPath里面的地址即可;
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧