使用commons-fileupload实现文件上传
一、准备:
1、 下载commons-fileupload库,下载地址:http://commons.apache.org/fileupload/
2、 下载commons-fileupload依赖库commons-io,下载地址:http://commons.apache.org/io/
二、表单页面:
<form name="uploadform" method="post" action="save.jsp" enctype="multipart/form-data">
<table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9bd7ff">
<tr>
<td width="100%" colspan="2">
文件1:<input name="file1" size="40" type="file">
</td>
</tr>
<tr>
<td width="100%" colspan="2">
文件夹:<input name="dir" size="40" type="text">
</td>
</tr>
<tr>
<td align="center">
<input name="upload" type="submit" value="开始上传">
</td>
</tr>
</table>
</form>
三、表单处理页面:
<%!
// 处理表单中的非文件上传域
void processFormField(FileItem fileItem,JspWriter out) throws IOException{
if(fileItem.isFormField()) {
String name=fileItem.getFieldName();
String value=new String(fileItem.getString().getBytes("iso-8859-1"),"UTF-8");
String fileName=fileItem.getName();
out.println("===================<br>");
out.println("FieldName: " + name + "<br>");
out.println("FieldValue: " + value + "<br>");
out.println("FileName: " + fileName + "<br>");
out.println("===================<br><br>");
}
}
// 处理表单中的文件上传域
void processUploadedFile(FileItem fileItem, JspWriter out) throws IOException{
if(!fileItem.isFormField()) {
String fieldName=fileItem.getFieldName();
String fileName=fileItem.getName();
String contentType=fileItem.getContentType();
boolean isInMemory=fileItem.isInMemory();
long sizeInBytes=fileItem.getSize();
// 打印文件信息
out.println("===================<br>");
out.println("FieldName: " + fieldName + "<br>");
out.println("FileName: " + fileName + "<br>");
out.println("ContentType: " + contentType + "<br>");
out.println("Size: " + sizeInBytes + "<br>");
out.println("===================<br><br>");
// 保存文件
}
}
%>
<%
// 是否支持文件上传
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// 如果isMultipart为false,表单的编码格式将不支持文件上传
if(isMultipart){
// 设置文件上传Bean属性
// 文件上传Bean,在内存中用于存放上传文件的内存大小为10*1024*1024byte
// 如果超出这个大小,将存放在临时文件夹D:/temp下
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File("D://temp"));
factory.setSizeThreshold(10*1024*1024);
// 分析上传文件
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
// 处理上传文件
Iterator iter=items.iterator();
while(iter.hasNext()){
FileItem item =(FileItem)iter.next();
if(item.isFormField()) {
// 处理非文件上传域
processFormField(item, out);
} else {
// 处理文件上传域
processUploadedFile(item, out);
}
}
}
%>
使用commons-fileupload实现文件上传----源代码2007-12-30 20:53完整的源代码:
index.jsp:
<%@page contentType="text/html"%>
<%@page 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>
<center>
<h1>文件上传演示</h1>
<form name="uploadform" method="post" action="save.jsp" enctype="multipart/form-data">
<table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9bd7ff">
<tr>
<td width="100%" colspan="2">
文件夹:<input name="dir" size="40" type="text">
</td>
</tr>
<tr>
<td width="100%" colspan="2">
文件1:<input name="file1" size="40" type="file">
</td>
</tr>
<tr>
<td width="100%" colspan="2">
文件2:<input name="file2" size="40" type="file">
</td>
</tr>
<tr>
<td width="100%" colspan="2">
文件1:<input name="file3" size="40" type="file">
</td>
</tr>
<tr>
<td align="center">
<input name="upload" type="submit" value="开始上传">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
save.jsp:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.servlet.*"%>
<%@page import="org.apache.commons.fileupload.disk.*"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<!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>
<%!
String mySaveDir = null;
// 处理表单中的非文件上传域
void processFormField(FileItem fileItem,JspWriter out) throws IOException{
if(fileItem.isFormField()) {
String name=fileItem.getFieldName();
String value=new String(fileItem.getString().getBytes("iso-8859-1"),"UTF-8");
// 输出域信息
out.println("===================<br>");
out.println("FieldName: " + name + "<br>");
out.println("FieldValue: " + value + "<br>");
out.println("===================<br><br>");
// 如果为文件夹dir域
if(name.equals("dir")) {
mySaveDir = value.trim();
System.out.println("获得dir");
}
}
}
// 处理表单中的文件上传域
void processUploadedFile(FileItem fileItem, JspWriter out)
throws IOException, Exception{
if(!fileItem.isFormField()) {
String fieldName=fileItem.getFieldName();
String fileName=fileItem.getName();
String contentType=fileItem.getContentType();
boolean isInMemory=fileItem.isInMemory();
long sizeInBytes=fileItem.getSize();
// 输出文件信息
out.println("===================<br>");
out.println("FieldName: " + fieldName + "<br>");
out.println("FileName: " + fileName + "<br>");
out.println("ContentType: " + contentType + "<br>");
out.println("Size: " + sizeInBytes + "<br>");
out.println("===================<br><br>");
// 保存文件
// 保存在D:/images/下
String imageDir = "D://images//";
System.out.println("当前的dir:" + mySaveDir);
if(mySaveDir != null && !mySaveDir.trim().equals("")){
imageDir = mySaveDir;
System.out.println("Images的dir:" + imageDir);
}
// 不存在指定的文件夹,创建此文件夹
File saveDir = new File(imageDir);
if(!saveDir.exists()) {
saveDir.mkdirs();
}
// 不存在指定的文件,创建此文件
File saveFile= new File(saveDir, fileName);
if(!saveFile.exists()) {
saveFile.createNewFile();
}
fileItem.write(saveFile);
}
}
%>
<%
// 是否支持文件上传
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// 如果isMultipart为false,表单的编码格式将不支持文件上传
if(isMultipart){
// 设置文件上传属性
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File("D://temp"));
factory.setSizeThreshold(10*1024*1024);
// 分析上传文件
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
// 显示上传文件
Iterator iter=items.iterator();
while(iter.hasNext()){
FileItem item =(FileItem)iter.next();
if(item.isFormField()) {
// 处理非文件上传域
processFormField(item, out);
} else {
// 处理文件上传域
processUploadedFile(item, out);
}
}
}
%>
</body>
</html>