一次上传400个文件的上传组件 附源代码

来源:互联网 发布:java最新框架 编辑:程序博客网 时间:2024/05/28 15:09
公司Quality Leader需要在公司的FMS系统上共享1000多个文档,在下专门为她定制了一个批量上传的组件,替代老的FMS里面单文件选择上次的方法。
开发的时候,我发现flash一次上传的文件大小要在100M以内,文件总和大概在200到500左右,因为文件的数量是由文件名的长短决定的。
老的FMS 采用的是Ajax框架,EXT 2.2,不果现在的EXT已经收费了,所以我也没兴趣了。对于flex也没太大的兴趣,因为其高级客户端组件如报表以及服务器组件等都是昂贵的,公司又不允许安装盗版软件(自从上次微软和Adobe的人来公司查过之后,我卸载了机器上所有的盗版软件),所以我目前也没打算做过多研究。
Flash大学的时候玩过,这次从新拿来练练手。
另外,通过flash上传的文件如果后台用Struts1.2的ActionForm来接收是有问题,因为flash上传的方式跟Stusts表单提交的结构不同,这时候我想到了直接从request里面取出来,在放到common-upload组件对象中区,发现总是异常,但用纯Servlet的 request是没问题的,研究了一下,发现Struts的request根Servlet中的request有写不一样,虽然大家都是被包装过的(装饰模式),但Sturts有可能改变了request的结构。解决的办法是,在Sturst配置文件中不要给该Action配置Form,这样又不会有问题了,所有对象直接从request中取。



Struts1.2 接收文件代码
/**
* 批量上传文件
*
* @param mapping
* @param form
* @param request
* @param response
* @throws Exception
*/
@SuppressWarnings(”unchecked”)
public ActionForward fileBatchUpload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setCharacterEncoding(”UTF-8″);

request.setCharacterEncoding(”UTF-8″);
FMSSession session = BaseSession.getInstance().getBean(
FMSSession.class, FMSUtil.getProperty(”fms.provider.url”));
FMSStaffVO fmsStaffVO = getStaffVO(request);
PrintWriter pw = null;
String fileName = null;
String path = null;
String realPath = null;
int fileSize = 0;
FMSFile fmsFile = null;
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding(”UTF-8″);
List items = null;
items = upload.parseRequest(request);
for (int i = 0; items != null && i < items.size(); i++) {
FileItem item = (FileItem) items.get(i);
if(item.getFieldName() != null && item.getFieldName().equals(”uploadFolderPath”)){
path = new String(item.getString().getBytes(”iso8859-1″),”UTF-8″);
}
System.out.println(”—————————————————-”);
System.out.println(”item.getFieldName()=”+item.getFieldName());
System.out.println(”item.getName()=”+ item.getName());
System.out.println(”item.isFormField()=”+item.isFormField());
System.out.println(”item.isInMemory()=”+item.isInMemory());
System.out.println(”item.getContentType()=”+item.getContentType());
System.out.println(”—————————————————-”);
}
pw = response.getWriter();
if(path == null || path.equals(”"))    {
pw.println(”<B>路径不正确</B>”);
pw.flush();
pw.close();
return null;
}
for (int i = 0; items != null && i < items.size(); i++) {
FileItem item = (FileItem) items.get(i);
if(item.getFieldName() != null && item.getFieldName().equals(”Filedata”)){
fileName = item.getName();
realPath = FMSUtil.getProperty(”root.path”)+ path;
File folder = new File(realPath);
System.out.println(”folder.exists()=”+folder.exists());
System.out.println(folder);
if(folder.exists() && folder.isDirectory()){
File file = new File(realPath+”\\” + fileName);
FileOutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
BufferedInputStream bis = new BufferedInputStream(item.getInputStream());
//copy file
fileSize = org.apache.commons.io.IOUtils.copy(bis, bos);
<!-- Easy AdSense V2.82 --> <!-- Post[count: 2] -->
//authority
fmsFile = new FMSFile();
fmsFile.setName(fileName);
fmsFile.setPath(path);
fmsFile.setSize(fileSize+”");
fmsFile.setCreateBy(fmsStaffVO.getStaffId()+”");
fmsFile.setCreateDate(session.getDate());
fmsFile.setModifyBy(fmsFile.getCreateBy());
fmsFile.setModifyDate(fmsFile.getCreateDate());


Set<IDocumentAccess> das = new HashSet<IDocumentAccess>();
FMSFileAccess da = new FMSFileAccess();
da.setAccessUnitCategory(FMSFileAccess.ACCESS_UNIT_CATEGORY_STAFF);
da.setAccessUnitId(fmsStaffVO.getStaffId()+”");
Set<String> auths = new HashSet<String>();
auths.add(”5″);
da.setPurviewSet(auths);
da.setFile(fmsFile);
das.add(da);

da = new FMSFileAccess();
da.setAccessUnitCategory(FMSFileAccess.ACCESS_UNIT_CATEGORY_ROLE);
da.setAccessUnitId(”45″);//public access
auths = new HashSet<String>();
auths.add(”1″);
da.setPurviewSet(auths);
da.setFile(fmsFile);
das.add(da);
session.saveDocsAccess(fmsFile, das);
/*List<FMSDocument> fMSDocuments = new ArrayList<FMSDocument>();
fMSDocuments.add(fmsFile);
session.save(fMSDocuments);*/

bis.close();
bos.close();

pw.println(”<B>上传成功</B>”);
pw.flush();
pw.close();
}else{
pw.println(”<B>路径不存在</B>”);
pw.flush();
pw.close();
return null;
}
}

}
pw.println(”<B>上传成功</B>”);
pw.flush();
pw.close();
} catch (Exception e) {
if(pw !=null){
pw.println(”<B>文件上传失败</B>”);
pw.flush();
pw.close();
}
//删除文件
if(realPath != null && fileName != null){
File file = new File(realPath+”\\”+fileName);
if(file.exists()){
file.delete();
}
}
//删除数据库文件和权限
session.deleteFmsDocumentByDocument(fmsFile, fmsStaffVO.getStaffId()+”");
return null;
}
return null;
}
flash文件上传组件附件:
FileUpload
原创粉丝点击