日期:2014-05-16 浏览次数:20877 次
以Apache组织的commons项目中的FileUpload项目做为jsp的文件上传组件。
?
在开始之前你要准备以下几个东西:
1. commons-FileUpload ?包?
? 下载地址:http://commons.apache.org/fileupload/
2. commons-IO ?包?
? 下载地址:http://commons.apache.org/io/
3. Commons-BeanUtils ?包?
? 下载地址:http://commons.apache.org/beanutils/
?
准备工作完成就开始了:
?
1、新建一个WEB项目upload;
?
2、把下载好了的jar包复制到项目的WEB-INF/lib目录下;
3、新建或修改JSP文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my upload JSP page. <br> <form name="upform" action="UploadServlet" method="POST" enctype="multipart/form-data"> <input type="file" name="file1" id="file1" /> <br /> <input type="file" name="file2" if="file2" /> <br /> <input type="file" name="file3" id="file3" /> <br /> <input type="text" name="text1" id="text1" /> <br /> <input type="submit" value="Submit" /> <input type="reset" /> </form> </body> </html>
?
?
上面有几个要注意的地方:
a、method在这时必须是post的;
b、enctype="multipart/form-data"使表单提交的流而不是表单文本。
?
4、书写UploadServlet.java
package demo.servlet; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.ProgressListener; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.Streams; /** * Servlet implementation class for Servlet: UploadServlet */ public class UploadServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { /** 初始化上传文件的临时存放目录 */ File tmpDir = null; /** 初始化上传文件后的保存目录 */ File saveDir = null; public UploadServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); try { // 检查是否是文件上传类型的请求 enctype="multipart/form-data" if (Serv