图片的上传

Posted 西瓜的小弟西西瓜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片的上传相关的知识,希望对你有一定的参考价值。

一:需要的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 {

    }
}

 

以上是关于图片的上传的主要内容,如果未能解决你的问题,请参考以下文章

使用 AFNetworking、ios 上传图片

Android - 应用程序启动时片段 onCreate 崩溃

手机safari图片上传竖变横处理

java Ftp上传创建多层文件的代码片段

根据图片的url地址下载图片到本地保存代码片段

如何将CKeditor编辑器的上传和thinkphp结合