主要解决spring boot 文件上传保存与访问问题
1.上传文件保存在指定静态目录。
2.刚上传的文件就可以访问。
3.方便迁移
误区与坑:
1.上传文件自动到静态目录
坑:
2.文件不好迁移,tomcat默认路径是这样的:C:\Users\boom\AppData\Local\Temp\tomcat-docbase.3407010661962198032.9006
3.每次重启目录都不对
环境:
spring boot
前端 vue iview
开始代码
1.vue前端代码 ,其他的html代码类似
...
Click or drag files here to upload
...
2.java代码目录
3.UploadFileConfig 代码,主要做下载拦截
package com.avie.ltd.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
/**
* Created by boom on 2018/11/26.
*/
@Configuration
public class UploadFileConfig {
@Value("${file.uploadFolder}")
private String uploadFolder;
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setLocation(uploadFolder);
//文件最大
factory.setMaxFileSize("5MB");
// 设置总上传数据总大小
factory.setMaxRequestSize("10MB");
return factory.createMultipartConfig();
}
}
3.UploadFileConfig 代码,主要做文件访问(也可以用配置文件设置替代本类)
package com.avie.ltd.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* Created by boom on 2018/11/26.
*/
@Configuration
public class UploadFilePathConfig extends WebMvcConfigurerAdapter {
@Value("${file.staticAccessPath}")
private String staticAccessPath;
@Value("${file.uploadFolder}")
private String uploadFolder;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder);
}
}
4.application.properties (可以在上面的代码中写死,自己取舍)
#静态资源对外暴露的访问路径
file.staticAccessPath=/**
#文件上传目录(注意Linux和Windows上的目录结构不同)
#file.uploadFolder=/root/uploadFiles/
file.uploadFolder=g://uploadFiles/
5.maven 配置
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.3.1
6.文件上传类:FileUpLoad
package com.avie.ltd.controller;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
/**
* Created by boom on 2018/11/26.
*/
@RestController
@RequestMapping(value="file")
public class FileUpLoad {
@ApiOperation("上传图片,多文件")
@PostMapping("/upload")
@ResponseBody
public Object upload(HttpServletRequest request1,MultipartHttpServletRequest request) throws IOException {
String ctxPath = request1.getSession().getServletContext()
.getRealPath("/");
System.out.println(ctxPath);
List files = request.getFiles("file");
if (files.size() > 0) {
for (MultipartFile multipartFile : files) {
System.out.println(multipartFile);
handleFileUpload(multipartFile,request);
}
}
return "";
}
/**
* 单一文件
* @param file
* @param request
* @return
*/
@RequestMapping("/upload2")
@ResponseBody
public String handleFileUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
if (!file.isEmpty()) {
String saveFileName = file.getOriginalFilename();
File saveFile = new File(request.getSession().getServletContext().getRealPath("/") + saveFileName);
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
out.write(file.getBytes());
out.flush();
out.close();
return saveFile.getName();
} catch (FileNotFoundException e) {
e.printStackTrace();
return "上传失败,";
} catch (IOException e) {
e.printStackTrace();
return "上传失败,";
}
} else {
return "上传失败,因为文件为空.";
}
}
}