前台html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Uploadify</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link href="css/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript" src="js/jquery.uploadify.v2.1.0.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'uploadify.swf', //指定上传控件的主体文件
'script' : 'uploadifile.action',//指定服务器端上传处理文件
'cancelImg' : 'images/cancel.png',//指定取消上传的图片
//'folder' : 'uploads', //您想将文件保存到的路径
'queueID' : 'fileQueue', //队列
'auto' : false, //选定文件后是否自动上传
'multi' : true, //是否允许同时上传多个文件
'simUploadLimit' : 2, //多文件上传时,同时上传文件数目限制
'buttonText' : 'BROWSE', //按钮显示文字,不支持中文,要用中文直接用背景图片cancelImg设置
'fileDataName': 'uploadify' //这个很重要这个名称必须要和后面 action里面的文件相对应</span>
});
});
</script>
</head>
<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p>
<a href="javascript:jQuery('#uploadify').uploadifyUpload()">开始上传</a>
<a href="javascript:jQuery('#uploadify').uploadifyClearQueue()">取消所有上传</a>
</p>
</body>
</html>
后台action中代码
package cn.rjxh.filterUpload;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import cn.rjxh.utils.SqlUtil;
import com.opensymphony.xwork2.ActionSupport;
public class UploadifyAction extends ActionSupport {
private File[] uploadify;
private String[] uploadifyFileName=null; // 文件名称
private String[] uploadifyContentType; // 文件类型
public void setUploadify(File[] uploadify) {
this.uploadify = uploadify;
}
public void setUploadifyFileName(String[] uploadifyFileName) {
this.uploadifyFileName = uploadifyFileName;
}
public void setUploadifyContentType(String[] uploadifyContentType) {
this.uploadifyContentType = uploadifyContentType;
}
public File[] getUploadify() {
return uploadify;
}
public String[] getUploadifyFileName() {
return uploadifyFileName;
}
public String[] getUploadifyContentType() {
return uploadifyContentType;
}
public void uploadifile() throws IOException {
PreparedStatement pstm = null;
Connection coon = SqlUtil.getConnection();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String savePath = ServletActionContext.getServletContext().getRealPath("");
savePath = savePath + "\\uploads\\"; //保存路径
System.out.println("************************savePath: " + savePath);
File f1 = new File(savePath);
System.out.println(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
String aa = "";
for(int i=0;i<uploadify.length;i++){
String newName =UUID.randomUUID().toString(); //文件的新名称
File uploadFile = uploadify[i];
String fileName = uploadifyFileName[i]; // 获取文件名称
String houzhui = fileName.substring(fileName.lastIndexOf(".") + 1,
fileName.length()); // 将文件的后缀名拿出来
aa = newName + "." + houzhui; // 对文件要重命名
String upPath = savePath + aa; // 获取文件的存放路径,插入数据库中
try {
FileUtils.copyFile(uploadFile, new File(f1, aa));
String sql = "insert into file (filepath) values (?)";
pstm = SqlUtil.getPstm(coon, sql);
try {
pstm.setString(1, upPath);
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
response.getWriter().print(aa); //这个比较重要哦,控制前台的代码,是不是图片自动消失的呢,而且随便添加多少个文件
}
}
就因为那第一个重要注释,搞的我差点放弃,不过还好,最后还是搞出来了