FileUpload组件上传文件

利用FileUpload组件上传文件,需要到apache上下载jar包 
http://commons.apache.org/fileupload/  
同时因为fileupload组件本身依赖apache common下的io包,所以也需要下载 

http://commons.apache.org/io/ 

form.jsp 

<form action="/fileupload/UploadServlet"  method="post" enctype="multipart/form-data">
    	username:<input type="text" name="username" /><br/>
    	password:<input type="password" name="password"/><br/>
    	file1:<input type="file" name="file1"/><br/>
    	file2:<input type="file" name="file2"/><br/>
    	<input type="submit" name="submit" value="submit"/>
 </form>
result.jsp 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
username:<%= request.getAttribute("username") %><br/>
password:<%= request.getAttribute("password") %><br/>
file1:<%= request.getAttribute("file1") %><br/>
file2:<%= request.getAttribute("file2") %><br/>

Servlet类

package com.test.UploadServlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		//创建一个工厂类
		DiskFileItemFactory factory = new DiskFileItemFactory();
		String path = request.getRealPath("/upload");
		//设置上传文件放在磁盘上的临时目录,如果上传的文件大于设置的最大值,则用该临时目录,否则用内存做临时存储
		factory.setRepository(new File(path));
		//这个参数就是上传文件临时是放在内存还是放在磁盘的文件大小设置,如果上传文件超出这个大小,则放磁盘,否则放内存
		factory.setSizeThreshold(1024*1024);
		
		//上传的关键对象
		ServletFileUpload fileuplod = new ServletFileUpload(factory);
		try {
			//解析各个表单域
			List<FileItem> list = fileuplod.parseRequest(request);
			
			for(FileItem fileItem : list){
				//Field有2种,一种是简单域,一种是file域,如果fileItem.isFormField()==true ,则代表是简单域
				if(fileItem.isFormField()){
					//获得简单域的名字
					String fieldName = fileItem.getFieldName();
					//获得简单域的值
					String fieldValue = fileItem.getString("UTF-8");
					System.out.println(fileItem.getName());
					request.setAttribute(fieldName, fieldValue);
				}else{
					//获得file域的名字
					String fieldName = fileItem.getFieldName();
					//获得file域的值,带路径,即是路径+文件名
					String value = fileItem.getName();
					//取的file域的值的名字,不带路径
					int pos = value.lastIndexOf("\\");
					String fieldValue = value.substring(pos+1);
					//获得是file文件的内容,而不是路径+文件名
					System.out.println( fileItem.getString("UTF-8") );
					request.setAttribute(fieldName, fieldValue);
					
					/*第一种方法:
					//保持文件名不变,把上传文件传到服务器上的目录
					OutputStream os = new FileOutputStream(new File(path,fieldValue));
					
					InputStream is = fileItem.getInputStream();
					byte buf[] = new byte[1024];
					int length = 0;
					while( (length = is.read(buf)) > 0 ){
						os.write(buf, 0, length);
					}
					//关闭流
					os.close();
					is.close();
					*/
					
					//第二种方法
					fileItem.write(new File(path,fieldValue));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.getRequestDispatcher("/result.jsp").forward(request, response);
	}

}

相关文章
相关标签/搜索