首先,一段实现文件上传功能 的广为流传的代码如下:(fileupload1.0)<%@ page language=“java”contentType=“text/html;charset=GBK”%>
<%@ page import=“java.util.*”%>
<%@ page import=“org.apache.commons.fileupload.*”%>
<html>
<head>
<title>保存上传文件</title>
</head>
<%
String msg = “”;
FileUpload fu = new FileUpload();
// 设置允许用户上传文件大小,单位:字节
fu.setSizeMax(10000000);
// maximum size that will be stored in memory?
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath(“C:\\TEMP”);
//开始读取上传信息
List fileItems = fu.parseRequest(request);
%>
<body bgcolor=“#FFFFFF”text=“#000000” leftmargin=“0”topmargin=“40”marginwidth=“0” marginheight=“0”>
<font size=“6”color=“blue”>文件列表:</font>
<center>
<table cellpadding=0 cellspacing=1 border=1 width=“100%”>
<tr>
<td bgcolor=“#008080”>文件名</td>
<td bgcolor=“#008080”>大小</td>
</tr>
<%
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals(“”)) && size==0)
continue;
%>
<tr>
<td><%=item.getName()%></td>
<td><%=item.getSize()%></td>
</tr>
<%
//保存上传的文件到指定的目录
name = name.replace(‘:’,‘_’);
name = name.replace(‘\\’,‘_’);
item.write(“F:\\”+ name);
}
}
%>
</table><br/><br/>
<a href=“upload.html”>返回上传页面</a>
</center>
</body>
</html>
其中 setSizeThreshold( );setRepositoryPath()这两个API在fileupload1.1中已经不用了,取而代之的均是 DiskFileItemFactory()。但我直接替换又不行,E文文档中写的似乎不是非常明确,请教如何实现文件上传功能?
另:如果有fileupload1.0,敬请发给我:[email protected]
<%@ page import=“java.util.*”%>
<%@ page import=“org.apache.commons.fileupload.*”%>
<html>
<head>
<title>保存上传文件</title>
</head>
<%
String msg = “”;
FileUpload fu = new FileUpload();
// 设置允许用户上传文件大小,单位:字节
fu.setSizeMax(10000000);
// maximum size that will be stored in memory?
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath(“C:\\TEMP”);
//开始读取上传信息
List fileItems = fu.parseRequest(request);
%>
<body bgcolor=“#FFFFFF”text=“#000000” leftmargin=“0”topmargin=“40”marginwidth=“0” marginheight=“0”>
<font size=“6”color=“blue”>文件列表:</font>
<center>
<table cellpadding=0 cellspacing=1 border=1 width=“100%”>
<tr>
<td bgcolor=“#008080”>文件名</td>
<td bgcolor=“#008080”>大小</td>
</tr>
<%
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals(“”)) && size==0)
continue;
%>
<tr>
<td><%=item.getName()%></td>
<td><%=item.getSize()%></td>
</tr>
<%
//保存上传的文件到指定的目录
name = name.replace(‘:’,‘_’);
name = name.replace(‘\\’,‘_’);
item.write(“F:\\”+ name);
}
}
%>
</table><br/><br/>
<a href=“upload.html”>返回上传页面</a>
</center>
</body>
</html>
其中 setSizeThreshold( );setRepositoryPath()这两个API在fileupload1.1中已经不用了,取而代之的均是 DiskFileItemFactory()。但我直接替换又不行,E文文档中写的似乎不是非常明确,请教如何实现文件上传功能?
另:如果有fileupload1.0,敬请发给我:[email protected]
比你那个好用。
↑Download
文件:commons-fileupload-1.1.zip
commons-io-1.2.zip(別忘了、与”commons-fileupload-1.1.jar”放一起)....
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List list = upload.parseRequest(request);
Iterator iter= list.iterator();
while (iter.hasNext()) {
FileItem fileItem = (FileItem) iter.next();
if (fileItem.isFormField()) {
System.out.println(fileItem.getFieldName());
} else { // 文件処理
System.out.println(fileItem.getName());
String uploadedName = new File(fileItem.getName()).getName();
String savePath = getServletContext().getRealPath("yourPathName");
File directory = new File(savePath);
File saveFile = new File(directory,uploadedName);
fileItem.write(saveFile);
}
}//end_while
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
周末有事,没及时看,明天搞,嘿嘿
http://blog.csdn.net/Silo/archive/2006/04/18/668250.aspx