zoukankan      html  css  js  c++  java
  • 文件上传

     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();
            }
    
        }
    
    }
  • 相关阅读:
    如何在局域网安装Redmine(转贴)
    介绍开源的项目管理系统-Redmine
    【转】ios开发证书,描述文件,bundle ID的关系
    【转】Spring+Mybatis+SpringMVC+Maven+MySql搭建实例
    【转】 Tomcat+redis+nginx配置
    【转】Java做服务器开发语言
    【转】程序员的五大陷阱
    一个简单的网页服务器
    【转】Eclipse中创建并运行Servlet项目
    【转】在Eclipse环境下配置Servlet开发环境
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/13749075.html
Copyright © 2011-2022 走看看