您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Java中文件上傳的方式有哪些”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Java中文件上傳的方式有哪些”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
//上傳單個(gè)文件 @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)) { //上傳多個(gè)文件,一個(gè)字段中包含多個(gè)文件 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)) { //上傳多個(gè)文件,每個(gè)字段一個(gè)文件 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 { //如果請(qǐng)求中包含文件 if(ServletFileUpload.isMultipartContent(request)){ // 創(chuàng)建一個(gè)通用的多部分解析器 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); //將文件保存到本地并記錄信息 //... } } }
還有一種方法,函數(shù)入?yún)⒅苯咏邮芪募?/strong>
例如:
//單文件 public String UploadFiles(@RequestParam("file") MultipartFile file){ } //多文件 public String UploadFiles(@RequestParam("file") List<MultipartFile> files){ }
讀到這里,這篇“Java中文件上傳的方式有哪些”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。