等提供了对struts2、spring mvc3的注解(annotation)一些高级特性的支持,而且不是SNAPSHOT版了时,在好好试试.^_^######对这些特性的支持,和这个表单文件上传的本身关系不大,不过能方便这些框架的使用者######您好,能给介绍少为啥会说他的效率在java实现中,比common-fileupload 要快吗?######fastupload采用了BM算法,查找的效率比commons fileupload的算法效率高,而commons fileupload采取的是类似于KMP查找######我使用过fileupload和cos,在实际应用中cos是要比fileupload快,当时cos很久不更新了,不知道这个如何。######我测试的样本是1.7M, 1.7M,1.2M的三个文件,得出的结论是给予这个测试的,不知道你测试的样本是什么样的,能否告诉我?######持续关注.######

我使用过fileupload和cos,在实际应用中cos是要比fileupload快,当时cos很久不更新了,不知道这个如何。

对于第一种方式,因为少量buffer写入文件,所以和cos相比,fastupload略快,第二种方式,把所有数据读取到内存后再解析,比目前所有同类中的其他上传组件都要快很多。这里有一些测试数据,,其中也fastupload快速的查找算法也有提到

######补充一下,测试的环境为tomcat 7, jdk 6

######在这里回复 @仪山湖 已经有一段时间,记得不太清了,是当时分别用fileupload和cos各写了1个servlet做后台,前台用的是一个Flex插件叫FlashFileupload,Flex插件上有上传速度和百分比,上传同一文件,进行比较,小文件差距不太明显,文件越大,cos的优势就越来越明显。用的是tomcat6,jdk6。######能否贴一下你写的代码,供我分析一下######

很简单的servlet,也是从网上收集的

cos
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
public class CosUploadHandler extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml; charset=utf-8");
String rootPath = "D:\\";
PrintWriter out = response.getWriter();
String uploadDir = null;
//int fileNum = 0;
if(request.getParameter("folder") != null) {
uploadDir = request.getParameter("folder");
uploadDir = rootPath + uploadDir;
}
uploadDir = "D:\\upload";
//if(request.getParameter("fileNum")!= null) {
//fileNum = Integer.parseInt(request.getParameter("fileNum"));
//}
int maxPostSize = 200 * 1024 * 1024;
MultipartRequest multipartRequest = null;
try {
multipartRequest = new MultipartRequest(request,uploadDir,maxPostSize,"utf-8");
} catch(IOException ex) {
out.println("error!");
out.flush();
out.close();
return;
}
Enumeration> fileNames = multipartRequest.getFileNames();
while(fileNames.hasMoreElements()) {
String fileName = (String)fileNames.nextElement();
multipartRequest.getFile(fileName);
System.out.println(multipartRequest.getFilesystemName(fileName));
}
out.println("success!");
out.flush();
out.close();
}
}
fileupload
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
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;
public class FileUploadHandler extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml; charset=utf-8");
//PrintWriter out = response.getWriter();
String uploadDir = null;
if(request.getParameter("uploadDir") != null) {
uploadDir = request.getParameter("uploadDir");
uploadDir = "D:\\" +uploadDir;
}
uploadDir = "D:\\upload";
if(!ServletFileUpload.isMultipartContent(request))
{
//out.println("只能处理 multipart/form-data 类型的数据!");
return;
}
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setSizeThreshold(1024*1024);
ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory);
fileUpload.setHeaderEncoding("utf-8");
fileUpload.setFileSizeMax(1024 * 1024 * 200);
List itemList = null;
Iterator iterator = null;
try {
itemList = fileUpload.parseRequest(request);
} catch (FileUploadException e) {
//out.println("解析数据时出现如下问题:");
//e.printStackTrace(out);
//e.printStackTrace();
return;
}
iterator = itemList.iterator();
while(iterator.hasNext()) {
FileItem fileItem = (FileItem)iterator.next();
if(!fileItem.isFormField()) {
String srcPath = fileItem.getName();
if(srcPath.trim().equals("")){
continue;
}
String fileName = srcPath.substring(srcPath.lastIndexOf("//") + 1);
File destFile = new File(uploadDir,fileName);
try {
fileItem.write(destFile);
} catch (Exception e) {
//out.println("存储文件时出现如下问题:");
//e.printStackTrace(out);
//e.printStackTrace();
fileItem.delete();
return;
}
fileItem.delete();
}
}
//out.flush();
//out.close();
}
}
######
DiskFileItemFactory不是fastupload中的类,fastupload的代码应该是这样的 DiskFileFactory dff = new DiskFileFactory(System.getProperty("user.home") , "utf-8"); dff.setParseThreshold(0x100000); HttpFileUploadParser parser = new HttpFileUploadParser(req, dff);
List files = parser.parse();######作者连代码都不舍得放到版本控制里呀。######只有个源代码包吧?在svn和git里面没找到代码。######源码在sourceforge上 http://sourceforge.net/projects/fastupload/