007 上傳文件

原文

https://spring.io/guides/gs/uploading-files/

直譯

創建一個Application類

要啓動Spring Boot MVC應用程序,我們首先需要一個啓動器; 在這裏,spring-boot-starter-thymeleaf並spring-boot-starter-web已添加爲依賴項。要使用Servlet容器上載文件,您需要註冊一個MultipartConfigElement類(將在web.xml中)。感謝Spring Boot,一切都是爲您自動配置的!

您開始使用此應用程序所需的只是以下Application課程。

src/main/java/hello/Application.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

作爲自動配置Spring MVC的一部分,Spring Boot將創建一個MultipartConfigElementbean併爲文件上傳做好準備。

創建文件上載控制器

初始應用程序已經包含一些類來處理在磁盤上存儲和加載上傳的文件; 它們都位於hello.storage包裝中。我們將在新的中使用它們FileUploadController。

src/main/java/hello/FileUploadController.java

package hello;

import java.io.IOException;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import hello.storage.StorageFileNotFoundException;
import hello.storage.StorageService;

@Controller
public class FileUploadController {

    private final StorageService storageService;

    @Autowired
    public FileUploadController(StorageService storageService) {
        this.storageService = storageService;
    }

    @GetMapping("/")
    public String listUploadedFiles(Model model) throws IOException {

        model.addAttribute("files", storageService.loadAll().map(
                path -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,
                        "serveFile", path.getFileName().toString()).build().toString())
                .collect(Collectors.toList()));

        return "uploadForm";
    }

    @GetMapping("/files/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {

        Resource file = storageService.loadAsResource(filename);
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=\"" + file.getFilename() + "\"").body(file);
    }

    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
            RedirectAttributes redirectAttributes) {

        storageService.store(file);
        redirectAttributes.addFlashAttribute("message",
                "You successfully uploaded " + file.getOriginalFilename() + "!");

        return "redirect:/";
    }

    @ExceptionHandler(StorageFileNotFoundException.class)
    public ResponseEntity<?> handleStorageFileNotFound(StorageFileNotFoundException exc) {
        return ResponseEntity.notFound().build();
    }

}

這個類帶有註釋,@Controller因此Spring MVC可以選擇並查找路由。每個方法都標記有@GetMapping或@PostMapping將路徑和HTTP操作綁定到特定的Controller操作。

在這種情況下:

  • GET /從中查找當前上傳文件的列表StorageService並將其加載到Thymeleaf模板中。它使用計算實際資源的鏈接MvcUriComponentsBuilder

  • GET /files/{filename}加載資源(如果存在),並將其發送到瀏覽器以使用"Content-Disposition"響應頭進行下載

  • POST /適用於處理多部分消息file並將其提供給StorageService保存

在生產場景中,您更有可能將文件存儲在臨時位置,數據庫或Mongo的GridFS之類的NoSQL存儲中。最好不要使用內容加載應用程序的文件系統。

您需要StorageService爲控制器提供與存儲層(例如文件系統)交互的控件。接口是這樣的:

src/main/java/hello/storage/StorageService.java

package hello.storage;

import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.util.stream.Stream;

public interface StorageService {

    void init();

    void store(MultipartFile file);

    Stream<Path> loadAll();

    Path load(String filename);

    Resource loadAsResource(String filename);

    void deleteAll();

}

示例應用程序中有一個接口的示例實現。如果您想節省時間,可以複製並粘貼它。

創建一個簡單的HTML模板

爲了構建一些有趣的東西,下面的Thymeleaf模板是上傳文件以及顯示已上傳內容的一個很好的例子。

src/main/resources/templates/uploadForm.html

<html xmlns:th="http://www.thymeleaf.org">
<body>

	<div th:if="${message}">
		<h2 th:text="${message}"/>
	</div>

	<div>
		<form method="POST" enctype="multipart/form-data" action="/">
			<table>
				<tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
				<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
			</table>
		</form>
	</div>

	<div>
		<ul>
			<li th:each="file : ${files}">
				<a th:href="${file}" th:text="${file}" />
			</li>
		</ul>
	</div>

</body>
</html>

該模板包含三個部分:

  • 頂部的可選消息,Spring MVC寫入閃存範圍的消息。

  • 允許用戶上傳文件的表單

  • 後端提供的文件列表

調整文件上載限制

配置文件上載時,設置文件大小限制通常很有用。想象一下嘗試處理5GB文件上傳!使用Spring Boot,我們可以MultipartConfigElement使用一些屬性設置調整其自動配置。

將以下屬性添加到現有屬性設置:

src/main/resources/application.properties

spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB

maltipart 設置受限制如下:

  • spring.http.multipart.max-file-size 設置爲128KB,意味着總文件大小不能超過128KB。

  • spring.http.multipart.max-request-size設置爲128KB,表示a的總請求大小multipart/form-data不能超過128KB。

使應用程序可執行

