用户工具

站点工具


spring:upload

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

spring:upload [2018/08/09 22:12]
guest 创建
spring:upload [2018/08/09 22:28] (当前版本)
guest
行 1: 行 1:
 +相关依赖
         <​dependency>​         <​dependency>​
             <​groupId>​org.springframework.boot</​groupId>​             <​groupId>​org.springframework.boot</​groupId>​
行 8: 行 9:
         </​dependency>​         </​dependency>​
  
 +文件上传服务
 <​code>​ <​code>​
 @Controller @Controller
行 60: 行 62:
  
 <​code>​ <​code>​
-package hello.storage;​ 
- 
-import java.io.IOException;​ 
-import java.io.InputStream;​ 
-import java.net.MalformedURLException;​ 
-import java.nio.file.Files;​ 
-import java.nio.file.Path;​ 
-import java.nio.file.Paths;​ 
-import java.nio.file.StandardCopyOption;​ 
-import java.util.stream.Stream;​ 
- 
-import org.springframework.beans.factory.annotation.Autowired;​ 
-import org.springframework.core.io.Resource;​ 
-import org.springframework.core.io.UrlResource;​ 
-import org.springframework.stereotype.Service;​ 
-import org.springframework.util.FileSystemUtils;​ 
-import org.springframework.util.StringUtils;​ 
-import org.springframework.web.multipart.MultipartFile;​ 
- 
 @Service @Service
 public class FileSystemStorageService implements StorageService { public class FileSystemStorageService implements StorageService {
 +    //​storage.location=upload-dir
     private final Path rootLocation;​     private final Path rootLocation;​
 +    //​@ConfigurationProperties("​storage"​)
     @Autowired     @Autowired
     public FileSystemStorageService(StorageProperties properties) {     public FileSystemStorageService(StorageProperties properties) {
         this.rootLocation = Paths.get(properties.getLocation());​         this.rootLocation = Paths.get(properties.getLocation());​
     }     }
- 
     @Override     @Override
     public void store(MultipartFile file) {     public void store(MultipartFile file) {
行 165: 行 147:
 } }
 </​code>​ </​code>​
 +
 +测试代码
 +
 +  spring.servlet.multipart.max-file-size=128KB
 +  spring.servlet.multipart.max-request-size=128KB
 +  spring.http.multipart.enabled=false
 +
 +<​code>​
 +@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());​
 +    }
 +}
 +</​code>​
 +
  
  
  
  
spring/upload.txt · 最后更改: 2018/08/09 22:28 由 guest