文件上传

 

 FileUpload简介

  *API:

    ** DiskFileItemFactory :磁盘文件项工厂

    ** ServletFileUpload: 核心解析类

    ** FileItem : 文件项

 

文件上传代码的实现

    

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/FileUpload" method="post" enctype="multipart/form-data">
        <input type="text" name="filename">
        <input type="file" name="upload">
        <input type="submit" value="上传">
    </form>
</body>
</html>
package com.imooc.upload;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;

/**
 * Servlet implementation class FileUpload
 */
@WebServlet("/FileUpload")
public class FileUpload extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建文件项工厂
        DiskFileItemFactory fileFactory = new DiskFileItemFactory();
        
        
        //2、创建解析类
        ServletFileUpload fileParse = new ServletFileUpload(fileFactory);
        //3、解析请求,成多个文件项FileItem
        try {
            List<FileItem> fileItem = fileParse.parseRequest(request);
            
            for(FileItem item : fileItem) {
                //判断是不是普通文件
                if(item.isFormField()) {
                    //普通文件
                    String name = item.getFieldName();//普通文件名
                    
                    String value = item.getString("UTF-8"); //普通文件值
                    
                    System.out.println(name+" "+value);
                }else {
                    //文件上传项
                    String fileName = item.getName();//获取文件名
                    //获取文件流
                    InputStream content = item.getInputStream();
                    
                    //获取文件的路径
                    String path = getServletContext().getRealPath("/upload");
                    
                    System.out.println(path);
                    
                    //创建输出流与输入流对接
                    OutputStream writeContent = new FileOutputStream(path+"\\"+fileName); //将文件写入路径
                    
                    int len=0;
                    byte[] b = new byte[1024];
                    
                    while((len=content.read(b))!=-1) {
                        writeContent.write(b, 0, len);
                    }
                    content.read();
                    writeContent.close();
                }
            }
            
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

 

posted @ 2020-09-29 12:49  python成长中  阅读(86)  评论(0编辑  收藏  举报