溫馨提示×

溫馨提示×

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

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

怎么在Spring Boot 項目中實現(xiàn)一個文件下載功能

發(fā)布時間:2021-03-11 15:53:53 來源:億速云 閱讀:264 作者:Leah 欄目:編程語言

怎么在Spring Boot 項目中實現(xiàn)一個文件下載功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

(一)需求

在您的 springboot 項目中,可能會存在讓用戶下載文檔的需求,比如讓用戶下載 readme 文檔來更好地了解該項目的概況或使用方法。

所以,您需要為用戶提供可以下載文件的 API ,將用戶希望獲取的文件作為下載資源返回給前端。

(二)代碼

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 中,進行如下屬性配置:

file:
 doc-dir: doc/

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

怎么在Spring Boot 項目中實現(xiàn)一個文件下載功能

將屬性與 pojo 類自動綁定

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 做了綁定。

激活配置屬性

在啟動類或其他配置類(@Configuration注解標記)上加入 @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);
  }
}

服務層

服務層的主要工作是把文件作為 IO 資源加載。注意,在 Service 實現(xiàn)類的構造方法中要使用 @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);
    }
  }
}

自定義異常

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

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

前端

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

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

關于怎么在Spring Boot 項目中實現(xiàn)一個文件下載功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

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

AI