溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot項目中怎么實現(xiàn)文件下載功能

發(fā)布時間:2021-08-07 14:31:37 來源:億速云 閱讀:150 作者:Leah 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)SpringBoot項目中怎么實現(xiàn)文件下載功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

maven 依賴

請您創(chuàng)建好一個 springboot 項目,一定要引入 web 依賴:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId></dependency>

建議引入 thymeleaf 作為前端模板:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

配置 application

在您的 application.yml 中,進(jìn)行如下屬性配置:

file: doc-dir: doc/

該路徑就是待下載文件存放在服務(wù)器上的目錄,為相對路徑,表示與當(dāng)前項目(jar包)的相對位置。

將屬性與 pojo 類自動綁定

springboot 中的注解 @ConfigurationProperties 可以將 application 中定義的屬性與 pojo 類自動綁定。所以,我們需要定義一個 pojo 類來做 application 中 file.doc-dir=doc/ 的配置綁定:

@ConfigurationProperties(prefix = "file")@Datapublic class FileProperties {  private String docDir;}

注解 @ConfigurationProperties(prefix = "file") 在 springboot 應(yīng)用啟動時將 file 為前綴的屬性與 pojo 類綁定,也就是將 application.yml 中的 file.doc-dir 與 FileProperties 中的字段 docDir 做了綁定。

激活配置屬性

在啟動類或其他配置類(@Configuration注解標(biāo)記)上加入 @EnableConfigurationProperties 即可讓 ConfigurationProperties 特性生效。

@SpringBootApplication@EnableConfigurationProperties({FileProperties.class})public class AutoTestApplication {  public static void main(String[] args) {    SpringApplication.run(AutoTestApplication.class, args);  }}

控制層

在控制層我們將以 spring 框架的 ResponseEntity 類作為返回值傳給前端,其中泛型為 spring io 包的 Resource 類,這意味著返回內(nèi)容為 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);  }}

服務(wù)層

服務(wù)層的主要工作是把文件作為 IO 資源加載。注意,在 Service 實現(xiàn)類的構(gòu)造方法中要使用 @Autowired 注入前面定義好的屬性綁定類 FileProperties.

@Servicepublic 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);    }  }}

自定義異常

在服務(wù)層,我們拋出自定義的文件異常 FileException.

public class FileException extends RuntimeException {  public FileException(String message) {    super(message);  }  public FileException(String message, Throwable cause) {    super(message, cause);  }}

前端

在前端 html 頁面,可以使用 a 標(biāo)簽來下載文件,注意在 a 標(biāo)簽中定義 download 屬性來規(guī)定這是一個下載文件。

<a href="/file/download/readme.pdf" rel="external nofollow" download>下載使用手冊</a>

上述就是小編為大家分享的SpringBoot項目中怎么實現(xiàn)文件下載功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI