fileupload控件的简单使用

fileupload.jsp

<%--
  Created by IntelliJ IDEA.
  User: Limbo
  Date: 2016/9/27
  Time: 23:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    File Upload


    
File: Describe:

FileUpload

package cn.limbo.fileUpload;

        import com.sun.tools.javac.jvm.Items;
        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;

        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import java.io.*;
        import java.util.Iterator;
        import java.util.List;

/**
 * Created by Limbo on 2016/9/27.
 */
public class FileUpload extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //创建一个DiskFileItemFactory用于获取form对象
        DiskFileItemFactory factory = new DiskFileItemFactory();

        // 添加限制
        /**
         * setSizeThreshold方法用于设置是否将上传文件已临时文件的形式保存在磁盘的临界值
         *(以字节为单位的int值),如果从没有调用该方法设置此临界值,将会采用系统默认值10KB。
         * 对应的getSizeThreshold() 方法用来获取此临界值。
         */
        factory.setSizeThreshold(1024*1024*10);    



        File file = new File("/User/Limbo/Document");
        factory.setRepository(file);//设置文件存储的位置

        //创建一个上传的处理器
        ServletFileUpload upload = new ServletFileUpload(factory);

        //设置所有上传文件的总大小
        upload.setSizeMax(1024*1024*1024);

        //将request中传经来的表单转化成一个列表
        List items = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        Iterator iterator = items.iterator();
        while(iterator.hasNext())
        {
            FileItem item = iterator.next();
            if(item.isFormField())
            {
                System.out.println(item.getFieldName() + " : " + item.getString());
            }
            else
            {
                //文件的基本信息
                String fileName = item.getName();
                System.out.println(item.getName());
                System.out.println(item.getContentType());
                System.out.println(item.isInMemory());
                System.out.println(item.getSize());

                //文件流的形式读入信息
                InputStream in = item.getInputStream();
                byte [] buffer = new byte[1024];
                int len = 0;

                //上传的文件的路径
                fileName = "/Users/Limbo/Documents/" + fileName;

                //输出流的形式输出
                OutputStream out = new FileOutputStream(fileName);
                while((len = in.read(buffer)) != -1)
                {
                    out.write(buffer,0,len);
                }
                out.close();
                in.close();
            }
        }
    }
}




不懂不一定要直接百度,有时候直接看文档更好呢!

你可能感兴趣的:(JavaWeb)