***web.xml***
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>fileUpload</servlet-name>
<servlet-class>cn.com.action.FileUpload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fileUpload</servlet-name>
<url-pattern>/fileUpload</url-pattern>
</servlet-mapping>
</web-app>
package cn.com.action;
import java.io.File;
import java.io.*;
import java.io.IOException;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*
* @author Administrator
* 文件上传
* 具体步骤:
* 1)获得磁盘文件条目工厂 DiskFileItemFactory 要导包
* 2) 利用 request 获取 真实路径 ,供临时文件存储,和 最终文件存储 ,这两个存储位置可不同,也可相同
* 3)对 DiskFileItemFactory 对象设置一些 属性
* 4)高水平的API文件上传处理 ServletFileUpload upload = new ServletFileUpload(factory);
* 目的是调用 parseRequest(request)方法 获得 FileItem 集合list ,
*
* 5)在 FileItem 对象中 获取信息, 遍历, 判断 表单提交过来的信息 是否是 普通文本信息 另做处理
* 6)
* 第一种. 用第三方 提供的 item.write( new File(path,filename) ); 直接写到磁盘上
* 第二种. 手动处理
*
*/
public class FileUpload extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
String path = request.getRealPath("/upload1");
/**
* 原理 它是先存到 暂时存储室,然后在真正写到 对应目录的硬盘上,
* 按理来说 当上传一个文件时,其实是上传了两份,第一个是以 .tem 格式的
* 然后再将其真正写到 对应目录的硬盘上
*/
factory.setRepository(new File(path));
factory.setSizeThreshold(1024*1024) ;
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> list = (List<FileItem>)upload.parseRequest(request);
for(FileItem item : list){
String name = item.getFieldName();
if(item.isFormField()){
String value = item.getString() ;
request.setAttribute(name, value);
}else{
/**
* 以下三步,主要获取 上传文件的名字
*/
String value = item.getName() ;
int start = value.lastIndexOf("\\");
String filename = value.substring(start+1);
request.setAttribute(name, filename);
OutputStream out = new FileOutputStream(new File(path,filename));
InputStream in = item.getInputStream() ;
int length = 0 ;
byte [] buf = new byte[1024] ;
System.out.println("获取上传文件的总共的容量:"+item.getSize());
while( (length = in.read(buf) ) != -1){
out.write(buf, 0, length);
}
in.close();
out.close();
}
}
}catch (FileUploadException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
request.getRequestDispatcher("filedemo.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
***index.jsp***
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传入口</title>
</head>
<body>
<form action="fileUpload" enctype="multipart/form-data" method="post" >
用户名称:<input type="text" name="usename"> <br/>
上传图片:<input type="file" name="file1"><br/>
上传文件:<input type="file" name="file2"><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
***filedemo.jsp***
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传文件图片案列</title>
</head>
<body>
用户名称:${requestScope.usename }<br />
图片名称:${requestScope.file1 }<br />
文件名称:${requestScope.file2 }<br />
<img alt="go" src="upload1/<%=(String) request.getAttribute("file1")%> " />
</body>
</html>