本文小编为大家详细介绍“Java中文件上传的方式有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java中文件上传的方式有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
请求类型
单文件上传
//上传单个文件 @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){}
读到这里,这篇“Java中文件上传的方式有哪些”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网行业资讯频道。