Struts2文件上传
来源:互联网 发布:linux 播放器剪辑 编辑:程序博客网 时间:2024/05/01 16:32
转帖地址http://www.blogjava.net/sxyx2008/archive/2009/10/22/299285.html
Struts2文件上传
大家都知道struts2对文件上传做了很好的封装,使文件上传不再那么恐怖。这里面struts2的文件上传主要依赖的是org.apache.struts2.interceptor.FileUploadInterceptor这个拦截器
关于这个拦截器我不想做过多的研究,这里主要讨论的是该拦截器里面定义的几个关于文件上传的重要属性。
protected Long maximumSize; //允许上传单个文件的大小单位为字节
protected String allowedTypes; //允许上传的文件类型详见tomcat中web.xml文件
protected Set allowedTypesSet;
//允许上传的文件类型Set集合详见tomcat中web.xml文件
allowedTypes与allowedTypesSet属性如有多个值之间用逗号隔开
以上的属性主要配置在struts.xml中对应文件上传Action的拦截器中
示列:
<!-- 文件上传Action -->
<action name="Upload" class="UploadAction">
<result name="success">success.jsp</result>
<result name="input">upload.jsp</result>
<interceptor-ref name="fileUpload">
//设置允许上传单个文件的大小单位为字节
<param name="maximumSize">102400</param>
//允许上传的文件类型详见tomcat中web.xml文件
<param name="allowedTypesSet">application/msword</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
注意:要使用文件上传功能我们必须显实的在对应文件上传的Action中指定<interceptor-ref name="fileUpload">这个拦截器及在该拦截器中配置我们需要的参数,最后别忘了附加上<interceptor-ref name="defaultStack"/>这个默认的拦截器,这样才能真正发挥struts2的文件上传功能。
示列代码:
Action中的示列代码
publicclass FileUploadAction extends ActionSupport {
private File file;
private String fileContentType;
private String fileFileName;
private String memo;
@Override
public String execute() throws Exception {
String path=ServletActionContext.getRequest().getRealPath("/upload");
if(file==null)
{
this.addFieldError("file", "文件不能为空,请选择");
returnINPUT;
}else
{
InputStream is=new FileInputStream(this.getFile());
OutputStream os=new FileOutputStream(newFile(path,this.getFileFileName()));
byte[] buf=newbyte[1024];
int length=0;
while((length=is.read(buf))>0)
{
os.write(buf, 0, length);
}
is.close();
os.close();
}
returnSUCCESS;
}
public File getFile() {
returnfile;
}
publicvoid setFile(File file) {
this.file = file;
}
public String getFileContentType() {
returnfileContentType;
}
public String getFileFileName() {
returnfileFileName;
}
public String getMemo() {
returnmemo;
}
publicvoid setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
publicvoid setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
publicvoid setMemo(String memo) {
this.memo = memo;
}
}
jsp中的示列代码
<s:form action="Upload" enctype="multipart/form-data" method="post">
<s:file name="file"></s:file>
<s:textfield name="memo"></s:textfield>
<s:submit></s:submit>
</s:form>
struts.xml中的示列代码
<action name="Upload" class="UploadAction">
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">102400</param>
<param name="allowedTypes">application/msword</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
注意:
a) 设置文件上传属性在Action中对应的类型的java.io.File;
b) 设置文件上传表单的enctype="multipart/form-data" method="post"
private File file;
private String fileContentType;
private String fileFileName;
c) 红色标注的与文件上传表单中文件上传属性的name一致
d) 蓝色的为固定写法
e) 对应Action中拦截器的配置
<interceptor-ref name="fileUpload">
<param name="maximumSize">102400</param>
<param name="allowedTypes">application/msword</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
Struts2多文件上传示例代码:
jsp上传页面代码:
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function clickme()
{
var divs=document.getElementById('mydiv');
var br=document.createElement('br');
var input=document.createElement('input');
input.type="file";
input.name="file"
var button=document.createElement('input');
button.type="button";
button.value="remove";
divs.appendChild(br);
divs.appendChild(input);
divs.appendChild(button);
button.onclick=function()
{
divs.removeChild(br);
divs.removeChild(input);
divs.removeChild(button);
}
}
</script>
<body>
<h1>Struts2</h1>
<s:fielderror name="file"/>
<s:form action="Upload" enctype="multipart/form-data" method="post">
<s:file name="file" theme="simple"></s:file>
<input type="button" value="Add More" onclick="clickme()"/>
<div id="mydiv">
</div>
<s:textfield name="memo"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
Actio中代码:用户自己写代码来判断文件类型及文件大小
package com.struts2.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class FileUploadAction extends ActionSupport {
private List<File> file;
private List<String> fileContentType;
private List<String> fileFileName;
private String memo;
@Override
public String execute() throws Exception {
String path=ServletActionContext.getRequest().getRealPath("/upload");
if(file==null)
{
this.addFieldError("file", "文件不能为空,请选择");
return INPUT;
}else
{
InputStream is = null;
OutputStream os = null;
File f=null;
long fileSize=0;
String ext="";
for (int i = 0; i < file.size(); i++) {
ext=fileFileName.get(i).substring(fileFileName.get(i).lastIndexOf(".")+1);
f=this.getFile().get(i);
fileSize=f.length();
System.out.println("fileSize:"+fileSize);
System.out.println("ext:"+ext);
if("exe".equals(ext)||"jar".equals(ext)||"bat".equals(ext)||"msi".equals(ext))
{
this.addFieldError("file", "the file is not allowed");
return INPUT;
}
if(fileSize>102000)
{
this.addFieldError("file", "the file is too large");
return INPUT;
}
is=new FileInputStream(f);
os=new FileOutputStream(new File(path,this.getFileFileName().get(i)));
byte[] buf=new byte[1024];
int length=0;
while((length=is.read(buf))>0)
{
os.write(buf, 0, length);
}
}
is.close();
os.close();
}
return SUCCESS;
}
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
}
Struts.xml中配置代码:
<!-- 文件上传Action -->
<action name="Upload" class=" com.struts2.action.FileUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<!--
<param name="maximumSize">102400</param>
<param name="allowedTypes">application/msword</param>
-->
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- C++判断字符与数字,按下esc键退出esc
- 第三次自考总结——时间管理和学习知识
- java集合练习笔记
- 真实网络环境下使用易邮搭建网络邮件服务器并测试通过
- 关于点击电源按钮的时候出现了这情况Transport (VMDB) error -44: Message.
- Struts2文件上传
- HDOJ 4474 Yet Another Multiple Problem(BFS + 剪枝)
- C# 委托的发展 delegate, anonymous method and lambda
- CF#272 (Div. 2) B.(dfs)
- 关于struts2的注解
- echoprint
- 常用url
- 堆排序(C语言版)
- 使用zTree控件制作的表格形式的树形+数据菜单