controller
package com.example.course05.controller;
import com.example.course05.config.LogConfig;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.UUID;
@RestController
@RequestMapping("/test")
public class LogController {
@Autowired
private LogConfig logConfig;
@PostMapping("/upload")
@ApiOperation("上传")
@ResponseBody
public void upload(MultipartFile file) throws IOException {
// 获得 classpath 的绝对路径
String realPath = ResourceUtils.getURL("classpath:").getPath() + logConfig.getFile();
File newFile = new File(realPath);
// 如果文件夹不存在、则新建
if (!newFile.exists()) newFile.mkdirs();
// 上传
String fileName = LocalDate.now() + UUID.randomUUID().toString().replace("-","") + file.getOriginalFilename();
file.transferTo(new File(newFile, fileName));
}
@GetMapping("/download/{fileName}")
@ApiOperation("下载")
public void download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
// 获得待下载文件所在文件夹的绝对路径
String realPath = ResourceUtils.getURL("classpath:").getPath() + logConfig.getFile();
// 获得文件输入流
FileInputStream inputStream = new FileInputStream(new File(realPath, fileName));
// 设置响应头、以附件形式打开文件
response.setHeader("content-disposition", "attachment; fileName=" + fileName);
ServletOutputStream outputStream = response.getOutputStream();
int len = 0;
byte[] data = new byte[1024];
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
outputStream.close();
inputStream.close();
}
}
config
package com.example.course05.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "filelocation")
@Data
public class LogConfig {
private String file;
}
SwaggerConfig
package com.example.course05.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否开启 (true 开启 false隐藏。生产环境建议隐藏)
//.enable(false)
.select()
//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
.apis(RequestHandlerSelectors.basePackage("com.example.course05.controller"))
//指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//设置文档标题(API名称)
.title("上传下载文件")
//文档描述
.description("接口说明")
//服务条款URL
.termsOfServiceUrl("http://localhost:8081/")
//版本号
.version("0.1")
.build();
}
}
application.properties
# 应用名称
spring.application.name=course05
# 应用服务 WEB 访问端口
server.port=8081
# 用来指定服务器端文件大小的限制
spring.servlet.multipart.max-file-size=300MB
# 用来指定客户端文件大小的限制
spring.servlet.multipart.max-request-size=300MB
filelocation.file=static/files