首页 > 代码库 > 图片的上传

图片的上传

一:需要的jar包

  commons—io.jar

  commons—fileupload.jar这个包对io有依赖

二:上传三部曲

  相关类:

      工厂:DiskFileItemFactory

      解析器:ServletFileUpload

      表单项:FileItem

  1)创建工厂    

DiskFileItemFactory factory = new DiskFileItemFactory ();

  2)创建解析器 

ServletFileUpload  servletFileUpload = new ServletFileUpload(factory);

  3)使用解析器来解析request,得到FileItem集合

List<FileItem> fileItem = servletFileUpload.parseRequest(request);

三:源码

  
  1.jsp

<%--
  Created by IntelliJ IDEA.
  User: YuWenHui
  Date: 2017/4/12 0012
  Time: 14:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>上传图片</title>
</head>
<body>
<h1>上传1</h1>
<form action="<c:url value=http://www.mamicode.com/‘/FileUploadServlet‘/>" method="post" enctype="multipart/form-data">
    用户名;<input type="text" name="username"/><br/>
    照 片:<input type="file" name="zhaoPian"/><br/>
    <input type="submit" value="http://www.mamicode.com/上传"/>
</form>
</body>
</html>

  2:配置文件

servlet>
        <servlet-name>FileUploadServlet</servlet-name>
        <servlet-class>servlet.FileUploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileUploadServlet</servlet-name>
        <url-pattern>/FileUploadServlet</url-pattern>
    </servlet-mapping>

 

  3:servlet

package servlet;

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 java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by YuWenHui on 2017/4/12 0012.
 */
public class FileUploadServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
//        得到工厂
        DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
//        创建解析器
        ServletFileUpload servletFileUpload =new ServletFileUpload(diskFileItemFactory);
//        得到FIleItem集合
        try {
            List<FileItem> fileItemList = servletFileUpload.parseRequest(request);
            FileItem fileItem1 = fileItemList.get(0);
            FileItem fileItem2 = fileItemList.get(1);
//            演示普通表单项
            System.out.println("普通表单项:"+fileItem1.getFieldName()+":"+fileItem1.getString("utf-8"));
//            演示文件表单项
            System.out.println("文件表单项目演示:");
            System.out.println("ContentType:"+fileItem2.getContentType());
            System.out.println("名称:"+fileItem2.getName());
            System.out.println("size:"+fileItem2.getSize());
//            创建一个文件,这里的文件目录就是上传文件的真实路径
            File distFile = new File("E:/JavaWebSrc/FileUpload/destFile.jpg");
//            保存文件
            fileItem2.write(distFile);
        } catch (FileUploadException e) {
           throw  new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }
}

 

图片的上传