jar包:html
commons-fileupload-1.3.1.jarjava
commons-io-1.3.2.jarweb
maven庫sql
<dependency>apache
<groupId>commons-fileupload</groupId>app
<artifactId>commons-fileupload</artifactId>jsp
<version>1.3.1</version>maven
</dependency>ide
<dependency>post
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <h1>this is file upload test!</h1> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="text" name="username"/> <input type="file" name="upfile"/> <input type="submit" value="submit"/> </form> </body> </html>
servlet:
import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.Streams; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; /** * fileupload上傳測試 * * @author leizhimin 14-4-11 上午10:35 */ public class TestuploadServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { boolean flag = ServletFileUpload.isMultipartContent(req); FileOutputStream fos = null; InputStream is =null; try { if (flag) { ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iter = upload.getItemIterator(req); while (iter.hasNext()){ FileItemStream fis = iter.next(); is = fis.openStream(); if(fis.isFormField()){ System.out.print(fis.getFieldName()); System.out.println(":"+ Streams.asString(is)); }else{ System.out.println(fis.getName()); String path = req.getSession().getServletContext().getRealPath("/upload"); path = path+"/"+fis.getName(); System.out.println(path); fos = new FileOutputStream(path); byte[] buff = new byte[1024]; int len = 0; while ((len=is.read(buff))>0){ fos.write(buff,0,len); } } } } }catch (Exception e){ } PrintWriter writer = resp.getWriter(); writer.print("Hello World !"); } private static final long serialVersionUID = 1L; }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>upload</servlet-name> <servlet-class>TestuploadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>upload</servlet-name> <url-pattern>/upload</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>upload.jsp</welcome-file> </welcome-file-list> </web-app>
運行結果:
username:asdf qhtf.sql D:\IdeaProjects\fileuploadtest\web\upload/qhtf.sql