雖然可以將此服務打包爲傳統的WAR文件以部署到外部應用程序服務器,但下面演示的更簡單的方法創建了一個獨立的應用程序。您將所有內容打包在一個可執行的JAR文件中,由一個好的舊Java main()方法驅動。在此過程中,您使用Spring的支持將Tomcat servlet容器嵌入爲HTTP運行時,而不是部署到外部實例。

您還需要一個目標文件夾來上傳文件,所以讓我們增強基本Application類並添加一個Boot CommandLineRunner,它在啓動時刪除並重新創建該文件夾:

src/main/java/hello/Application.java

package hello;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import hello.storage.StorageProperties;
import hello.storage.StorageService;

@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    CommandLineRunner init(StorageService storageService) {
        return (args) -> {
            storageService.deleteAll();
            storageService.init();
        };
    }
}

@SpringBootApplication 是一個便利註釋,添加了以下所有內容:

  • @Configuration 標記該類作爲應用程序上下文的bean定義的來源。

  • @EnableAutoConfiguration 告訴Spring Boot開始根據類路徑設置,其他bean和各種屬性設置添加bean。

  • 通常你會添加@EnableWebMvc一個Spring MVC應用程序,但Spring Boot會在類路徑上看到spring-webmvc時自動添加它。這會將應用程序標記爲Web應用程序並激活關鍵行爲,例如設置DispatcherServlet。

  • @ComponentScan告訴Spring在包中尋找其他組件,配置和服務hello,允許它找到控制器。

該main()方法使用Spring Boot的SpringApplication.run()方法啓動應用程序。您是否注意到沒有一行XML?也沒有web.xml文件。此Web應用程序是100%純Java,您無需處理配置任何管道或基礎結構。

構建可執行的JAR

您可以使用Gradle或Maven從命令行運行該應用程序。或者,您可以構建一個包含所有必需依賴項,類和資源的可執行JAR文件,並運行該文件。這使得在整個開發生命週期中,跨不同環境等將服務作爲應用程序發佈,版本和部署變得容易。

如果您使用的是Gradle,則可以使用./gradlew bootRun。或者您可以使用構建JAR文件./gradlew build。然後你可以運行JAR文件:

java -jar build / libs / gs-uploaded-files-0.1.0.jar

如果您使用的是Maven,則可以使用該應用程序運行該應用程序./mvnw spring-boot:run。或者您可以使用構建JAR文件./mvnw clean package。然後你可以運行JAR文件:

java -jar target / gs-uploaded-files-0.1.0.jar

上面的過程將創建一個可運行的JAR。您也可以選擇構建經典WAR文件。
它運行接收文件上傳的服務器端部分。顯示記錄輸出。該服務應在幾秒鐘內啓動並運行。

在服務器運行時,您需要打開瀏覽器並訪問http:// localhost:8080 /以查看上載表單。選擇一個(小)文件並按“上傳”,您應該從控制器中看到成功頁面。選擇一個太大的文件,你會得到一個醜陋的錯誤頁面。

然後,您應該在瀏覽器窗口中看到類似的內容:

You successfully uploaded <name of your file>!

測試您的應用程序

在我們的應用程序中有多種方法可以測試此特定功能。這是一個利用的示例MockMvc,因此不需要啓動Servlet容器:

src/test/java/hello/FileUploadTests.java

package hello;

import java.nio.file.Paths;
import java.util.stream.Stream;

import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import hello.storage.StorageFileNotFoundException;
import hello.storage.StorageService;

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
public class FileUploadTests {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private StorageService storageService;

    @Test
    public void shouldListAllFiles() throws Exception {
        given(this.storageService.loadAll())
                .willReturn(Stream.of(Paths.get("first.txt"), Paths.get("second.txt")));

        this.mvc.perform(get("/")).andExpect(status().isOk())
                .andExpect(model().attribute("files",
                        Matchers.contains("http://localhost/files/first.txt",
                                "http://localhost/files/second.txt")));
    }

    @Test
    public void shouldSaveUploadedFile() throws Exception {
        MockMultipartFile multipartFile = new MockMultipartFile("file", "test.txt",
                "text/plain", "Spring Framework".getBytes());
        this.mvc.perform(fileUpload("/").file(multipartFile))
                .andExpect(status().isFound())
                .andExpect(header().string("Location", "/"));

        then(this.storageService).should().store(multipartFile);
    }

    @SuppressWarnings("unchecked")
    @Test
    public void should404WhenMissingFile() throws Exception {
        given(this.storageService.loadAsResource("test.txt"))
                .willThrow(StorageFileNotFoundException.class);

        this.mvc.perform(get("/files/test.txt")).andExpect(status().isNotFound());
    }

}

在那些測試中,我們使用各種模擬來設置與Controller的交互,以及StorageService使用Servlet容器本身MockMultipartFile。

有關集成測試的示例,請查看FileUploadIntegrationTests課程。

摘要

恭喜!您剛剛編寫了一個使用Spring處理文件上傳的Web應用程序。

延伸知識

使用 MockMvc 進行controller測試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章