Servlet实现文件上传

一、Servlet实现文件上传,需要添加第三方提供的jar包

下载地址:

1) commons-fileupload-1.2.2-bin.zip      :   点击打开链接

2) commons-io-2.3-bin.zip                       :    点击打开链接    

二、文件上传的表单提交方式必须是POST方式,

编码类型:enctype="multipart/form-data",默认是 application/x-www-form-urlencoded

比如:<form action="FileUpLoad"enctype="multipart/form-data"method="post">

三、举例:

servlet 

package com.test.servlet;

import java.io.*;
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;
import org.apache.commons.io.FilenameUtils;

/**
 * 
 * @author Administrator 文件上传 具体步骤: 1)获得磁盘文件条目工厂 DiskFileItemFactory 要导包 2) 利用
 *         request 获取 真实路径 ,供临时文件存储,和 最终文件存储 ,这两个存储位置可不同,也可相同 3)对
 *         DiskFileItemFactory 对象设置一些 属性 4)高水平的API文件上传处理 ServletFileUpload
 *         upload = new ServletFileUpload(factory); 目的是调用
 *         parseRequest(request)方法 获得 FileItem 集合list ,
 * 
 *         5)在 FileItem 对象中 获取信息, 遍历, 判断 表单提交过来的信息 是否是 普通文本信息 另做处理 6) 第一种. 用第三方
 *         提供的 item.write( new File(path,filename) ); 直接写到磁盘上 第二种. 手动处理
 * 
 */
public class FileUpLoad extends HttpServlet {
    // 获取文件需要上传到的路径
    String path = "E:/Clusters/static/sai_zd/upload/imagePrease";

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

        request.setCharacterEncoding("utf-8"); // 设置编码

        // 获得磁盘文件条目工厂
        DiskFileItemFactory factory = createDiskFileItemFactory(request, path);

        // 高水平的API文件上传处理
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            // 可以上传多个文件
            List<FileItem> list = (List<FileItem>) upload.parseRequest(request);

            for (FileItem item : list) {
                // 获取表单的属性名字
                String name = item.getFieldName();

                // 如果获取的 表单信息是普通的 文本 信息
                if (item.isFormField()) {
                    // 获取用户具体输入的字符串 ,名字起得挺好,因为表单提交过来的是 字符串类型的
                    String value = item.getString();

                    request.setAttribute(name, value);
                }
                // 对传入的非 简单的字符串进行处理 ,比如说二进制的 图片,电影这些
                else {
                    /**
                     * 以下三步,主要获取 上传文件的名字
                     */
                    // 获取路径名
                    String value = item.getName();

                    // 截取 上传文件的 字符串名字,加1是 去掉反斜杠,
                    String filename = FilenameUtils.getName(value);
                    if (filename == null | filename.equals(""))
                        continue;
                    // 目标文件
                    File targetFile = new File(path, filename);

                    // 真正写到磁盘上
                    writeDisk(item, targetFile);

                    request.setAttribute(name, filename);
                }
            }

        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        request.getRequestDispatcher("MyJsp.jsp").forward(request, response);

    }

    /**
     * 写入磁盘
     * 
     * @param item
     * @param filename
     * @throws FileNotFoundException
     * @throws IOException
     * 
     * @author mjorcen
     * @email mjorcen@gmail.com
     * @dateTime Jun 24, 2014 12:04:37 PM
     * @version 1
     */
    private void writeDisk(FileItem item, File file)
            throws FileNotFoundException, IOException {
        // 它抛出的异常 用exception 捕捉

        // item.write( new File(path,filename) );//第三方提供的

        // 手动写的
        OutputStream out = new FileOutputStream(file);

        InputStream in = item.getInputStream();

        int length = 0;
        byte[] buf = new byte[1024];

        System.out.println("获取上传文件的总共的容量:" + item.getSize());

        // in.read(buf) 每次读到的数据存放在 buf 数组中
        while ((length = in.read(buf)) != -1) {
            // 在 buf 数组中 取出数据 写到 (输出流)磁盘上
            out.write(buf, 0, length);

        }

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

    /**
     * 
     * @param request
     * @return
     * 
     * @author mjorcen
     * @email mjorcen@gmail.com
     * @dateTime Jun 24, 2014 12:01:45 PM
     * @version 1
     * @param path
     */
    private DiskFileItemFactory createDiskFileItemFactory(
            HttpServletRequest request, String path) {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 如果没以下两行设置的话,上传大的 文件 会占用 很多内存,
        // 设置暂时存放的 存储室 , 这个存储室,可以和 最终存储文件 的目录不同
        /**
         * 原理 它是先存到 暂时存储室,然后在真正写到 对应目录的硬盘上, 按理来说 当上传一个文件时,其实是上传了两份,第一个是以 .tem
         * 格式的 然后再将其真正写到 对应目录的硬盘上
         */
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        factory.setRepository(dir);
        // 设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
        factory.setSizeThreshold(1024 * 1024);
        return factory;
    }

}

form jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'fileupload.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
    <!-- enctype 默认是 application/x-www-form-urlencoded -->
    <form action="fileUpload" enctype="multipart/form-data" method="post">

        用户名:<input type="text" name="usename"> <br /> 上传文件:<input
            type="file" name="file1"><br /> 上传文件: <input type="file"
            name="file2"><br /> <input type="submit" value="提交" />

    </form>



</body>
</html>
My JSP 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'filedemo.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>

    用户名:${requestScope.usename }
    <br /> 文件:${requestScope.file1 }
    <br /> ${requestScope.file2 }
    <br />
    <!-- 把上传的图片显示出来 -->
    <img alt="go" src="upload/<%=(String) request.getAttribute("file1")%> " />



</body>
</html>
原文地址:https://www.cnblogs.com/mjorcen/p/3806076.html