添加万恶的依赖
<!-- 附件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
修改配置文件,屏蔽servlet自带的附件上传
#配置上传文件信息
http:
multipart:
# 最大支持文件大小 即单个文件大小
max-file-size: 2m
# 最大支持请求大小 即一次性上传的总文件大小
max-request-size: 10m
#取消Servlet自带的上传
enabled: false
增加控制器
package Net.XiangCaoWuYu.Controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* ClassName: CommonsFileUpload <br/>
* Description: <br/>
* date: 2019/7/28 9:28<br/>
*
* @author lisen01<br />
* @since JDK 1.8
*/
@RestController
@ResponseBody
@RequestMapping(value = "/commonsfileupload")
public class CommonsFileUpload {
private static final Logger logger = LoggerFactory.getLogger(CommonsFileUpload.class);
@PostMapping("/upload")
public String Upload(@RequestParam MultipartFile[] files, @RequestParam String name, HttpServletRequest request) throws IOException {
if (files.length <= 0) {
return "请先选择要上传的附件";
}
for (MultipartFile file : files) {
if (file.isEmpty()) {
return "文件信息不能为空";
}
String fileType = file.getContentType();
String fileName = file.getName();
String fileOriginalName = file.getOriginalFilename();
//String fileSavePath = request.getServletContext().getRealPath("/static/sources");
//使用jar包,放到同级目录
File rootPath = new File(ResourceUtils.getURL("classpath:").getPath());
String fileSavePath = rootPath.getAbsolutePath()+"/static/upload/";
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
fileSavePath = fileSavePath + simpleDateFormat.format(date);
fileSavePath = fileSavePath + "/" + fileOriginalName;
fileSavePath.replace("/", File.separator);
File saveFile = new File(fileSavePath);
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
file.transferTo(saveFile);
}
return "上传成功";
}
}
哈哈
WordPress标题自动翻译英文插件LS_baidu_translator_木子网