使用struts2和uploadify实现多文件上传

action类:

package com.fsti.ssh.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.fsti.ssh.utils.FileUtils;

/**
 * 上传
 * 
 * 
 * @author deniro
 * 
 */
public class UploadAction extends BaseAction {

	static Logger log = Logger.getLogger(UploadAction.class);

	private static final long serialVersionUID = 1L;

	/**
	 * 上传文件域 集合
	 */
	private List<File> files;

	/**
	 * 上传文件名 列表
	 */
	private List<String> filesFileName;

	public List<String> getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(List<String> filesFileName) {
		this.filesFileName = filesFileName;
	}

	public List<String> getFilesContentType() {
		return filesContentType;
	}

	public void setFilesContentType(List<String> filesContentType) {
		this.filesContentType = filesContentType;
	}

	/**
	 * 上传文件类型 列表
	 */
	private List<String> filesContentType;

	/**
	 * 保存路径
	 */
	private static String savePath="/upload";

	public String getSavePath() throws Exception{
		return ServletActionContext.getServletContext().getRealPath("/WEB-INF/") + savePath;
	}


	

	public List<File> getFiles() {
		return files;
	}

	public void setFiles(List<File> files) {
		this.files = files;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() {

		log.info("filesFileName:" + filesFileName);
		log.info("filesContentType:"+filesContentType);
		
		try {
			int i=0;
			for (File file : getFiles()) {
				//文件名形如:ID.文件后缀
				FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
						+ UUID.randomUUID().toString()+"."+FileUtils.getExtensionName(filesFileName.get(i)));
				FileInputStream fis = new FileInputStream(file);
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
				fos.close();
				fis.close();
				i++;
			}
		} catch (Exception e) {
			log.error("保存",e);
		}
		
		

		return null;
	}

}

jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<h2 class="contentTitle">多文件上传</h2>


<style type="text/css" media="screen">
.my-uploadify-button {
	background:none;
	border: none;
	text-shadow: none;
	border-radius:0;
}

.uploadify:hover .my-uploadify-button {
	background:none;
	border: none;
}

.fileQueue {
	width: 400px;
	height: 150px;
	overflow: auto;
	border: 1px solid #E5E5E5;
	margin-bottom: 10px;
}
</style>

<div class="pageContent" style="margin: 0 10px" layoutH="50">
		<h2>单个按钮方式:</h2>
		<div class="divider"></div>
		<input id="testFileInput" type="file" name="files" 
		uploaderOption="{
			swf:'lib/dwz/uploadify/scripts/uploadify.swf',
			uploader:'upload!save.action',
			formData:{},
			buttonText:'请选择文件',
			fileSizeLimit:'2000KB',
			fileTypeDesc:'*.jpg;*.jpeg;*.gif;*.png;',
			fileTypeExts:'*.jpg;*.jpeg;*.gif;*.png;',
			auto:true,
			multi:true,
			onQueueComplete:uploadifyQueueComplete
		}"
	/>

	<div class="divider"></div>
	<h2>多个按钮方式:</h2>
	<div class="divider"></div>
	<input id="testFileInput2" type="file" name="files" 
		uploaderOption="{
			swf:'lib/dwz/uploadify/scripts/uploadify.swf',
			uploader:'upload!save.action',
			formData:{},
			queueID:'fileQueue',
			buttonImage:'lib/dwz/uploadify/img/add.jpg',
			buttonClass:'my-uploadify-button',
			width:102,
			auto:false
		}"
	/>
	
	<div id="fileQueue" class="fileQueue"></div>
	
	<input type="image" src="lib/dwz/uploadify/img/upload.jpg" οnclick="$('#testFileInput2').uploadify('upload', '*');"/>
	<input type="image" src="lib/dwz/uploadify/img/cancel.jpg" οnclick="$('#testFileInput2').uploadify('cancel', '*');"/>


	<div class="divider"></div>
	<p style="margin:10px"><a href="http://www.uploadify.com/documentation/" target="_blank">Uploadify 在线文档</a></p>
	


</div>


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值