日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC 特性实现文件下载

發布時間:2024/9/19 javascript 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC 特性实现文件下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼

import java.io.File; import javax.servlet.http.HttpSession; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam;import com.srieservice.dell.ApplicationProperties;@Controller @RequestMapping("/app/download") public class DownloadController {private Logger log = LoggerFactory.getLogger(this.getClass());@Autowiredprivate ApplicationProperties appProperties;@RequestMapping(path= {"", "index"}, method=RequestMethod.GET)public ResponseEntity<byte[]> index(HttpSession session, @RequestParam("filename")String filename) {// 檢查一下是否有下載權限?// 組合文件File downloadFile = new File(this.appProperties.getDocumentFolder(), filename);if (downloadFile.exists()){log.debug("download file : {}", downloadFile.getAbsoluteFile());} else {throw new RuntimeException("文件不存在!");}// 組合 Header,確定 ContentType 和文件名HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", filename); // 構造 ResponseEntityreturn new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(downloadFile), headers, HttpStatus.CREATED);} }

總結

以上是生活随笔為你收集整理的Spring MVC 特性实现文件下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。