摘要:介绍springboot
作为静态文件服务器的配置、上传文件到本地、本地文件下载的几种简要实现;实际使用则按需修改即可。
pom.xml配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
application.yml
配置
最好加上斜杠,因为不加斜杠你访问的路径则需要加上最后一个文件夹名称才能访问到文件
file:
upload:
local-storage: /data/
实际代码
StaticsApplication
.class
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class StaticsApplication {
public static void main(String[] args) {
SpringApplication.run(StaticsApplication.class, args);
}
}
FileUploadProperties
.class
用做配置属性
@Configuration
@Data
@ConfigurationProperties(prefix = FileUploadProperties.CONFIG_PREFIX)
public class FileUploadProperties {
public static final String CONFIG_PREFIX = "file.upload";
/** 存储路径 */
private String localStorage;
}
StaticResourceConfig
.class
静态文件配置类,访问前缀
@Configuration
public class StaticResourceConfig implements WebMvcConfigurer {
/** 必须这样 */
private static final String FOLDER_PRE = "file:";
@Autowired
private FileUploadProperties fileUploadProperties;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static-test/**").addResourceLocations(FOLDER_PRE + fileUploadProperties.getLocalStorage());
}
}
FileController
.class
注意该上传写入文件有两种方式,一种是
inputFile.transferTo(file);
这种需要区分windows
路径还是linux
路径,windows
则需要指定磁盘如D:/data/
,推荐使用FileUtils.writeByteArrayToFile(file, inputFile.getBytes(), false);
这种方式不用区分windows
还是linux
@CrossOrigin
@RestController
@RequestMapping(value = "/file")
public class FileController {
@Autowired
private FileUploadProperties fileUploadProperties;
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws Exception{
return this.uploadOneMultipleFile(file,Boolean.TRUE);
}
@GetMapping("/download")
public void download(HttpServletResponse response,String fileName) throws Exception{
File file = new File(fileUploadProperties.getLocalStorage()+fileName);
if (!file.exists() || !file.isFile()){
return;
}
InputStream inputStream = new FileInputStream(fileUploadProperties.getLocalStorage()+fileName);
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
ServletOutputStream outputStream = response.getOutputStream();
byte[] b = new byte[1024];
int len;
try {
while((len = inputStream.read(b)) > 0) {
outputStream.write(b, 0, len);
}
} finally {
inputStream.close();
}
}
/**
* 上传单文件
* @param inputFile
* @param isNewName
* @return
* @throws Exception
*/
public String uploadOneMultipleFile(MultipartFile inputFile,boolean isNewName) throws Exception{
String realName = inputFile.getOriginalFilename();
if(isNewName){
String ext = realName.substring(realName.lastIndexOf("."));
realName = UUID.randomUUID().toString() + ext;
}
File file = makeDirsAndCreateFileName(fileUploadProperties.getLocalStorage(),realName);
FileUtils.writeByteArrayToFile(file, inputFile.getBytes(), false);
//inputFile.transferTo(file);
return "/" + realName;
}
/**
* 创建文件夹和文件
* @param path
* @param fileName
* @return
*/
private static File makeDirsAndCreateFileName(String path,String fileName){
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
file = new File(path,fileName);
return file;
}
}