struts批量上传文件
来源:互联网 发布:阿里云 centos 中文 编辑:程序博客网 时间:2024/06/05 14:42
struts.xml的配置
<package name="upload" extends="struts-default">
<!-- 文件上传的拦截器栈 -->
<interceptors>
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload">
<param name="maximumSize">409600</param>
<!-- <param name="allowedTypes">image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg</param> -->
</interceptor-ref>
<interceptor-ref name="basicStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="fileUploadStack"></default-interceptor-ref>
<action name="upload" class="com.zwlc.fpoa.system.information.action.UploadAction">
<result name="success">/default.jsp</result>
<result name="input">/system/upload.jsp</result>
<interceptor-ref name="fileUploadStack"></interceptor-ref>
</action>
</package>
upload.jsp上传页面
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><s:text name="resource" />
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css"
href="<s:property value="%{default_css}"/>" />
<script type="text/javascript">
function addMore()
{
var td = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
var span=document.createElement("span");
input.type = "file";
input.name = "file";
button.type = "button";
button.value = "删除";
button.onclick = function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
td.removeChild(span);
}
input.onchange=function(){
clearTooltip(this);
checkExt(this);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(span);
td.appendChild(button);
}
</script>
</head>
<body>
<table align="center" width="50%">
<tr>
<td>
<s:fielderror cssStyle="color:red" />
</td>
</tr>
</table>
<s:form action="upload.action" theme="simple" enctype="multipart/form-data" method="post">
<table align="center" width="50%" border="1">
<tr>
<td>请选择要上传的文件</td>
<td id="more">
<s:file name="file" onchange="clearTooltip(this);checkExt(this);"></s:file>
<span></span>
<input type="button" value="添加多个" onclick=addMore();>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<s:submit value="确定"></s:submit>
<s:reset value="重置"></s:reset>
</td>
<td>
</td>
</tr>
</table>
</s:form>
</body>
</html>
UploadAction.java文件
package com.zwlc.fpoa.system.information.action;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.zwlc.fpoa.upload.Upload;
public class UploadAction extends ActionSupport {
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception {
Upload upload = new Upload();
// ActionContext ctx = ActionContext.getContext();
// HttpServletRequest request = (HttpServletRequest)
// ctx.get(ServletActionContext.HTTP_REQUEST);
// System.out.println(com.gdf.util.SessionManager.getUser(request).getId());
ArrayList list = upload.upload(file, this.getFileFileName(), "/files/");
if (list.size() > 0) {
return INPUT;
} else {
return SUCCESS;
}
}
}
Upload.java文件
package com.zwlc.fpoa.upload;
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 java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
public class Upload {
@SuppressWarnings("unchecked")
/*
* @file:文件域列表
*
* @fileName:上传文件的文件名
*
* @path:文件上传的目录,相对目录。 说明:文件上传所存放目录的规则:网站根目录下 files/会员ID/网站类型名称/上传文件分类名称/
*
* @返回结果:一个存放上传文件所在位置相对路径的ArrayList
*/
public ArrayList upload(List file, List fileName, String path)
throws IOException {
//String root = ServletActionContext.getRequest().getRealPath(path);
String root = ServletActionContext.getServletContext().getRealPath(path);
File dir = new File(root);
if (dir.exists() == false) {
dir.mkdirs();
}
ArrayList list = new ArrayList();
for (int i = 0; i < file.size(); ++i) {
InputStream is = new FileInputStream(file.get(i).toString());
/*SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");// 格式化时间输出
String Rname = sdf.format(new Date());// 取得当前时间,Date()是java.util包里的,这作为真实名称
name = Rname + name;// 重命名文件名称,命名规则为:时间+原文件名称*/
String name = fileName.get(i).toString();// 得到上传文件的原名称
File destFile = new File(root, name);
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
list.add(path + name);
is.close();
os.close();
}
return list;
}
}
- struts批量上传文件
- struts批量上传文件
- Struts 1.2 批量上传。
- Struts批量上传
- 文件上传,支持批量上传
- 批量上传文件
- 批量文件上传界面
- SWFUpload 批量上传文件
- JSP 批量上传文件
- Struts2批量上传文件
- 批量上传文件
- ssh批量上传文件
- uploadify批量上传文件
- php 批量上传文件
- 批量上传文件
- uploadify批量上传文件
- strtuts2批量文件上传
- 批量上传文件
- ubuntu 安装 flash
- mysql的使用14
- 技术演示----分析听众个人特点
- ZOJ Problem Set - 2812 Quicksum
- 邻接表 小讲
- struts批量上传文件
- 3-1 Point类的构造函数
- stm32的SPI设置步骤,SPI配置参数
- InvSqrt
- 请假模块申请算法(3)
- SDN学习
- ajax实现注册页面动态验证用户名是否已注册,不必提交即可验证
- POJ1721_CARDS【置换群】
- 乐其网络2015校园招聘笔试题——处理汉字字符的问题