码迷,mamicode.com
首页 > Web开发 > 详细

图片的上传

时间:2017-04-12 14:58:01      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:exception   set   extends   tco   tin   post   write   图片   url   

一:需要的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=‘/FileUploadServlet‘/>" method="post" enctype="multipart/form-data">
    用户名;<input type="text" name="username"/><br/>
    照 片:<input type="file" name="zhaoPian"/><br/>
    <input type="submit" value="上传"/>
</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 {

    }
}

 

图片的上传

标签:exception   set   extends   tco   tin   post   write   图片   url   

原文地址:http://www.cnblogs.com/yuwenhui/p/6699143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!