文件的上传:二进制文件的上传;

***二进制文件上传的方法:

***在lib目录下:导入文件上传的开源架包:commons-fileupload-1.2.1.jar,commons-io-2.0.jar;

 

建立Servlet类:UploadServlet,实现文件上传的方法package com.lanqiao.javaweb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        // 1. 得到 FileItem 的集合 items
        // Create a factory for disk-based file items
        DiskFileItemFactory factory = new DiskFileItemFactory();
        
//        FileCleaningTracker fileCleaningTracker = 
//        FileCleanerCleanup.getFileCleaningTracker(getServletContext());
//        factory.setFileCleaningTracker(fileCleaningTracker);
                
        // Set factory constraints,设置文件的大小
        factory.setSizeThreshold(1024 * 500);
                
        //超过该大小,就读到临时的文件里面
        File tempDirectory = new File("f:\\tempDirectory");
        factory.setRepository(tempDirectory);

        // Create a new file upload handler;上传该文件
        ServletFileUpload upload = new ServletFileUpload(factory);
        
        // Set overall request size constraint;设置文件总的大小
        upload.setSizeMax(1024 * 1024 * 5);
        
        // Parse the request
        try {
            //获取jsp文件里的请求,在集合中
            List /* FileItem */items = upload.parseRequest(request);

            // 2. 遍历 items:
            for (FileItem item : items) {
                // 若是一个一般的表单域, 打印信息
                if (item.isFormField()) {
                    String name = item.getFieldName();
                    String value = item.getString();

                    System.out.println(name + ": " + value);
                            
                            
                }
                // 若是文件域则把文件保存到 d:\\files 目录下.
                else {
                    //文件名为file
                    String fieldName = item.getFieldName();
                        
                    //上传的原始的文件名
                    String fileName = item.getName();
                        
       

              //截取路径名,因为想要获取的是文件名而不是文件的路径
              fileName=fileName.substring(3);

//上传的文件的类型
                    String contentType = item.getContentType();
                            
                    //上传的文件的大小
                    long sizeInBytes = item.getSize();
                    System.out.println(fieldName);
                    System.out.println(fileName);
                    System.out.println(contentType);
                    System.out.println(sizeInBytes);

                    InputStream in = item.getInputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                            
                    //文件上传后的 路径和文件名
                    fileName = "f:\\files\\" + fileName;
                    System.out.println(fileName);

                    OutputStream out = new FileOutputStream(fileName);
                            
                    while ((len = in.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                    }

                    out.close();
                    in.close();
                }
            }

        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        
    }

}

 

web.xml文件配置和映射:



  day-14-fileupload
UploadServlet UploadServlet class>com.lanqiao.javaweb.UploadServletclass>
UploadServlet /uploadServlet

 

upload.jsp表单:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


    
    
        
          File:  
        

Desc:  

 

转载于:https://www.cnblogs.com/lxnlxn/p/5838734.html

你可能感兴趣的:(文件的上传:二进制文件的上传;)