首页 > 代码库 > Servlet上传文件

Servlet上传文件

Servlet上传文件


1、准备工作

(1)利用FileUpload组件上传文件,需要到apache上下载commons-fileupload-1.3.1.jar

         下载地址:http://commons.apache.org/fileupload/

(2)由于文件上传还得有IO流传输,需要到apache上下载commons-io-2.4.jar

         下载地址:http://commons.apache.org/io/


2、正式开发

(1)新建文件上传界面

        file.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=http://www.mamicode.com/""> >
(2)新建文件上传成功界面

         result.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=http://www.mamicode.com/""> >
(3)新建servlet进行文件上传处理

        FileUploadServlet.java:

package com.you.file.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
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 FileUploadServlet extends HttpServlet 
{
	/**
	 * @Fields  serialVersionUID:
	 */
	private static final long serialVersionUID = -3788743064732005240L;
	
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		/**
		 * 设置编码格式
		 */
		request.setCharacterEncoding("UTF-8");
		/**
		 * 创建一个工厂类
		 */
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletContext context = getServletContext();  
		String path = context.getRealPath("/file");
		/**
		 * 设置上传文件放在磁盘上的临时目录
		 */
		factory.setRepository(new File(path));
		/**
		 * 设置上传文件大小
		 */
		factory.setSizeThreshold(1024*1024);
		
		//上传对象
		ServletFileUpload fileuplod = new ServletFileUpload(factory);
		try 
		{
			/**
			 * 解析各个表单域
			 */
			List<FileItem> list = fileuplod.parseRequest(request);
			
			for(FileItem item : list)
			{
				/**
				 * 判断是简单域 (item.isFormField()==true)
				 */
				if(item.isFormField())
				{
					//获得简单域的名字
					String fieldName = item.getFieldName();
					//获得简单域的值
					String fieldValue = http://www.mamicode.com/item.getString("UTF-8");>
(3)配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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">
  <display-name></display-name>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>FileUploadServlet</servlet-name>
    <servlet-class>com.you.file.servlet.FileUploadServlet</servlet-class>
   
  </servlet>

  <servlet-mapping>
    <servlet-name>FileUploadServlet</servlet-name>
    <url-pattern>/FileUploadServlet</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3、过程演示

(1)初始化时



(2)输入用户名、密码,上传文件



(3)还未上传,项目中的file文件夹



(4)上传成功后,页面显示



(5)上传成功后,file文件夹