使用Servlet做简单的上传功能

这篇文章主要为大家详细介绍了java基于servlet实现上传功能,后台使用java实现,前端主要是jsp,感兴趣的小伙伴们可以参考一下

一、准备工作

1.1 jar包

servlet-api 是servlet的核心api包 commons-fileupload 上传功能的通用工具包 fastjson 阿里的json工具包

1.2 pom文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.7</version>
        </dependency>

二、项目介绍

1.1 web.xml配置

此项目是采用maven构建的java web项目,传统的jsp作为页面交互。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<web-app>
	<listener>
		<listener-class>
			org.apache.commons.fileupload.servlet.FileCleanerCleanup
		</listener-class>
	</listener>
	<servlet>
		<servlet-name>FileUploadServlet</servlet-name>
		<display-name>FileUploadServlet</display-name>
		<description>FileUploadServlet</description>
		<servlet-class>com.ghj.servlet.FileUploadServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>FileUploadServlet</servlet-name>
		<url-pattern>/FileUploadServlet</url-pattern>
	</servlet-mapping>
</web-app>

配置的servlet的位置,那么我们就要去写代码了。

1.2 上传功能

新建一个class文件com.ghj.servlet.FileUploadServlet,继承自HttpServlet。重写它的init()doPost()方法。

代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.ghj.servlet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileCleaningTracker;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 把今天最好的表现当作明天最新的起点..~
 * いま 最高の表現 として 明日最新の始発..~
 * Today the best performance  as tomorrow newest starter!
 * Created by IntelliJ IDEA.
 *
 * @author: xiaomo
 * @github: https://github.com/soubaku
 * @email: hupengbest@163.com
 * @QQ_NO: 83387856
 * @Date: 2016/10/24 9:15.
 * @Description:
 * @Copyright(©) 2016 by xiaomo.
 **/
public class FileUploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private File fileUploadPath;
    private static final String fileDirectory = "/data";

    /**
     * 设置文件存储位置
     */
    @Override
    public void init() {
        String realPath = this.getServletConfig().getServletContext().getRealPath("/");
        fileUploadPath = new File(realPath + fileDirectory);
        if (!fileUploadPath.exists()) {
            boolean isCreate = fileUploadPath.mkdirs();
            if (isCreate) {
                System.out.println("文件夹己被创建");
            }
        }
    }


    /**
     * post请求(文件上传的操作)
     * 说明:该方法支持同时上传多个文件(在index.jsp中添加文件上传的控件)
     * 注意:文件名只能为英文
     *
     * @param request  request
     * @param response response
     * @throws ServletException ServletException
     * @throws IOException      IOException
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            ServletContext servletContext = this.getServletConfig().getServletContext();
            File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
            DiskFileItemFactory factory = newDiskFileItemFactory(servletContext, repository);

            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("utf-8");

            PrintWriter printWriter = response.getWriter();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            JSONArray files = new JSONArray();
            try {
                List<FileItem> items = upload.parseRequest(request);
                for (FileItem item : items) {
                    if (item.isFormField()) { //表单输入域
                        System.out.println(item.getFieldName());
                    } else { // 文件上传域
                        String name = item.getName();
                        if (!name.equals("")) {
                            int index = name.indexOf("\\");
                            File uploadedFile;
                            if (index == -1) {
                                uploadedFile = new File(fileUploadPath,
                                        File.separator + name);
                            } else {
                                uploadedFile = new File(fileUploadPath, File.separator + name.substring(name.lastIndexOf(File.separator) + 1));
                            }
                            item.write(uploadedFile);
                            JSONObject file = new JSONObject();
                            file.put("name", name);
                            file.put("size", item.getSize());
                            file.put("url", fileUploadPath + "/" + name);
                            file.put("deleteType", "post");
                            files.add(file);
                        } else {
                            JSONObject file = new JSONObject();
                            file.put("result", "please choose a file");
                            files.add(file);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JSONObject result = new JSONObject();
                result.put("files", files);
                printWriter.write(result.toString());
                printWriter.close();
            }
        }
    }

    /**
     * 将请求的每个项目封装成单独的文件项目
     *
     * @param context    context
     * @param repository repository
     * @return DiskFileItemFactory
     */
    private DiskFileItemFactory newDiskFileItemFactory(ServletContext context, File repository) {
        FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(context);
        DiskFileItemFactory factory = new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository);
        factory.setFileCleaningTracker(fileCleaningTracker);
        return factory;
    }

}

2.3 页面

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<html>
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<body>
<h2>文件上传</h2>
<form action="FileUploadServlet" method="post" enctype="multipart/form-data">
	<input type="file" name="file1""><br>
	<input type="submit" value="submit">
</form>
</body>
</html>

看起来清新简单,就不加那么style的修饰了。

三、效果图

效果图

四、源代码

https://github.com/java-user/ServletFileUploadDemo 需要的朋友自取,随意使用不用客气

署名 - 非商业性使用 - 禁止演绎 4.0