標籤:ade cto text input check config 上傳 上傳檔案 note
這裡我們介紹檔案上傳的知識,使用的是apache的Commons FileUpload架構。
檔案上傳的使用
項目的部分結構如下:
一、使用Commons FileUpload的上傳功能,我們需要引入兩個jar包:commons-fileupload和commons-io。首先我們列出html的部分
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>huhx1.html</title></head><body> <form method="POST" enctype="multipart/form-data" action="FileUploadServlet"> File to upload1: <input type="file" name="upfile1"><br/> File to upload2: <input type="file" name="upfile2"><br/> Notes about the file: <input type="text" name="note"><br/> <br/> <input type="submit" value="Press"> to upload the file! </form></body></html>
二、 FileUploadServlet是一個servlet,內容如下:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletContext servletContext = this.getServletConfig().getServletContext(); File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir"); factory.setRepository(repository); ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> items = upload.parseRequest(request); Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); System.out.println("name: " + name + ", value: " + value); } else { String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); File uploadedFile = new File(request.getServletContext().getRealPath("file"), fileName); System.out.println(uploadedFile.getAbsolutePath()); item.write(uploadedFile); } } } catch (Exception e) { e.printStackTrace(); }}
這裡需要在部署項目的目錄下面建立file檔案夾,如。request.getServletContext().getRealPath("file")代碼
spring mvc上傳檔案
一、在設定檔中配置上傳的資訊
<!-- 支援上傳檔案 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8" /> <property name="maxUploadSize" value="5000000" /></bean>
CommonsMultipartResolver其實就是對上述的common-fileupload的一種封裝。
二、在java類中: 多檔案上傳需要@RequestParam(value="files"),files表示定義file的name。對於formData,那就是檔案的key。
public void fileUplaod(HttpServletRequest request, @RequestParam(value="files") MultipartFile[] files, HttpServletResponse response) { System.out.println(files.length); ResponseResult result = new ResponseResult(); for (MultipartFile file : files) {try { FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, file.getOriginalFilename())); } catch (IOException e1) { e1.printStackTrace(); } } Map<String, String> map = new HashMap<>(); result.setReturnCode(Constants.SUCCESS_RETURN_CODE); result.setData(map); ResponseUtils.returnResponseJsonData(response, result);}
三、為防止檔案重複提交,需要比較檔案的資訊
String newFileContent = file.getOriginalFilename() + file.getSize();String newEncryFile = EncryptUtils.sha1Encrypt(newFileContent);// 在實際使用中,request.getSession().getAttribute("fileCheck")是從資料庫中得到的if (newEncryFile.equals(request.getSession().getAttribute("fileCheck"))) { result.setReturnCode(Constants.FAIL_RETURN_CODE); result.setErrorMsg(ConstantsMsg.FILE_UPLOAD_REPEAT); ResponseUtils.returnResponseJsonData(response, result); return;}// ....上傳完成之後,encryFile會存在資料庫當中String fileContent = file.getOriginalFilename() + file.getSize();String encryFile = EncryptUtils.sha1Encrypt(fileContent);request.getSession().setAttribute("fileCheck", encryFile);
FileUpload的原理
後續補充
友情連結
- fileupload的官方文檔:http://commons.apache.org/proper/commons-fileupload/using.html
web基礎---->Fileupload檔案的上傳