Home > Java > javaTutorial > body text

Code example of using FileUpload to complete upload in Java

黄舟
Release: 2017-09-25 10:36:15
Original
2029 people have browsed it

This article mainly introduces the relevant information about the instance of FileUpload in Java Advanced. I hope this article can help everyone. Friends in need can refer to

Java Advanced FileUpload Example of completing the upload

FileUpload is a sub-project under Apache commons, used to implement the file upload function under Java projects. Common file uploads include SmartUpload, Servlet3.0, and Struts2.

Here I use commons-fileupload-1.2.1. The following is a simple example. The parsing process is written in the comments in the code. The comments are very detailed


//创建磁盘文件项工厂 
      DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory(); 
      //设置缓冲区的大小,如果文件的大小超过了缓冲区的大小,就会产生临时文件 
      diskFileItemFactory.setSizeThreshold(1024*1024*3); 
      //获得核心解析类 
      ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory); 
      //解决中文文件名上传乱码 
      fileUpload.setHeaderEncoding("UTF-8"); 
      //解析request,返回list集合 
      List<FileItem> list = fileUpload.parseRequest(request); 
      //获得每个部分,将遍历的值存入到一个map集合中 
      Map<String, String> map=new HashMap<>(); 
      //设置上传文件的文件名 
      String fileName=null; 
      for (FileItem fileItem : list) { 
        //判断普通项和文件上传项 
        if (fileItem.isFormField()) { 
          //普通项 
          String name=fileItem.getFieldName(); 
          String value=fileItem.getString("UTF-8"); 
          map.put(name, value); 
        }else { 
          //文件上传项 
          //获得文件名 
          fileName=fileItem.getName(); 
          //获得文件的输入流 
          InputStream is=fileItem.getInputStream(); 
          //获得文件上传的路径 
          String path=this.getServletContext().getRealPath("/products/1"); 
          OutputStream os=new FileOutputStream(path+"/"+fileName); 
          int len=0; 
          byte[] b=new byte[1024]; 
           
          while ((len=is.read(b))!=-1) { 
            os.write(b, 0, len); 
          } 
          is.close(); 
          os.close(); 
        }
Copy after login

The component FileUpload uses the FileItemFactory factory to create a new file item. This gives the component FileUpload a lot of flexibility. This factory has final control over how projects are created. Temporary data for uploaded project files during factory execution can be stored in memory or on the hard disk. This depends on the size of the uploaded item (ie: bytes of data). However, this behavior can be customized appropriately in your application.

The above is the detailed content of Code example of using FileUpload to complete upload in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template