uploadify3.2.1+struts2实现多文件上传



 uploadify.css 中需要更改图片路径,否则不显示"X"

index.jsp上传页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<link rel="stylesheet" type="text/css" href="uploadify/uploadify.css">
	<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="uploadify/jquery.uploadify.min.js"></script>
	<script type="text/javascript">
	$(function(){
		$("#uploadify").uploadify({    
			'debug'     : false, //开启调试
	        'auto'           : false, //是否自动上传   
	        'swf'            : 'uploadify/uploadify.swf',  //引入uploadify.swf  
	        'uploader'       : 'upload',//请求路径  
	        'queueID'        : 'fileQueue',//队列id,用来展示上传进度的  
	        'width'     : '75',  //按钮宽度  
	        'height'    : '24',  //按钮高度
	        'queueSizeLimit' : 3,  //同时上传文件的个数  
	        'fileTypeDesc'   : '视频文件',    //可选择文件类型说明
	        'fileTypeExts'   : '*.jpg;*.gif', //控制可上传文件的扩展名  
	        'multi'          : true,  //允许多文件上传  
	        'buttonText'     : '图片上传',//按钮上的文字  
	        'fileSizeLimit' : '2MB', //设置单个文件大小限制   
	        'fileObjName' : 'uploadify',  //<input type="file"/>的name  
	        'method' : 'post',  
	        'removeCompleted' : true,//上传完成后自动删除队列  
	        'onFallback':function(){    
	            alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");    
	        }, 
	        'onUploadSuccess' : function(file, data, response){//单个文件上传成功触发  
	                               //data就是action中返回来的数据  
	        },'onQueueComplete' : function(){//所有文件上传完成  
	        	alert("文件上传成功!");
	       		}  
	        });
	});
	</script>
  </head>
  
  <body>
   <input type="file" id="uploadify" name="uploadify">  
   <div id="fileQueue"></div>  
   <a href="javascript:$('#uploadify').uploadify('upload','*')">开始上传</a>  
   <a href="javascript:$('#uploadify').uploadify('cancel')">取消所有上传</a>  
  </body>
</html>

 Action:

package com.struts2AndUploadify.action;



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
	
	private File uploadify;//上传文件file对象
	private String uploadifyFileName;//上传文件名
	private String uploadifyContentType;//上传文件类型
	private String description;//上传文件的描述
	private String uploadDir;//保存上传文件的目录,相对于web应用程序的根路径,在struts.xml文件中配置
	
	public String execute(){
		String newFileName=null;
		//得到当前时间开始流逝的毫秒数,将这个毫秒数作为上传文件新的文件名
		long now=new Date().getTime();
		//得到保存上传文件的真实路径
		String path=ServletActionContext.getServletContext().getRealPath(uploadDir);
		File dir=new File(path);
		//如果这个目录不存在,则创建它
		if (!dir.exists()) {
			dir.mkdir();
		}
		int index=uploadifyFileName.lastIndexOf(".");
		
		//判断上传文件是否有扩展名,以时间戳作为新的文件名
		if (index!=-1) {
			newFileName=now+uploadifyFileName.substring(index);
		}else {
			newFileName=Long.toString(now);
		}
		BufferedOutputStream bos=null;
		BufferedInputStream bis=null;
		
		//读取保存在临时目录下的上传文件,写入到新的文件中
		try {
			FileInputStream fis=new FileInputStream(uploadify);
			bis=new BufferedInputStream(fis);
			
			FileOutputStream fos=new FileOutputStream(new File(dir,newFileName));
			bos=new BufferedOutputStream(fos);
			
			byte [] buf=new byte[4096];
			int len=-1;
			while ((len=bis.read(buf))!=-1) {
				bos.write(buf,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (null!=bis) {
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (null!=bos) {
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		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;
	}

	public String getUploadifyContentType() {
		return uploadifyContentType;
	}

	public void setUploadifyContentType(String uploadifyContentType) {
		this.uploadifyContentType = uploadifyContentType;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}
	
	
}

 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="default" namespace="/" extends="struts-default">
		<action name="upload" class="com.struts2AndUploadify.action.UploadAction">
			<param name="uploadDir">uploadFiles</param>
		</action>
	</package>
</struts>    

 如有需要请下载附件:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值