项目结构:
代码:
index.jsp:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
$(function(){
$("#uploadify").uploadify({
'debug' : false, //开启调试
'auto' : false, //是否自动上传
'swf' : 'uploadify/uploadify.swf', //引入uploadify.swf
'uploader' : 'upload;jsessionid=${pageContext.session.id}',//请求路径
'queueID' : 'fileQueue',//队列id,用来展示上传进度的
'width' : '75', //按钮宽度
'height' : '24', //按钮高度
'queueSizeLimit' : 3, //同时上传文件的个数
'fileTypeDesc' : '视频文件', //可选择文件类型说明
'fileTypeExts' : '*.jpg;*.gif', //控制可上传文件的扩展名
'multi' : true, //允许多文件上传
'buttonText' : '图片上传',//按钮上的文字
'fileSizeLimit' : '2MB', //设置单个文件大小限制
'fileObjName' : 'uploadify', //的name
'method' : 'post',
'removeCompleted' : true,//上传完成后自动删除队列
'onFallback':function(){
alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");
},
'onUploadSuccess' : function(file, data, response){//单个文件上传成功触发
//data就是action中返回来的数据
},'onQueueComplete' : function(){//所有文件上传完成
alert("文件上传成功!");
}
});
});
struts.xml:
UploadAction.java:
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import com.entity.Nfcuser;
import com.util.DeleteFile;
/**
* 上传文件到resource中
* @author Administrator
*
*/
public class UploadAction extends BaseAction {
private static final long serialVersionUID = 1L;
private File uploadify;//上传文件file对象
private String uploadifyFileName;//上传文件名
private String uploadifyContentType;//上传文件类型
private String description;//上传文件的描述
public String execute() throws IOException{
System.out.println("000000000");
Properties pro = new Properties();
InputStream in = DeleteFile.class.getClassLoader().getResourceAsStream("qiniu.properties");
pro.load(in);
String headpath = pro.getProperty("headpath");//读取配置文件,获取保存文件的根路径
String num = "";
for(int i&#61;0;i<5;i&#43;&#43;){
num &#43;&#61; String.valueOf((int)(Math.random()*10));//得到5位随机数
}
SimpleDateFormat dt &#61; new SimpleDateFormat("yyyyMMddhhmmss");
String date &#61; dt.format(new Date());//获取当前时间字符串
Nfcuser user &#61; (Nfcuser)session.get("loginuser");//从内存中得到用户对象
System.out.println(user.getUseraccount());
if(user&#61;&#61;null){
System.out.println("1");
return LOGIN;//用户未登录
}else{
System.out.println("2");
int userid &#61; user.getId();//用户编号
String filenum &#61; "";//生成保存上传文件的文件夹名称&#xff0c;这个名称也作为压缩文件的名称&#xff0c;
if(session.get("filenum")&#61;&#61;null){
System.out.println(3);
filenum &#61; userid &#43; date &#43; num;
session.put("filenum", filenum);
}else{
System.out.println(4);
System.out.println(session.get("filenum"));
filenum &#61; (String)session.get("filenum");
}
String path &#61; headpath &#43; userid &#43; "/" &#43; filenum &#43; "/resource/";//保存文件的路径
System.out.println(path);
File folder &#61; new File(path);
if(!folder.exists()){
folder.mkdirs();//如果路径不存在则创建
}
//下面才是保存文件&#xff0c;这上面都是对路劲的处理
System.out.println(uploadify);
String savePath &#61; path &#43; this.uploadifyFileName;
FileInputStream fis &#61; new FileInputStream(uploadify);
FileOutputStream fos &#61; new FileOutputStream(savePath);
IOUtils.copy(fis, fos);
fos.flush();
fos.close();
fis.close();
}
return null;
}
public File getUploadify() {
return uploadify;
}
public void setUploadify(File uploadify) {
this.uploadify &#61; uploadify;
}
public String getUploadifyFileName() {
return uploadifyFileName;
}
public void setUploadifyFileName(String uploadifyFileName) {
this.uploadifyFileName &#61; uploadifyFileName;
}
public String getUploadifyContentType() {
return uploadifyContentType;
}
public void setUploadifyContentType(String uploadifyContentType) {
this.uploadifyContentType &#61; uploadifyContentType;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description &#61; description;
}
}
注意&#xff1a;如果提交报错&#xff0c;数据根本就没有传递到后台&#xff0c;那么可以试着把提交路径改为
&#39;uploader&#39; : &#39;upload.action;jsessionid&#61;${pageContext.session.id}&#39;,//请求路径
如果web.xml中的配置为&#xff1a;
struts2
/*
那么路径可以为&#xff1a;
&#39;uploader&#39; : &#39;upload;jsessionid&#61;${pageContext.session.id}&#39;,//请求路径
如果web.xml配置为&#xff1a;
struts2
*.action
那么提交路径必须为&#xff1a;
&#39;uploader&#39; : &#39;upload.action;jsessionid&#61;${pageContext.session.id}&#39;,//请求路径
如果需要在UploadAction中使用session&#xff0c;那么在路径后把jsessionid&#61;${pageContext.session.id}&#39;加上&#xff0c;如果不需要使用session可以不用加。