javaweb簡單通過springmvc實現檔案下載
阿新 • • 發佈:2019-02-15
@RequestMapping("/downloadApk")
public ResponseEntity<byte[]> DownloadAPK(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//確定文字編碼
req.setCharacterEncoding("utf-8");
//確定根目錄
String path = "D:\\apache-tomcat-6.0.35\\webapps\\Spring_mvc\\xx.apk";
//檔名
String fileName = "xx.apk";
File file = null;
HttpHeaders headers =null;
file = new File(path);
//請求頭
headers = new HttpHeaders();
String fileName1 =new String(fileName.getBytes("UTF-8"),"iso-8859-1");//解決檔名亂碼
//通知瀏覽器以attachment(下載方式)開啟圖片
headers.setContentDispositionFormData("attachment",fileName1);
//application/octet-stream二進位制流資料(最常見的檔案下載)。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
public ResponseEntity<byte[]> DownloadAPK(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//確定文字編碼
req.setCharacterEncoding("utf-8");
//確定根目錄
String path = "D:\\apache-tomcat-6.0.35\\webapps\\Spring_mvc\\xx.apk";
//檔名
String fileName = "xx.apk";
File file = null;
HttpHeaders headers =null;
file = new File(path);
//請求頭
headers = new HttpHeaders();
String fileName1 =new String(fileName.getBytes("UTF-8"),"iso-8859-1");//解決檔名亂碼
//通知瀏覽器以attachment(下載方式)開啟圖片
headers.setContentDispositionFormData("attachment",fileName1);
//application/octet-stream二進位制流資料(最常見的檔案下載)。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);