构建一个简单的spring-boot的web项目实现上传和下载文件

附源码:构建一个简单的spring-boot的web项目实现上传和下载文件

目标:

本文目标在于:

  • 构建一个简单的spring-boot-web项目实现上传文件和下载文件;
  • spring-boot、web和thymeleaf结合实现web网页功能;
  • spring-boot切片测试和集成测试;

1. 初始化源代码

1.1 从spring网站初始化和下载源代码

在这里插入图片描述

1.2 将下载好的源代码作为maven项目导入idea

其中 pom.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo_upfiles</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo_upfiles</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.4.3</version>
			</plugin>
		</plugins>
	</build>
</project>

2. 上传下载主要功能实现

2.1 application.properties

springboot上传下载属性设置限制

#单个文件上传大小限制
spring.servlet.multipart.max-file-size=128KB
#总文件上传大小限制
spring.servlet.multipart.max-request-size=128KB
2.2 文件存储服务接口规范
  • 规范文件操作接口
  • 文件操作初始化
  • 保存文件
  • 加载和列出所有文件
  • 加载指定文件名字的文件
  • 删除所有文件
package com.example.demo_upfiles.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();
}

2.3 文件存储服务实现
  • 初始化时创建根目录
  • springboot web已经根据设定把文件下载下来,我们需要做的就是把MultipartFile文件转存一个位置而已
  • 文件遍历采用流式处理
package com.example.demo_upfiles.storage;

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.web.multipart.MultipartFile;

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;

@Service
public class FileSystemStorageService implements StorageService{
    private final Path rootLocation;
    @Autowired
    public FileSystemStorageService(StorageProperties properties){
        this.rootLocation = Paths.get(properties.getLocation());
    }
    @Override
    public void init() {
        try {
            Files.createDirectories(this.rootLocation);
        } catch (IOException e) {
            throw new StorageException("Can not initialize storage", e);
        }
    }
    @Override
    public void store(MultipartFile file) {
        try{
            if(file.isEmpty()){
                throw new StorageException("Failed to store empty file.");
            }
            Path destinationFile = this.rootLocation.resolve(Paths.get(file.getOriginalFilename()))
                                        .normalize().toAbsolutePath(); if(!destinationFile.getParent().equals(this.rootLocation.toAbsolutePath())){
                throw new StorageException("Can't store file outside current directory.");
            }
            try(InputStream inputStream = file.getInputStream()){
                Files.copy(inputStream, destinationFile, StandardCopyOption.REPLACE_EXISTING);
            }
        }catch (IOException e){
            throw new StorageException("Failed to store file.", e);
        }
    }
    @Override
    public Stream<Path> loadAll() {
        try {
            return Files.walk(this.rootLocation,1)  
                		.filter(path -> !path.equals(this.rootLocation))    
                		.map(this.rootLocation::relativize);    
        } catch (IOException e) {
            throw new StorageException("Fail to read stored files", e);
        }
    }
    @Override
    public Path load(String filename) {
        return this.rootLocation.resolve(filename);
    }
    @Override
    public Resource loadAsResource(String filename) {
        try {
            Path file = load(filename);
            Resource resource = new UrlResource(file.toUri());
            if (resource.exists() && resource.isReadable()){
                return resource;
            }else{
                throw new StorageFileNotFoundException("Can not read file: " + filename);
            }
        } catch (MalformedURLException e) {
            throw new StorageFileNotFoundException("Can not read file: " + filename, e);
        }
    }
    @Override
    public void deleteAll() {
        FileSystemUtils.deleteRecursively(this.rootLocation.toFile());
    }
}
2.4 Properties基础配置类
  • 定义存储文件的根目录位置
package com.example.demo_upfiles.storage;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("storage")
public class StorageProperties {
    private String location = "upload-dir";
    public void setLocation(String location) {
        this.location = location;
    }
    public String getLocation() {
        return location;
    }
}
2.5 上传下载controller功能实现和开放服务
  • 列出所有已经上传过的文件:listUploadedFiles

    • 列出所有已经上传过的文件,并返回这些文件的URLList给客户端
    • 构建一个可以访问指定controller资源的连接,最终由Stream 返回一个Stream对象
    • 把Stream中的数据累加到List中
  • 提供下载服务:serveFile

    • /file/{filename:.+} 能够过滤请求"/file/至少一个任意字符的文件名称"
    • {filename:.+}表示对文件名变量进行正则匹配
    • URLEncoder.encode可以解决下载文件时自动获取到的文件名乱码变成下划线的情况
package com.example.demo_upfiles;

import com.example.demo_upfiles.storage.StorageFileNotFoundException;
import com.example.demo_upfiles.storage.StorageService;
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.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.stream.Collectors;

@Controller
public class FileUploadController {
    private final StorageService storageService;
    @Autowired
    public FileUploadController(StorageService storageService){
        this.storageService = storageService;
    }

    @GetMapping("/")
    public String listUploadedFiles(Model model){
        model.addAttribute(
                "files",
                storageService.loadAll()
                        .map(
                               path -> MvcUriComponentsBuilder.fromMethodName(
                                       FileUploadController.class,
                                       "serveFile",
                                       path.getFileName().toString()
                               ).build().toUri().toString()
                        ).collect(Collectors.toList())
        );
        return "uploadForm";
    }
    @GetMapping("/file/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String filename){
        Resource file = storageService.loadAsResource(filename);
        String encoderedFileName = "";
        try {
            encoderedFileName = URLEncoder.encode(file.getFilename(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok().header(
                HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=\""+ encoderedFileName +"\""
        ).body(file);
    }
    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes){
        storageService.store(file);
        redirectAttributes.addFlashAttribute("message", "You successfullly uploaded " + file.getOriginalFilename() + "!");
        return "redirect:/";
    }
    @ExceptionHandler(StorageFileNotFoundException.class)
    public ResponseEntity<?> handleStorageFileNotFound(StorageFileNotFoundException exc){
        return ResponseEntity.notFound().build();
    }
}

2.6 启动服务代码
  • 初始化时删除已经上传过的文件

  • 初始化时创建根目录

package com.example.demo_upfiles;

import com.example.demo_upfiles.storage.StorageProperties;
import com.example.demo_upfiles.storage.StorageService;
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;

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

	public static void main(String[] args) {
		SpringApplication.run(DemoUpfilesApplication.class, args);
	}
	
	@Bean
	CommandLineRunner init(StorageService storageService){
		return args -> {
			storageService.deleteAll();
			storageService.init();
		};
	}
}

3. 切片测试

