Servlet中配合common-fileupload.jar和common-io.jar进行文件上传

common-fileupload.jar和common-io.jar是apache组织下文件上传的开发包,有了这两个jar包,文件上传将变得方便很多。

①上传文件的界面

    文件上传. 
userName:
file1:
file2:

②上传处理的servlet,首先需要在web.xml中配置

  
    FileUpload
    edu.njcit.upload.FileUpload
  
  
    FileUpload
    /FileUpload
  

③接下来是处理servlet

package edu.njcit.upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
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 FileUpload extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void destroy() {
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 得到WEB-ROOT下某个文件夹的实际的,绝对的路径
		String path = request.getRealPath("/fileUpload");
		// 当文件大于默认的10K时,文件将放到该仓库临时存储
		factory.setRepository(new File(path));
		// 修改默认的10K为1M大小
		factory.setSizeThreshold(1024 * 1024);// 1M
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List list = upload.parseRequest(request);
			// 遍历
			for (FileItem item : list) {
				String name = "";
				// 1、当是简单的文本域时
				if (item.isFormField()) {
					name = item.getFieldName();// userName
					// 编码格式转换
					name = new String(name.getBytes("gbk"), "UTF-8");
					String inputValue = item.getString("UTF-8");
					System.out.println(name + "=" + inputValue);
					request.setAttribute(name, inputValue);
				} else {
					// 2、是文件
					// 获取文件不带路径的名字,注意opera浏览器会有路径信息
					name = item.getFieldName();
					name = new String(name.getBytes("gbk"), "UTF-8");
					String value = item.getName();
					// 解决opera浏览器问题
					int start = value.lastIndexOf("\\");
					String fileName = value.substring(start + 1);
					fileName = new String(fileName.getBytes("gbk"), "UTF-8");
					System.out.println("fileName:" + fileName);
					request.setAttribute(name, fileName);
					// 写文件
					item.write(new File(path, fileName));

					/**
					// 手动写磁盘start
					OutputStream out = new FileOutputStream(new File(path, fileName));
					InputStream in = item.getInputStream();
					byte buffer[] = new byte[1024];
					int lenght = 0;
					while ((lenght = in.read(buffer)) != -1) {
						out.write(buffer, 0, lenght);
					}
					out.close();
					in.close();
					// 手动写磁盘end
                    */
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		// /WEB-INF/page/fileUploadResult.jsp去掉WEB-INF前面的/也是可以的
		request.getRequestDispatcher("/WEB-INF/page/fileUploadResult.jsp")
				.forward(request, response);
	}

	public void init() throws ServletException {
	}

}
④后台写磁盘结束后,将跳转到fileUploadResult.jsp,如下:

上传结果: 
userName:${requestScope.userName }
file1:${requestScope.myfile1}
file2:${requestScope.myfile2}



你可能感兴趣的:(Java,web)