SpringBoot 專案中實現檔案下載功能
技術標籤:SpringBoot常用操作springjavaspring boot後端http
springboot 專案中,可能會存在讓使用者下載文件的需求,比如讓使用者下載 readme、模板等 文件來更好地瞭解該專案的概況或使用方法。 所以,您需要為使用者提供可以下載檔案的 API ,將使用者希望獲取的檔案作為下載資源返回給前端使用者。
1.建立好一個 springboot 專案,一定要引入 web 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web</artifactId>
</dependency>
2.在application.yml 中,進行如下屬性配置:
file:
doc-dir: doc/
該路徑就是待下載檔案存放在伺服器上的目錄,為相對路徑,表示與當前專案(jar包)的相對位置。
3.springboot 中的註解 @ConfigurationProperties 可以將 application 中定義的屬性與 pojo 類自動繫結。所以,我們需要定義一個 pojo 類來做 application 中 file.doc-dir=doc/ 的配置繫結:
@ConfigurationProperties (prefix = "file")
@Data
public class FileProperties {
private String docDir;
}
註解 @ConfigurationProperties(prefix = “file”) 在 springboot 應用啟動時將 file 為字首的屬性與 pojo 類繫結,也就是將 application.yml 中的 file.doc-dir 與 FileProperties 中的欄位 docDir 做了繫結。
4.在啟動類或其他配置類(@Configuration註解標記)上加入 @EnableConfigurationProperties 即可讓 ConfigurationProperties 特性生效。
@SpringBootApplication
@EnableConfigurationProperties({FileProperties.class})
public class AutoTestApplication {
public static void main(String[] args) {
SpringApplication.run(AutoTestApplication.class, args);
}
}
5.在控制層我們將以 spring 框架的 ResponseEntity 類作為返回值傳給前端,其中泛型為 spring io 包的 Resource 類,這意味著返回內容為 io 流資源;並在返回體頭部新增附件,以便於前端頁面下載檔案。
@RestController
@RequestMapping("file")
public class FileController {
private static final Logger logger = LoggerFactory.getLogger(FileController.class);
@Autowired
private FileService fileService;
@GetMapping("download/{fileName}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName,
HttpServletRequest request) {
Resource resource = fileService.loadFileAsResource(fileName);
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException e) {
logger.error("無法獲取檔案型別", e);
}
if (contentType == null) {
contentType = "application/octet-stream";
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
}
6.服務層的主要工作是把檔案作為 IO 資源載入。注意,在 Service 實現類的構造方法中要使用 @Autowired 注入前面定義好的屬性繫結類 FileProperties.
@Service
public class FileServiceImpl implements FileService {
private final Path filePath;
@Autowired
public FileServiceImpl(FileProperties fileProperties) {
filePath = Paths.get(fileProperties.getDocDir()).toAbsolutePath().normalize();
}
@Override
public Resource loadFileAsResource(String fileName) {
Path path = filePath.resolve(fileName).normalize();
try {
UrlResource resource = new UrlResource(path.toUri());
if (resource.exists()) {
return resource;
}
throw new FileException("file " + fileName + " not found");
} catch (MalformedURLException e) {
throw new FileException("file " + fileName + " not found", e);
}
}
}
7.自定義異常
在服務層,我們丟擲自定義的檔案異常 FileException.
public class FileException extends RuntimeException {
public FileException(String message) {
super(message);
}
public FileException(String message, Throwable cause) {
super(message, cause);
}
}
8.前端
在前端 html 頁面,可以使用 a 標籤來下載檔案,注意在 a 標籤中定義 download 屬性來規定這是一個下載檔案。
<a href="/file/download/readme.pdf" download>下載使用手冊</a>
如果對你有幫助,請一定要幫忙點贊好評,給予創作的動力…