• commons.fileupload简单应用


    导入包:

    commons-fileupload-1.3.1.jar

    commons-io-2.4.jar

    commons-fileupload依赖于commons-io,commons-io-2.4必须是JDK6+;

    下载地址:

    http://commons.apache.org/proper/commons-fileupload/download_fileupload.cgi

    http://commons.apache.org/proper/commons-io/download_io.cgi


    index.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1>
    
            <form name="myform" action="FileUploadTestServlet" method="post" enctype="multipart/form-data">
                姓名: <br>
                <input type="text" name="name" size="15"><br>
                附件1:<br>
                <input type="file" name="myfile1"><br>
                附件2:<br>
                <input type="file" name="myfile2"><br>
                <br>
                <input type="submit" name="submit" value="Commit">
            </form>
        </body>
    </html>
    FileUploadTestServlet.java
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Iterator;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    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.FileItemFactory;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    
    /**
     *
     * @author y
     */
    public class FileUploadTestServlet extends HttpServlet {
    
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            
            if(!ServletFileUpload.isMultipartContent(request)){
                return;
            }
            
            FileItemFactory fileFactory = new DiskFileItemFactory();
            
            ServletFileUpload fileUpload = new ServletFileUpload(fileFactory);
            
            try {
                List items = fileUpload.parseRequest(request);
                Iterator iter = items.iterator();
                
                while(iter.hasNext()){
                    FileItem item = (FileItem)iter.next();
                    if(item.isFormField()){
                        out.print("<p>"+item.getFieldName()+":"+new String(item.getString().getBytes("ISO8859-1"), "utf-8")+"</p>");
                    }else{
                        String path = "/home/y/my_temp/upload"+File.separator+item.getName();
                        File disk = new File(path);
                        item.write(disk);
                        
                        out.print("<p>文件保存到:"+path+"</p>");
                    }
                }
            } catch (FileUploadException ex) {
                Logger.getLogger(FileUploadTestServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(FileUploadTestServlet.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
                out.close();
            }
            
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
    }
  • 相关阅读:
    牛客多校第九场 && ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解
    2019牛客多校第九场B Quadratic equation(二次剩余定理)题解
    URAL 1132 Square Root(二次剩余定理)题解
    牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解
    配置android.support.v7.widget.Toolbar 搜索框样式
    Google之路
    Editplus 竖选,竖插入技巧
    JNDI
    Spring Hibernate Transaction示例
    AOP 入门
  • 原文地址:https://www.cnblogs.com/yshyee/p/4141099.html
Copyright © 2020-2023  润新知