读书人

uploadify3.0 与 struts2组合

发布时间: 2012-12-21 12:03:49 作者: rapoo

uploadify3.0 与 struts2结合

$("input[id][id^='file_']").each(function(i,obj) {$(obj).uploadify({'langFile': '${ctx}/static/js/jquery-uploadify/uploadifyLang_zh.js','swf'      : '${ctx}/static/js/jquery-uploadify/uploadify.swf','uploader': '${ctx}/powerword/powerword_ajaxUpload.do',//servlet的路径或者.jsp 这是访问servlet 'scripts/uploadif' 'method'         : 'get','postData': {'id':'${powerword.id}','audioId':''+$(obj).attr("id")},'fileObjName'   : 'uploadify',//和input的name属性值保持一致就好,Struts2就能处理了   'cancelImage'    : '${ctx}/static/js/jquery-uploadify/uploadify-cancel.png','auto'           : true, //选定文件后是否自动上传,默认false'multi'          : false, //是否允许同时上传多文件,默认false'queueID': "fileList_"+$(obj).attr("id"),'debug': false,'removeCompleted': false,'requeueErrors': false,'progressData': "all",'queueSizeLimit' : 1, //限制在一次队列中的次数(可选定几个文件)。默认值= 999,而一次可传几个文件有 simUploadLimit属性决定。'fileSizeLimit': 50*1024*1024, //设置单个文件大小限制,单位为byte ,50M'fileTypeDesc'   : '支持格式:mp3', //如果配置了以下的'fileExt'属性,那么这个属性是必须的 'fileTypeExts'   : '*.mp3',//允许的格式onUploadSuccess : function(file,data,response) {   if(data){   var mp3 = eval('(' + data + ')');$("#label_"+$(obj).attr("id")).text(mp3.path);   }},onUploadError : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {$("#dialog-message").html(errorString);}});});

?

?

<input id='file_listenAudio' name='uploadify' type='file'/>

?

?

public class PowerwordAction extends Struts2BaseAction {/** *  */private static final long serialVersionUID = 1L;private File uploadify;                private String uploadifyContentType;                private String uploadifyFileName;/** * 配合uploadify插件使用的上传方法。 * * @author liwei * @return * @throws Exception  */private String uploadify() throws Exception{Date date = new Date();DateFormat df = new SimpleDateFormat("yyyy-M-d");String today = df.format(date);String path = "/upload/audio/powerword/" + today;File folder = new File(ServletActionContext.getServletContext().getRealPath(path));String filePath = path + "/" + UUID.randomUUID().toString().replaceAll("-", "") + ".mp3";if (!folder.exists())folder.mkdirs();File outFile = new File(ServletActionContext.getServletContext().getRealPath(filePath));FileOutputStream outStream = new FileOutputStream(outFile);FileInputStream inStream = new FileInputStream(uploadify);byte[] buffer = new byte[1024];int l = 0;while ((l = inStream.read(buffer)) > 0) {outStream.write(buffer, 0, l);}inStream.close();outStream.close();return filePath;}}

?

<package name="powerword" extends="sessionControl"><action name="powerword_ajaxUpload" method="ajaxUpload"><interceptor-ref name="fileUpload"><param name="maximumSize">102400000</param><param name="allowedTypes">audio/mp3,application/octet-stream</param></interceptor-ref><interceptor-ref name="sessionStack"/></action></package>

?

?

<filter><filter-name>struts2Filter</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2Filter</filter-name><url-pattern>*.do</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping><filter-mapping><filter-name>struts2Filter</filter-name><url-pattern>*.jsp</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping>

?

?

一开始查了N多资料没有对3.0的文章,3.0与2.0+不一样的地方就是fileObjName这个属性的名字换了

还有可能我的struts2的配置跟网上的不太一样,最后调试了很长时间才调试通过

3 楼 fengzhiyu_sh 2012-05-15 'postData' : {'id':'${powerword.id}','audioId':''+$(obj).attr("id")},
Struts2的Action中貌似无法获得传过来的参数啊? 4 楼 jackyrong 2012-08-22 想问下,我的是struts2+uploadfit 3.1,文件能上传成功,
上传的时候,另外用formdata传递了表单的其他参数到sturts2的后台,
struts2的后台也能接受到文件,只不过struts2返回的是一个json(把表单的
另外的传递的参数原样子以JSON输出到前端而已,在uploadfiy
中,DEBUG模式下,也看到有:
File ID: SWFUpload_0_0 Response Received: true Data: {"version":"fdgfg"}

的字样,但是用:
'onUploadSuccess' : function(event, ID, fileObj, response, data) {
var mp3 = eval('(' + data + ')');
alert(data);
alert('The file ' + fileObj.name + ' was successfully uploaded with a response of ' + response + ':' + data);

}

打印出来的data和response都是空的?

读书人网 >编程

热点推荐