需要commons-fileupload.jar和 commons-io-1.2.jar包
注意:commons-fileuplaod1_2与1_1区别 如下:
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
改为<%@ page import="org.apache.commons.fileupload.DefaultFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
改为<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
1.index.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
contentType="text/html; charset=utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传图片</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form name="Webform" action="upload.jsp" method="post"
enctype="multipart/form-data">
输入你的名字:
<br />
<input type="text" name="name" size="15" value="hello" />
<br />
图形:
<br />
<input type="file" name="myimage">
<br />
文件:
<br />
<input type="file" name="myfile">
<br />
<br />
<input type="submit" name="Submit" value="Submit Files" />
</form>
</body>
</html>
2. upload.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.File"%>
<%@ page import="java.text.*"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.FileUpload"%>
<%@ page import="org.apache.commons.fileupload.DefaultFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileUploadBase"%>
<%@ page import="org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'upload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
// 检查输入请求是否为multipart的表单数据。
boolean isMultipart = DiskFileUpload.isMultipartContent(request);
if (!isMultipart) {
out.print("表单数据格式不是multipart/form-data,或者非法提交");
return;
}
DefaultFileItemFactory factory = new DefaultFileItemFactory();// Create a factory for disk-based file items
factory.setSizeThreshold(102400);// 设置初始化内存,如果上传的文件超过该大小,将不保存到内存,而且硬盘中(单位:byte)
File fileTemp = new File("c://temp");// 建立临时目录
fileTemp.mkdir();
%>
<%--
如果上传的文件超过设置内存的大小,将文件保存至该目录,
待文件上传完毕后,自动删除上传的临时文件;
如果多次上传同一个文件,该文件会保存到服务器的临时目录,而不删除
--%>
<%
factory.setRepository(fileTemp);
DiskFileUpload upload = new DiskFileUpload(factory);// 创建文件上传对象
upload.setSizeMax(1024000);// 设置客户端最大上传,-1为无限大(单位:byte)
//upload.setSizeMax(1024000);
//FileUploadBase.SizeLimitExceededException
List items = null;
try {
items = upload.parseRequest(request);// 分析request,并将保存结果至List里
System.out.println(items);
} catch (SizeLimitExceededException e) {
out.print("上传的文件超过系统限制(1MB)");
return;
}
Iterator itr = items.iterator();
while (itr.hasNext()) {// 循环处理表单元素
FileItem item = (FileItem) itr.next();
System.out.println("item is: " + item);
if (item.isFormField()) {// 普通的表单
if ("name".equals(item.getFieldName())) {
String name = item.getFieldName();
System.out.println(name);
String value = item.getString("utf-8");
System.out.println(name + ":=" + value);
}
} else {// 上传文件
if (item.getSize() != 0) {
String filename = item.getName();// 获取文件的客户端上传路径
System.out.println("最初的filename=" + filename);
int i = filename.lastIndexOf("//");
filename = filename.substring(i + 1);// 获取文件名
System.out.println("文件名1=:" + filename);
/**
//注意:要支持中文,请用下面这两句,OK,不过如果中文是奇数,好像会出错.
// System.out.println("文件名:"+new String(filename.getBytes(),"utf-8"));
//filename = new String(filename.getBytes(),"utf-8");
**/
System.out.println("Realpath is:"
+ getServletContext().getRealPath("/"));
//注意:创建File savedFile时,要在getRealPath("/")的目录下创建一个temp的目录,因为它传了getRealPath("/")+"temp"字符串了.
File savedFile = new File(getServletContext()
.getRealPath("/")
+ "temp_files");
savedFile.mkdirs();
//File savedFile = new File("D://test//upload3");
//savedFile.mkdirs();
savedFile = new File(savedFile, filename);
System.out.println(item);
item.write(savedFile);
out.println("upload success!!!");
System.out.println("upload success!!!");
}
}
}
%>
</body>
</html>