首页 > 代码库 > commons.fileupload简单应用

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);    }}

 

commons.fileupload简单应用