标签: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")代码
一、在配置文件中配置上传的信息
<!-- 支持上传文件 --> <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);
后续补充
标签:ade cto text input check config 上传 上传文件 note
原文地址:http://www.cnblogs.com/huhx/p/baseusewebfileupload.html