springboot 下載文件

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import Java.nio.file.Paths; /** * 下載圖片 * * @author zcqshine */ @RestController @RequestMapping("download") public class DownloadController { private final ResourceLoader resourceLoader; @Value("${upload.file.path}") private String filePath; @Autowired public DownloadController(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } @GetMapping(value = "/{filename:.+}") public ResponseEntity<?> getFile(@PathVariable String filename) { try { String path = Paths.get(filePath, filename).toString(); Resource resource = resourceLoader.getResource("file:" + path); return ResponseEntity.ok(resource); } catch (Exception e) { throw e; } } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章