今天晚上头有些痛,写篇文章,全当休息下吧。
讲两点:一是如何在Struts2中使用FlashFileUpload这个flash的上传插件;二是在使用flash上传插件时如何解决Session问题。
先说第一点,如何在Struts2中使用FlashFileUpload这个flash的上传插件。
以前做.net时,用过一个FlashFileUpload.swf批量文件上传工具,很帅很简单。
网址:
http://www.codeproject.com/KB/aspnet/FlashUpload.aspx
因为这个用Flex写的客户端插件公开源码,而且实现的的相当完美,在asp.net中用起来,就是一两行代码的事,所以非常喜欢。
最近想在Struts2中实现批量上传(使用html的file标签时,一次只能选一个文件,我是想一下可以选多个文件),结果找了半天,大都是swf+ajax的,用起来太复杂了,代码也很繁多,实在没耐心了。
就想起FlashFileUpload来了,结果发现不知道怎么用,官网上只给出了C#的例子,上网找了半天也没找到在Struts2中用的例子,甚至jsp的例子也没找到,最后自己研究出来了,在这里公开一下,希望对大家有所帮助。
大家从官网上下载了压缩包解压后,会找到Upload.cs,从代码中可以看到HttpContext对象可以直接把文件对象分离出来,想对上传的临时文件进行转存时,也很方便:
for(int j = 0; j < context.Request.Files.Count; j++)
{
// get the current file
HttpPostedFile uploadFile = context.Request.Files[j];
// if there was a file uploded
if (uploadFile.ContentLength > 0)
{
// save the file to the upload directory
//use this if testing from a classic style upload, ie.
// <form action="Upload.axd" method="post" enctype="multipart/form-data">
// <input type="file" name="fileUpload" />
// <input type="submit" value="Upload" />
//</form>
// this is because flash sends just the filename, where the above
//will send the file path, ie. c:\My Pictures\test1.jpg
//you can use Test.thm to test this page.
//string filename = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf("\\"));
//uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "Upload\\", filename));
// use this if using flash to upload
uploadFile.SaveAs(Path.Combine(uploadPath, uploadFile.FileName));
但是到了Struts2中,文件上传使用的是fileUpload拦截器实现文件对象的注入,注入时需要明确file对象的name,如:
<@s.file name="file" label="选择上传文件" />
(使用FreeMarker模板)
后台对应的Action中,需要定义对应的【private File file;】属性及其set方法,这样fileUpload拦截器才能正确的将file对象注入(设置)到Action中。当然多文件上传时,需要使用如【private List<File> file;】样的属性。
现在问题来了,当使用FlashFileUpload这个flash的客户端上传组件时,我们无法得知或声明文件对象的name值,我们也没有办法象在.net中,使用HttpContext拿到文件列表。
我就是在这里卡壳了,后来终于想出了个办法来获知其name了,就是伟大的Debug,通过在fileUpload拦截器中设置断点,或是在自己的Action方法中加上取Request对象的语句,通过断点查看Request中存放的对象,就能找到FlashFileUpload组件中file对象对应的name了,那就是:
Filedata,我们可以在Action中定义Filedata属性及其set方法,fileUpload拦截器就可以正确的将FlashFileUpload组件提交上来的文件对象设到Filedata里了。
代码示例:
/** 文件对象 */
private List<File> Filedata;
/** 文件名 */
private List<String> FiledataFileName;
/** 文件内容类型 */
private List<String> FiledataContentType;
/**
* @return the filedata
*/
public List<File> getFiledata() {
return Filedata;
}
/**
* @param filedata the filedata to set
*/
public void setFiledata(List<File> filedata) {
Filedata = filedata;
}
/**
* @return the filedataFileName
*/
public List<String> getFiledataFileName() {
return FiledataFileName;
}
/**
* @param filedataFileName the filedataFileName to set
*/
public void setFiledataFileName(List<String> filedataFileName) {
FiledataFileName = filedataFileName;
}
/**
* @return the filedataContentType
*/
public List<String> getFiledataContentType() {
return FiledataContentType;
}
/**
* @param filedataContentType the filedataContentType to set
*/
public void setFiledataContentType(List<String> filedataContentType) {
FiledataContentType = filedataContentType;
}
其他转存的代码就与Struts2中多文件上传的代码一模一样了。至此,我们就可以用最少的代码,来实现非常完美的批量上传了,效果相当不错。因为公开源码,所以如果你愿意,可以把FlashFileUpload这个flex做的组件改的更好看,或增加点自己的代码,我抓个粗略的效果图:
下面说一下第二点:使用flash上传插件时如何解决Session问题
在使用Flash上传文件时,存在一个Session丢失的问题。我们做文件上传时,一般都需要用户登录后,才允许上传,但是我发现在使用了Flash上传插件后,一上传Session就丢了,也就是说,在后台处理中,从Session中是取不到判断用户是否登录的标识的,原因是通过Flash上传时发起了一个独立于浏览器之外的请求,在后台程序中是无法通过Cookie取到对应的sessionId的,也就无法判断出用户是否登录了。
解决之法就是在jsp或ftl文件中定义flash上传组件的代码时,把sessionId顺便登记上,这样,通过【url;jsessionid=】的形式,可以把sessionId通过URL传到后台,就可以使Session不丢失了。
示例代码(FreeMarker):
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
width="100%" height="100%" id="file" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="/flash/FlashFileUpload.swf?ver=2009041702" />
<param name="quality" value="high" />
<param name="wmode" value="transparent">
<param name="FlashVars" value='fileTypeDescription=JPG格式图片&fileTypes=*.jpg;*.jpeg&fileSizeLimit=3000000&totalUploadSize=10240000&uploadPage=/performMultiuploadForDemo;jsessionid=${session.id}?username=${username}'>
<embed src="/flash/FlashFileUpload.swf?ver=2009041702" FlashVars='fileTypeDescription=JPG格式图片&fileTypes=*.jpg;*.jpeg&fileSizeLimit=3000000&totalUploadSize=10240000&uploadPage=/performMultiuploadForDemo;jsessionid=${session.id}?username=${username}'
quality="high" wmode="transparent" width="100%" height="100%" name="file"
align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>>
好了,写完收工。
- 大小: 21 KB
分享到:
相关推荐
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
专为multipart请求信息使用的org.apache.struts2.dispatcher.multipart.MultiPartRequest解析器接口(文件上传用) struts.multipart.saveDir The directory to use for storing uploaded files 设置存储上传...
Struts2漏洞检查工具Struts2.2019.V2.3
Struts2.0+jquery.progressbar实现上传文件进度条 附带实例源码 uploadJqueryProgress.jsp uploadprogress.jsp 两个上传页面,两种不一样风格的进度条。uploadJqueryProgress.jsp 是在 uploadprogress.jsp 的基础...
Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip
IBMStruts2培训PPT (大全集) 主讲:刘雷 完整实用 第一章:认识体验Struts2.ppt 第二章:struts2架构剖析.ppt ...第八章:struts2文件上传下载.ppt 第九章:struts2拦截器.ppt + Struts2开发详解.ppt
struts2的struts.properties配置文件详解
org.apache.struts2.dispatcher.FilterDispatcher
Struts批量上传文件.doc Struts批量上传文件.doc Struts批量上传文件.docStruts批量上传文件.doc
struts2+jquery.uploadify实现上传下载
struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传
Struts1[1].2实现单文件上传讲解.doc
Struts2升级到2.5.30,问题解决及过程记录, 文章详见:https://blog.csdn.net/sinat_36743893/article/details/124582101
struts2 中tags.tld文件,共享使用
以前和struts2一起使用的配置文件,struts2的核心控制器
自己做的一个struts2 实现文件批量上传功能,
struts2-showcase.rar struts2自带的源码例子