《struts2权威指南》学习笔记之struts2多文件上传--使用数组方式
来源:互联网 发布:代县数据库工程师招聘 编辑:程序博客网 时间:2024/05/02 00:33
上传页面:
<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%...@taglib prefix="s" uri="/struts-tags"%>
<%...@ page isELIgnored="false" %>
<%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
<s:fielderror/>
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="text" name="title" /><br>
<input type="file" name="upload" /><br>
<input type="file" name="upload" /><br>
<input type="file" name="upload" /><br>
<input value="upload" type="submit" />
</form>
</body>
</html>
<%...@taglib prefix="s" uri="/struts-tags"%>
<%...@ page isELIgnored="false" %>
<%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
<s:fielderror/>
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="text" name="title" /><br>
<input type="file" name="upload" /><br>
<input type="file" name="upload" /><br>
<input type="file" name="upload" /><br>
<input value="upload" type="submit" />
</form>
</body>
</html>
上传Action
package lee;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
/** *//**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class UploadAction extends ActionSupport
...{
private String title;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
...{
this.savePath = value;
}
private String getSavePath() throws Exception
...{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) ...{
this.title = title;
}
public File[] getUpload() ...{
return upload;
}
public void setUpload(File[] upload) ...{
this.upload = upload;
}
public String[] getUploadContentType() ...{
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public String[] getUploadFileName() ...{
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return title;
}
@Override
public String execute() throws Exception
...{
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
File[] files=this.getUpload();
for(int i=0;i<files.length;i++)...{
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "/" + getUploadFileName()[i]);
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
...{
fos.write(buffer , 0 , len);
}
}
return SUCCESS;
}
}
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
/** *//**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class UploadAction extends ActionSupport
...{
private String title;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
...{
this.savePath = value;
}
private String getSavePath() throws Exception
...{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) ...{
this.title = title;
}
public File[] getUpload() ...{
return upload;
}
public void setUpload(File[] upload) ...{
this.upload = upload;
}
public String[] getUploadContentType() ...{
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public String[] getUploadFileName() ...{
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return title;
}
@Override
public String execute() throws Exception
...{
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
File[] files=this.getUpload();
for(int i=0;i<files.length;i++)...{
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "/" + getUploadFileName()[i]);
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
...{
fos.write(buffer , 0 , len);
}
}
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>
<param name="maximumSize">2000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>
<param name="maximumSize">2000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
结果显示页面:
<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%...@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value=" + title"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName[0]"/>"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName[1]"/>"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName[2]"/>"/><br>
</body>
</html>
<%...@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value=" + title"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName[0]"/>"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName[1]"/>"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName[2]"/>"/><br>
</body>
</html>
- 《struts2权威指南》学习笔记之struts2多文件上传--使用数组方式
- 《struts2权威指南》学习笔记之struts2文件上传
- 《struts2权威指南》学习笔记之struts2文件上传
- 《struts2权威指南》学习笔记之struts2文件上传--使用fileUpload拦截器
- 《struts2权威指南》学习笔记之struts2之文件下载
- 《struts2权威指南》学习笔记之struts2表单标签
- 《struts2权威指南》学习笔记之struts2 ajax动态表单
- 《struts2权威指南》学习笔记之struts2 pub-sub模型
- 《struts2权威指南》学习笔记之struts2 ajax标签
- 《struts2权威指南》学习笔记之struts2整合Spring
- 《struts2权威指南》学习笔记之struts2整合jsf
- 《struts2权威指南》学习笔记之在Freemarker中使用struts2标签
- 《struts2权威指南》学习笔记之使用拦截器完成权限控制
- 《struts2权威指南》学习笔记之struts2之Tree标签改进
- 《struts2权威指南》学习笔记之struts2 ajax标签之autocompleter
- 《struts2权威指南》学习笔记之struts2 ajax标签之tabbedPanel
- struts2学习笔记之文件上传
- Struts2学习笔记之文件上传
- JSF调试利器---phase listener
- [ASP.NET] C#的前途如何?
- [转]C/C++ 误区四:char c = getchar();
- Java对象序列化
- 欢迎大家加入"数据恢复技术"QQ群:30481379, “数据恢复群”
- 《struts2权威指南》学习笔记之struts2多文件上传--使用数组方式
- [转]C/C++ 误区五:检查 new 的返回值
- 利用STRUTS实现国际化支持
- MessageClient
- 计算机基础知识 [比赛用]
- 用PyLucene实现本地文件名、目录名全文索引
- C中常用宏定义
- 用ASP.NET 2.0设计网络在线投票系统
- ColumnManager