  • 主要测试Controller是否符合预期
    • @SpringBootTest: 说明当前类是一个springboot测试类;可以指定classes为DemoUpfilesApplication.class来描述spring运行的环境,标识测试环境和正常运行环境是相同的
    • @AutoConfigureMockMvc:自动配置模拟的mvc,如此可以不启动服务进行测试;严格的测试需要启动servlet容器服务,并向servlet容器发送controller请求,然后看http返回结果是不是预期结果,但是这样会比较慢,所以采用模拟的测试过程;
  • MockMvc mvc:模拟mvc控制器;
  • @MockBean:注入依赖的模拟Bean对象,不是真的注入进来,而是注入类似正常运行时service对象的模拟对象,该模拟对象实现了和正常运行的service对象相同的行为;
  • @Test:标识一个单元测试
  • given()方法:模拟对象的行为
  • perform(get(…)):执行MockMvcRequestBuilders发送get(还可是post、put、delete等)请求;
    • get(…).contentType()指定编码格式
    • get(…).param()指定请求参数,可带多个
  • andExpect():添加MockMvcResultMatchers验证规则,验证执行结果是否正确;可在perform(…)后多次调用,对多个条件判断,可检测同一web请求的多个方面,包括HTTP响应状态码(response status),响应内容类型(content type),会话中存放的值,检验重定向、model或者header的内容等等;
  • andDo():添加 MockMvcResultHandlers结果处理器,这是可以用于打印结果输出;
  • andReturn():结果返回,然后可以进行下一步的处理;
  • MockMultipartFile:模拟要上传的文件
  • this.mvc.perform(multipart("/").file(mmf)):模拟上传文件请求
  • then(this.storageService).should().store(mmf):校验模拟文件存储服务是否被执行
  • willThrow():模拟抛出异常
package com.example.demo_upfiles;

import com.example.demo_upfiles.storage.StorageFileNotFoundException;
import com.example.demo_upfiles.storage.StorageService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
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.web.servlet.MockMvc;

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

import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@AutoConfigureMockMvc
@SpringBootTest
class DemoUpfilesApplicationTests {
	@Autowired
	private MockMvc mvc;
	@MockBean
	private StorageService storageService;	

	@Test
	public void shouldListAllFiles() throws Exception {
		given(this.storageService.loadAll())	
				.willReturn(
						Stream.of(
								Paths.get("first1.txt"),
								Paths.get("second2.txt")
						)
				);
		this.mvc.perform(get("/"))
				.andExpect(status().isOk())
				.andExpect(
						model().attribute(
								"files",
Matchers.contains("http://localhost/file/first1.txt","http://localhost/file/second2.txt")
						)
				);
	}

	@Test
	public void shouldSaveUploadedFile() throws Exception {
		MockMultipartFile mmf = new MockMultipartFile("file", "test.txt", "text/plain", "Spring Framework".getBytes());
		this.mvc.perform(multipart("/").file(mmf))
				.andExpect(status().isFound())
				.andExpect(header().string("Location", "/"));
		then(this.storageService).should().store(mmf);
	}

	@Test
	public void should404WhenMissingFile() throws Exception {
		given(this.storageService.loadAsResource("test.txt"))
			.willThrow(StorageFileNotFoundException.class);
		this.mvc.perform(get("/file/test.txt"))
				.andExpect(status().isNotFound());

	}

}

4. 集成测试

集成测试如下:

  • 集成测试不仅仅测试controller层或者service层,而是从发送http请求开始
  • 集成测试注入的对象是servlet容器启动后的实际对象,没有模拟对象
package com.example.demo_upfiles;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Matchers.any;

import com.example.demo_upfiles.storage.StorageService;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoUpfilesApplicationIntegrationTests {
	@Autowired
	private TestRestTemplate restTemplate;
	@MockBean
	private StorageService storageService;	//真实注入进来正常运行时的service对象
	@LocalServerPort
	private int port;

	@Test
	public void shouldUploadFile() throws Exception {
		ClassPathResource resource = new ClassPathResource("test.txt", getClass());
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
		map.add("file", resource);
		ResponseEntity<String> response = this.restTemplate.postForEntity("/", map, String.class);
		assertThat(response.getStatusCode()).isEqualByComparingTo(HttpStatus.FOUND);
	assertThat(response.getHeaders().getLocation().toString()).startsWith("http://localhost:" + this.port + "/");
		then(storageService).should().store(any(MultipartFile.class));
	}

	@Test
	public void shouldDownloadFile() throws Exception {
		ClassPathResource resource = new ClassPathResource("test.txt", getClass());
		given(this.storageService.loadAsResource("test.txt")).willReturn(resource);
		ResponseEntity<String> response = this.restTemplate.getForEntity("/file/{filename:.+}", String.class, "test.txt");

		assertThat(response.getStatusCodeValue()).isEqualTo(200);
		assertThat(response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION)).isEqualTo("attachment; filename=\"test.txt\"");
		assertThat(response.getBody()).isEqualTo("asd");
	}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值