Java多种文件上传方式总结
程序员文章站
2024-02-19 11:31:46
...
请求类型
单文件上传
//上传单个文件
@ResponseBody
@RequestMapping(value = "uploadfile", produces = "multipart/form-data;charset=UTF-8")
public String uploadfiles(HttpServletRequest request){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile("file");
if (multipartFile == null) {
System.out.println("multipartFile == null");
}
String filename = multipartRequest.getFile("file").getOriginalFilename();
String tmpFile = request.getSession().getServletContext().getRealPath("/") + UUID.randomUUID().toString().replace("-", "") + ".apk";
File targetFile = new File(tmpFile);
AppEntity appEntity = new AppEntity();
if (null != multipartFile) {
//保存文件
try {
multipartFile.transferTo(targetFile);
} catch (IllegalStateException e) {
logger.error("multipartFile.transferTo, IllegalStateException", e);
} catch (IOException e) {
logger.error("multipartFile.transferTo, IOException", e);
}
}
}
return "";
}
多文件上传
多文件单字段上传
@ResponseBody
@RequestMapping(value = "uploadfiles", produces = "multipart/form-data;charset=UTF-8")
public String uploadfile(MultipartHttpServletRequest request){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
//上传多个文件,一个字段中包含多个文件
String projectPath = request.getSession().getServletContext().getRealPath("/");
MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
List<MultipartFile> fileSet = new LinkedList<>();
for(Map.Entry<String, List<MultipartFile>> temp : multiFileMap.entrySet()){
fileSet = temp.getValue();
}
for(MultipartFile temp : fileSet){
File file2 = new File(projectPath+temp.getOriginalFilename());
try {
temp.transferTo(file2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
多文件多字段上传
@ResponseBody
@RequestMapping(value = "uploadfiles", produces = "multipart/form-data;charset=UTF-8")
public String uploadfile(MultipartHttpServletRequest request){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
//上传多个文件,每个字段一个文件
Iterator<String> fileNames = request.getFileNames();
while (fileNames.hasNext()){
// 取得上传文件
String uploadName = fileNames.next();
MultipartFile file = request.getFile(uploadName);
if (file != null) {
String projectPath = request.getSession().getServletContext().getRealPath("/");
String originalFilename = file.getOriginalFilename();
String tmpFile = projectPath + System.currentTimeMillis() + "_" + originalFilename;
File targetFile = new File(tmpFile);
try {
file.transferTo(targetFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "";
}
over!
上一篇: Java在页面实现文件上传具体代码
下一篇: Springboot 添加图片