Uploadify在Struts2中的应用(转)
步骤一: 到官网上下载uploadify的JS文件.
Uploadify在线演示:在线Demo
Uploadify配置参数及接口文档:http://www.uploadify.com/documentation
Uploadify插件下载地址:http://www.uploadify.com/download
--------------------------------
步骤二:下载好uploadify压缩包文件后,解压文件包。在文件夹中找到以下五个文件,并添加到项目的对应路径中:
jquery.uploadify.v2.1.0.js
swfobject.js
uploadify.swf
uploadify.css
cancel.png
步骤三:加入struts2的jar包、struts.xml ,在web.xml中加入struts2的FilterDispatcher过滤器。
---------------------------------------------------------
1、jsp代码:
----------------------------------------------------------
2、web.xml
----------------------------------------------------------
4、action代码package com.lijigou.action;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class UploadAction extends ActionSupport { private File uploadify; private String uploadifyFileName; @SuppressWarnings("deprecation") public String uploadFile() throws Exception { String extName = "";//扩展名 String newFileName= "";//新文件名 String nowTime = new SimpleDateFormat("yyyymmddHHmmss").format(new Date());//当前时间 String savePath = ServletActionContext.getRequest().getRealPath(""); savePath = savePath +"/uploads/"; HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncoding("utf-8"); //获取扩展名 if(uploadifyFileName.lastIndexOf(".")>=0){ extName = uploadifyFileName.substring(uploadifyFileName.lastIndexOf(".")); } newFileName = nowTime+extName; uploadify.renameTo(new File(savePath+newFileName)); response.getWriter().print(uploadifyFileName+"上传成功"); return null; //这里不需要页面转向,所以返回空就可以了 } public File getUploadify() { return uploadify; } public void setUploadify(File uploadify) { this.uploadify = uploadify; } public String getUploadifyFileName() { return uploadifyFileName; } public void setUploadifyFileName(String uploadifyFileName) { this.uploadifyFileName = uploadifyFileName; }}
以上代码都通过测试,没有问题的。