您的位置:首页 > 编程语言 > Java开发

使用Uploadify插件在struts2中实现多文件上传示例

2016-03-18 09:43 603 查看
使用到的版本为:struts2.1 + uploadify3.2 + swfobject2.2

package com.ssh.web.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

/**
* 文件上传Action
* @author dzm
*/
public class UploadFileAction extends BaseAction {

/**
*
*/
private static final long serialVersionUID = 7405040279951251816L;

// 封装上传文件域
private File[] fileName;
// 封装上传文件类型
private String[] fileNameContentType;
// 封装上传文件名
private String[] fileNameFileName;
// 文件保存路径
private String savePath;

@SuppressWarnings("deprecation")
public String uploadFile() throws Exception {
File dir=new File(getSavePath());
if(!dir.exists()){
dir.mkdirs();
}
String path = ServletActionContext.getRequest().getRealPath(savePath);
for (int i = 0; i < fileName.length; i++) {
FileOutputStream fos = new FileOutputStream(path + "\\" + this.fileNameFileName[i]);
FileInputStream fin = new FileInputStream(fileName[i]);
byte[] buff = new byte[1024];
int len = 0;
while ((len = fin.read(buff)) > 0) {
fos.write(buff, 0, len);
}
}
this.getResponse().getWriter().write("ok!!");
return null;
}

public File[] getFileName() {
return fileName;
}

public void setFileName(File[] fileName) {
this.fileName = fileName;
}

public String[] getFileNameContentType() {
return fileNameContentType;
}

public void setFileNameContentType(String[] fileNameContentType) {
this.fileNameContentType = fileNameContentType;
}

public String[] getFileNameFileName() {
return fileNameFileName;
}

public void setFileNameFileName(String[] fileNameFileName) {
this.fileNameFileName = fileNameFileName;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

public String getSavePath() {
return savePath;
}

}


package com.ssh.web.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

/**
* 文件下载Action
* @author dzm
*/
public class DownloadFileAction extends BaseAction {

/**
*
*/
private static final long serialVersionUID = 6072847986960679635L;

private String fileName;// 文件名
private String loadPath;// 文件路径

public String downFile() throws Exception {
String path = ServletActionContext.getRequest().getRealPath(loadPath);
InputStream fin = new FileInputStream(path + "\\" + fileName);
HttpServletResponse response = this.getResponse();
response.reset();
response.setContentType("application/x-msdownload;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
OutputStream out = response.getOutputStream();
int len = 0;
byte[] buff = new byte[1024];
while ((len = fin.read(buff)) > 0) {
out.write(buff, 0, len);
}
out.flush();
fin.close();

return null;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getLoadPath() {
return loadPath;
}

public void setLoadPath(String loadPath) {
this.loadPath = loadPath;
}

}


<%@ page language="java" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>使用uploadify上传文件</title>

<link href="${pageContext.request.contextPath }/js/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/uploadify/swfobject.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/uploadify/jquery.uploadify.min.js"></script>

<style type="text/css">
#file_queue {
background-color: #FFF;
border-radius: 3px;
box-shadow: 0 1px 3px rgba(0,0,0,0.25);
height: 103px;
margin-bottom: 10px;
overflow: auto;
padding: 5px 10px;
width: 300px;
}
</style>

<script type="text/javascript">
$(function(){

$("#upload_file").uploadify({

"swf":"${pageContext.request.contextPath }/js/uploadify/uploadify.swf",
"uploader" : "${pageContext.request.contextPath }/upload/uploadify.action",

"queueID" : "file_queue", //设置一个拥有唯一ID的DOM元素来作为显示上传队列的容器
"fileObjName" : "fileName", //定义上传数据处理文件中接收数据使用的文件对象名
"auto"  : false, //是否自动开始
"multi": true, //是否支持多文件上传
"buttonText": "选择文件", //按钮上的文字
"simUploadLimit" : 1, //一次同步上传的文件数目

"fileSizeLimit" : "100KB",//限制的大小要转换成KB否则容易报错,这里是总共的大小,不是单个文件的
"queueSizeLimit" : 2, //队列中同时存在的文件个数限制
"fileTypeDesc": "Image Files", //如果配置了以下的'fileTypeExts'属性,那么这个属性是必须的
"fileTypeExts": "*.jpg;*.png",//允许的格式

"onUploadSuccess": function (file, data, response) {
alert("文件:" + file.name + "上传成功....服务器信息"+data);
$("#download_file").append("<a href='${pageContext.request.contextPath }/upload/download.action?fileName="+file.name+"'>"+file.name+"</a><br/>");
},
"onUploadError": function(file, errorCode, errorMsg, errorString) {
alert("文件:" + file.name + "上传失败...."+errorCode+"...."+errorMsg+"...."+errorString);
},
"onCancel": function(file){
alert("The file " + file.name + " was cancelled.");
},
"onClearQueue" : function(queueItemCount) {
alert("队列中" + queueItemCount + " 个文件被清空。");
}
});

});

</script>

</head>

<body>
<div id="file_queue"></div>
<input type="file" name="fileName" id="upload_file" />

<p>

<a href="javascript:$('#upload_file').uploadify('upload','*');" id="btn1">开始上传</a> 

<a href="javascript:$('#upload_file').uploadify('cancel','*');;" id="btn2">清空队列</a>

</p>
<div id="download_file">
</div>

</body>
</html>


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>

<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />

<package name="upload" namespace="/upload" extends="struts-default">
<interceptors>
<!-- 对上传文件进行过滤 -->
<interceptor-stack name="uploadPermitStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/png</param>
<param name="maximumSize">102400</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="uploadify" class="com.ssh.web.controller.UploadFileAction" method="uploadFile">
<interceptor-ref name="uploadPermitStack" />
<param name="savePath">/upload/test</param>
<result name="input">/error.jsp</result>
</action>
<action name="download" class="com.ssh.web.controller.DownloadFileAction" method="downFile">
<param name="loadPath">/upload/test</param>
</action>
</package>

</struts>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: