$("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" class="powerwordAction" 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的配置跟网上的不太一样,最后调试了很长时间才调试通过