我使用的uploadify版本为3.2.1,整合SSH+uploadify
需要的文件有:
jquery-1.7.2.js(注意:这个文件是我自己单独下载的,uploadify文件夹里没有)
jquery.uploadify.js
jquery.uploadify.min.js
uploadify-cancel.png
uploadify.css
uploadify.swf
前台html代码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript" src="uploadify/jquery.uploadify.js"></script>
<script type="text/javascript" src="uploadify/jquery.uploadify.min.js"></script>
<link rel="stylesheet" type="text/css" href="uploadify/uploadify.css">
</head>
<script type="text/javascript">
$(function () {
$('#uploadify').uploadify({
uploader: 'upload!uploadFile', // 服务器端处理地址
swf: '/uploadify/uploadify.swf', // 上传使用的 Flash
width: 60, // 按钮的宽度
height: 23, // 按钮的高度
buttonText: "上传", // 按钮上的文字
buttonCursor: 'hand', // 按钮的鼠标图标
fileObjName: 'uploadify', // 上传参数名称 后台action里面的属性uploadify
// 两个配套使用
fileTypeExts: "*.jpg;*.png;*.txt", // 扩展名
fileTypeDesc: "请选择 jpg png txt 文件", // 文件说明
auto: false, // 选择之后,自动开始上传
multi: true, // 是否支持同时上传多个文件
queueSizeLimit: 5 // 允许多文件上传的时候,同时上传文件的个数
});
});
</script>
<body>
<input type="file" name="uploadify" id="uploadify" />
<a href="javascript:$('#uploadify').uploadify('cancel')">清除第一个文件</a>
<a href="javascript:$('#uploadify').uploadify('cancel', '*')">清除所有文件</a>
<a href="javascript:$('#uploadify').uploadify('upload', '*')">上传所有文件</a>
</body>
</html>
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import com.hhkj.tjsjbz.util.PublicOPCode;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("framework-package")
public class UploadAction extends ActionSupport{
private File uploadify;
private String uploadifyFileName;
public String uploadFile() throws IOException{
String savePath = ServletActionContext.getServletContext().getRealPath("/uploads/");//上传完后文件存放位置
System.out.println(savePath);
String newsuffix = "";
String current=PublicOPCode.getcurrentdate();
if((uploadifyFileName != null)&&(uploadifyFileName.length()>0))
{
int dot = uploadifyFileName.lastIndexOf(".");
if((dot >-1) && (dot < (uploadifyFileName.length() - 1)))
{
newsuffix = uploadifyFileName.substring(dot + 1);
}
}
FileInputStream fis = new FileInputStream(uploadify);
FileOutputStream fos = new FileOutputStream(savePath+"/"+ current + "." + newsuffix);
IOUtils.copy(fis, fos);
fos.flush();
fos.close();
fis.close();
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;
}
}