Struts2 结合uploadify 注解方式上传文件 带进度条显示

	
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>    
<%@ include file="../../../WEB-INF/jsp/common/inculde/cdn.jsp" %>
<!DOCTYPE html>
<html lang="en">  
<head>
<link rel="stylesheet" type="text/css" href="<%=cdn%>/demo_utils/jsp/uploadify/css/uploadify.css">	
<script type="text/javascript" src="<%=cdn%>/demo_utils/jsp/uploadify/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=cdn%>/demo_utils/jsp/uploadify/js/jquery.uploadify.js"></script>
<script type="text/javascript" src="<%=cdn%>/demo_utils/jsp/uploadify/js/jquery.uploadify.min.js"></script>
<script type="text/javascript">
$(function () {
	
	//setTimeout 解决chrome 下浏览器崩溃的问题
    setTimeout(function(){
        $('#uploadify').uploadify(
        	 {
        	        uploader: '<%=cdn%>/uploadify/!uploadFiles.action',           
        	        swf: '<%=cdn%>/demo_utils/jsp/uploadify/js/uploadify.swf', 
        	        cancelImg:	"<%=cdn%>/demo_utils/jsp/uploadify/img/uploadify-cancel.png",
        	        width: 60,                          // 按钮的宽度
        	        height: 23,                         // 按钮的高度
        	        buttonText: "上传",                 // 按钮上的文字
        	        buttonCursor: 'hand',                // 按钮的鼠标图标
        	        fileObjName: 'uploadify',            // 上传参数名称 后台<span style="color:#ff0000;">action里面的属性uploadify</span>
        	        // 两个配套使用
        	        fileTypeExts: "*.*",             // 扩展名
        	        fileTypeDesc: "请选择 jpg png txt 文件",     // 文件说明
        	        auto: false,                // 选择之后,自动开始上传
        	        multi: true,               // 是否支持同时上传多个文件
        	        queueSizeLimit: 5          // 允许多文件上传的时候,同时上传文件的个数
        	 });    	
    },10)

});
</script>
</head>
<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.company.core.upload.action;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import com.company.core.utils.StringUtils;
import com.company.core.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 上传action
 * @author winder
 * 2014-12-03
 */
@Namespace("/uploadify")
@Action("")
@ParentPackage("cc")
//@Auth(enalbe=Roles.MANAGER)
@InterceptorRefs({
	@InterceptorRef("defaultStack")
})
public class UploadifyAction extends BaseAction implements ModelDriven {
	
	private File uploadify;
	
	private String uploadifyFileName;
	
	private static final int BUFFER_SIZE = 16 * 1024;

	
	public void uploadFiles() throws IOException{
    	String s = File.separator;
        String path = ServletActionContext.getServletContext().getRealPath("/");
        String savePath = path + "demo_utils"+s+"jsp"+s+"uploadify"+s+"uploads"; //上传完后文件存放位置		
		String newsuffix = "";
		
		String uuid = StringUtils.getUUID();
		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+"/"+ uuid + "." + newsuffix);
		copy(fis, fos);
		fos.flush();
		fos.close();
		fis.close();
	}
	
	/**
     * 拷贝文件
     * @param src xx
     * @param dst xx
     */
	public void copy(FileInputStream fis, FileOutputStream fos) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(fis,BUFFER_SIZE);
				out = new BufferedOutputStream(fos,BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	
	
	
	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;
	}

	public Object getModel() {
		// TODO Auto-generated method stub
		return null;
	}	
	
}

 

 

你可能感兴趣的:(uploadify)