您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“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中文件上傳的方式有哪些”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。