Java中的多种文件上传方式总结
短信预约 -IT技能 免费直播动态提醒
请求类型
单文件上传
//上传单个文件
@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 "";
}
如果上面方法不行,看下面
@PostMapping(value = "test", produces = "multipart/form-data;charset=UTF-8")
public void UploadFiles(HttpServletRequest request) throws IOException {
//如果请求中包含文件
if(ServletFileUpload.isMultipartContent(request)){
// 创建一个通用的多部分解析器
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = multipartRequest.getFiles("file");
File targetFile;
for (MultipartFile file : files){
//保存文件到本地
String projectPath = request.getSession().getServletContext().getRealPath("/");
projectPath = projectPath + System.currentTimeMillis() + "_" + new Random().nextInt(100);
File dir = new File(projectPath);
if (!dir.exists()){
dir.mkdir();
}
String originalFilename = file.getOriginalFilename();
String tmpFile = projectPath + File.separator + originalFilename;
targetFile = new File(tmpFile);
file.transferTo(targetFile);
//将文件保存到本地并记录信息
//...
}
}
}
还有一种方法,函数入参直接接受文件
例如:
//单文件
public String UploadFiles(@RequestParam("file") MultipartFile file){
}
//多文件
public String UploadFiles(@RequestParam("file") List<MultipartFile> files){
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341