java 上传小实例

前端jsp
<html>
	<head>
		<meta http-equiv="Content-Type" contect="text/html;charset=utf-8">
	</head>
	<body>
		<form action="../fileupload/upload" method="post"
		enctype="multipart/form-data">
			<!-- file对应的input必须有name属性,否则不能上传 -->
			<input type="file" name="file"/>
			<input type="submit" value="submit" />
		</form>
	</body>
</html>


后台servlet
package org.xxm;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dareway.org.apache.commons.fileupload.FileItem;
import dareway.org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import dareway.org.apache.commons.fileupload.disk.DiskFileItemFactory;
import dareway.org.apache.commons.fileupload.servlet.ServletFileUpload;
import dareway.org.apache.commons.fileupload.util.Streams;

/**
 * @author xuxiaoming
 * 2009-03-19
 * 以tomcat6.0为服务器
 */
public class UploadFileServlet extends HttpServlet {
	public void doGet(HttpServletRequest req,
			HttpServletResponse res) {
		return;
	}
	
	public void doPost(HttpServletRequest req,
			HttpServletResponse res) throws IOException {
		/**
		 * form中的enctype必须是multipart/...
		 * 组件提供方法检测form表单的enctype属性
		 * 在isMultipartContent方法中同时检测了是否是post提交
		 * 如果不是post提交则返回false
		 */
		if(ServletFileUpload.isMultipartContent(req)) {
			req.setCharacterEncoding("utf-8");
			DiskFileItemFactory factory = new DiskFileItemFactory();
			/**
			 * 临时文件存储路径要真实存在
			 */
			factory.setRepository(new File("../webapps/fileupload/tmp"));
			//内存最大占用
			factory.setSizeThreshold(1024000);
			ServletFileUpload sfu = new ServletFileUpload(factory);
			//单个文件最大值byte
			sfu.setFileSizeMax(102400000);
			//所有上传文件的总和最大值byte
			sfu.setSizeMax(204800000);
			List items = null;
			try {
				items = sfu.parseRequest(req);
			} catch (SizeLimitExceededException e) {
				System.out.println("size limit exception!");
			} catch(Exception e) {
				e.printStackTrace();
			}
			
			Iterator iter = items==null?null:items.iterator();
			while(iter != null && iter.hasNext()) {
				FileItem item = (FileItem)iter.next();
				//简单的表单域
				if(item.isFormField()) {
					System.out.print("form field:");
					System.out.print(item.getFieldName() + "  ");
					System.out.print(item.getString());
				} 
				//文件域
				else if(!item.isFormField()) {
					System.out.println("client name:" + item.getName());
					String fileName = item.getName().substring(item.getName().lastIndexOf("\\"));
					BufferedInputStream in = new BufferedInputStream(item.getInputStream());
					//文件存储在工程的upload目录下
					BufferedOutputStream out = new BufferedOutputStream(
							new FileOutputStream(new File("../webapps/fileupload/upload/" + fileName)));
					Streams.copy(in, out, true);
				}
			}
		} else {
			System.out.println("enctype error!");
		}
	}
}

你可能感兴趣的:(java,apache,jsp,servlet)