《struts2权威指南》学习笔记之struts2文件上传--使用fileUpload拦截器
来源:互联网 发布:淘宝童装女孩冬装4一7 编辑:程序博客网 时间:2024/05/02 04:24
上传页面:
<%...@ 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 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 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;
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 void setUpload(File upload) ...{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return (this.title);
}
public File getUpload() ...{
return (this.upload);
}
public String getUploadContentType() ...{
return (this.uploadContentType);
}
public String getUploadFileName() ...{
return (this.uploadFileName);
}
@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());
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "/" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
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;
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 void setUpload(File upload) ...{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return (this.title);
}
public File getUpload() ...{
return (this.upload);
}
public String getUploadContentType() ...{
return (this.uploadContentType);
}
public String getUploadFileName() ...{
return (this.uploadFileName);
}
@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());
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "/" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
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>
如果需要使用文件上传拦截器来过滤文件大小,或者过滤文件内容,则必须显示配置引用Struts默认的拦截器栈:defaultStack
资源文件中配置文件类型不正确和大小不正确的提示:
struts.messages.error.content.type.not.allowed = 文件类型不正确
struts.messages.error.file.too.large= 文件大小不正确
- 《struts2权威指南》学习笔记之struts2文件上传--使用fileUpload拦截器
- 《struts2权威指南》学习笔记之struts2文件上传
- 《struts2权威指南》学习笔记之struts2文件上传
- 《struts2权威指南》学习笔记之struts2多文件上传--使用数组方式
- 《struts2权威指南》学习笔记之使用拦截器完成权限控制
- 《struts2权威指南》学习笔记之struts2之文件下载
- struts2使用fileUpload拦截器实现文件上传
- Struts2 文件上传fileUpload拦截器不起作用
- Struts2文件上传-fileUpload拦截器
- Struts2文件上传-fileUpload拦截器
- 《struts2权威指南》学习笔记之struts2表单标签
- 《struts2权威指南》学习笔记之struts2 ajax动态表单
- 《struts2权威指南》学习笔记之struts2 pub-sub模型
- 《struts2权威指南》学习笔记之struts2 ajax标签
- 《struts2权威指南》学习笔记之struts2整合Spring
- 《struts2权威指南》学习笔记之struts2整合jsf
- 《struts2权威指南》学习笔记之在Freemarker中使用struts2标签
- Struts2权威指南总结7:拦截器
- 如何得到一个控件相对于屏幕左上角的坐标
- 迷宫(C++)
- Linux下软件的安装
- sql
- 偶然开博
- 《struts2权威指南》学习笔记之struts2文件上传--使用fileUpload拦截器
- linux 下gdb命令一览表
- 回到开发岗位,重新开始使用CSDN
- 实战 Lucene,第 1 部分: 初识 Lucene
- Programming Perl 2
- HTTPS 协议下东东的下载
- c#中资源文件的使用
- CEO、COO、CFO、CTO是什么意思
- 休假结束了