方式一:
@RequestMapping("mydownload")
? ? public ResponseEntity download(HttpServletResponse response) throws IOException {
? ? ? ? String path = "D:\\111.ts";
? ? ? ? File file = new File(path);
? ? ? ? long size = file.length();
? ? ? ? //為了解決中文名稱亂碼問題 這里是設(shè)置文件下載后的名稱? ? ? ? String fileName = new String("000.ts".getBytes("UTF-8"), "iso-8859-1");
? ? ? ? response.reset();
? ? ? ? response.setHeader("Accept-Ranges", "bytes");
? ? ? ? //設(shè)置文件下載是以附件的形式下載? ? ? ? response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
? ? ? ? response.addHeader("Content-Length", String.valueOf(size));
? ? ? ? ServletOutputStream sos = response.getOutputStream();
? ? ? ? FileInputStream in = new FileInputStream(file);
? ? ? ? BufferedOutputStream outputStream = new BufferedOutputStream(sos);
? ? ? ? byte[] b = new byte[1024];
? ? ? ? int i = 0;
? ? ? ? while ((i = in.read(b)) > 0) {
? ? ? ? ? ? outputStream.write(b, 0, i);
? ? ? ? }
? ? ? ? outputStream.flush();
? ? ? ? sos.close();
? ? ? ? outputStream.close();
? ? ? ? in.close();
? ? ? ? return new ResponseEntity(HttpStatus.OK);
? ? }
方式二:
//文件下載相關(guān)代碼 與上面的方法類似 @RequestMapping("/download")
? ? public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
? ? ? ? String fileName = "111.mov";
? ? ? ? if (fileName != null) {
? ? ? ? ? ? //當(dāng)前是從該工程的WEB-INF//File//下獲取文件(該目錄可以在下面一行代碼配置)? ? ? ? ? ? String realPath = request.getServletContext().getRealPath(
? ? ? ? ? ? ? ? ? ? "WEB-INF/File/");
? ? ? ? ? ? File file = new File(realPath, fileName);
? ? ? ? ? ? if (file.exists()) {
? ? ? ? ? ? ? ? // 設(shè)置強(qiáng)制下載不打開? ? ? ? ? ? ? ? response.setContentType("application/force-download");
? ? ? ? ? ? ? ? // 設(shè)置文件名? ? ? ? ? ? ? ? response.addHeader("Content-Disposition",
? ? ? ? ? ? ? ? ? ? ? ? "attachment;fileName=" + fileName);
? ? ? ? ? ? ? ? byte[] buffer = new byte[1024];
? ? ? ? ? ? ? ? FileInputStream fis = null;
? ? ? ? ? ? ? ? BufferedInputStream bis = null;
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? fis = new FileInputStream(file);
? ? ? ? ? ? ? ? ? ? bis = new BufferedInputStream(fis);
? ? ? ? ? ? ? ? ? ? OutputStream os = response.getOutputStream();
? ? ? ? ? ? ? ? ? ? int i = bis.read(buffer);
? ? ? ? ? ? ? ? ? ? while (i != -1) {
? ? ? ? ? ? ? ? ? ? ? ? os.write(buffer, 0, i);
? ? ? ? ? ? ? ? ? ? ? ? i = bis.read(buffer);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? System.out.println("success");
? ? ? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? ? ? if (bis != null) {
? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? bis.close();
? ? ? ? ? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (fis != null) {
? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? fis.close();
? ? ? ? ? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
主要原理觸發(fā)瀏覽器下載,設(shè)置response 響應(yīng)頭為response.setHeader("Content-disposition","attachment; filename=testsprint2.xlsx");
然后通過response.getOutputStream 去寫入文件的相應(yīng)字節(jié)