欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javaWEB文件上传

程序员文章站 2022-05-08 17:02:25
...

Servlet

package top.biglin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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.IOUtils;

public class FileuploadServlet2 extends HttpServlet {

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

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
     * , javax.servlet.http.HttpServletResponse)
     */
    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        // 接收上传的文件
        // 1、创建磁盘文件项工厂
        String path2 = this.getServletContext().getRealPath("temp");
        DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 1024,
                new File(path2));
        // 2、创建文件上传的核心类
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");// 解决文件名称乱码
        // 判断是否为文件上传表单
        boolean isMutilpartContent = ServletFileUpload.isMultipartContent(request);
        if (isMutilpartContent) {
            // 分解表单
            List<FileItem> parseRequest = null;
            try {
                parseRequest = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }

            if (parseRequest != null) {
                for (FileItem item : parseRequest) {
                    boolean isFormField = item.isFormField();
                    if (isFormField) {
                        String fieldName = item.getFieldName();
                        String value = item.getString("UTF-8");
                        System.out.println(fieldName + ":" + value);
                    } else {// 表示为文件项
                        String fileName = item.getName();
                        InputStream in = item.getInputStream();
                        String path = this.getServletContext().getRealPath("upload");
                        OutputStream out = new FileOutputStream(path+"/"+fileName);
                        //IOUtils ioUtils = new IOUtils();
                        IOUtils.copy(in, out);
                        in.close();
                        out.close();
                        
                        //删除临时文件
                        item.delete();
                    }
                }
            }
        } else {
            // 使用request.getParameter()获取参数
        }
        // 5、判断普通项
        doGet(request, response);
    }
}


jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
    <div>
        <form  enctype="multipart/form-data" action="/testfileupload/upload2" method="post">
            <p>用户名:<input type="text" name="username" value="" /></p>
            <input type="file" name="fileupload" value="dddd" />
            <p><input type="submit" name="fileUpload"
                value="a上传" /></p>
        </form>
    </div>
</body>
</html>