package cn.ysu.zhy.devmanager.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
@Controller("upLoadAction")
public class UpLoadAction extends BaseAction {
/**
* 1.做文件上传必须得三个参数:file,fileFileName,fileContentType
*/
/**
* 对应jsp的file标签,表示上传的文件(多文件用集合表示)
*/
private List<File> file;
/**
* 文件名称
*/
private List<String> fileFileName;
/**
* 文件的类型
*/
private List<String> fileContentType;
/**
* 2.下载的文件名
*/
private String fileName;
private String fileUploadErrorMessage;
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;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* fileUpload拦截器只是当文件上传到服务器上之后,才进行的文件类型和大小判断。在Action中如果我们什么操作都不做的话,
* 异常就会展现在用户的面前,因此想了一个方法,把该异常信息设置为Action级别的错误信息。即重写addActionError方法。
*/
// @Override
// public void addActionError(String anErrorMessage) {
// // 这里要先判断一下,是我们要替换的错误,才处理
// if
// (anErrorMessage.startsWith("the request was rejected because its size"))
// {
// Pattern pattern = Pattern.compile("\\d+");
// Matcher m = pattern.matcher(anErrorMessage);
// // 匹配一次
// m.find();
// String s1 = m.group();// 上传的文件大小
// // 再匹配一次
// m.find();
// String s2 = m.group();// 所限制的大小
// if (!s1.equals("") && !s2.equals("")) {
// fileUploadErrorMessage = "你上传的文件大小(" + Long.valueOf(s1) / 1024 / 1024 +
// "M)超过允许的大小(" + Long.valueOf(s2) / 1024 / 1024 + "M)";
// request.setAttribute("fileUploadErrorMessage", "文件太大,超过了允许的大小(" +
// Long.valueOf(s2) / 1024 / 1024 + "M),上传失败!");
// // 将信息替换掉
// super.addActionError(fileUploadErrorMessage);
// }
// } else {// 否则按原来的方法处理
// super.addActionError(anErrorMessage);
// }
//
// }
/**
* @Name: home
* @Description: 跳转到上传首页upload.jsp
* @Author: ZHY(作者)
* @Version: V1.00 (版本号)
* @Create Date: 2016-10-07 (创建日期)
* @Parameters: 无
* @Return: String 跳转到上传首页upload.jsp
*/
public String home() {
return "home";
}
/**
* @throws Exception
* @Name: upload
* @Description: 处理文件的上传
* @Author: ZHY(作者)
* @Version: V1.00 (版本号)
* @Create Date: 2016-10-07 (创建日期)
* @Parameters: 无
* @Return: String 上传成功,重定向到upload.jsp
*/
public String upload() throws Exception {
// 1.获取上传的文件路径
File filePath = new File(ServletActionContext.getServletContext().getRealPath("myUploadFile"));
// 2.判断目录是否存在,如果不存在创建文件夹
if (!filePath.exists()) {
filePath.mkdir();// 创建目录
}
System.out.println(filePath);
if (file != null && file.size() > 0 && fileFileName != null && fileFileName.size() > 0) {
for (int i = 0; i < file.size(); i++) {
// 3.声明文件的输入流,为输入流指定路径
FileInputStream is = new FileInputStream(file.get(i));
// 获取上传文件的大小(单位是字节1257114280--是1.17G),做数据狭小的限制,如果大于209715200,也就是200M,return
// error,提示上传文件大小不能超过200M,2097152--2M
int length = is.available();// 获取上传文件的字节大小
// 上传的文件超过200M
if (length >= 209715200) {
request.setAttribute("error", "您上传的文件超过200M");
return "error";
}
// 4.获取文件输出流,声明输出流的地址已及名称
FileOutputStream out = new FileOutputStream(filePath + "\\" + fileFileName.get(i));
try {
// 5.流对接
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) > 0) {
// 将buffer里的二进制数,从0开始,每次写len这么长,写入输出流中
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
out.close();
}
}
}
request.setAttribute("fileName", fileFileName);
return "upload";
}
/**
* @Name: upload
* @Description: 处理文件的下载
* @Author: ZHY(作者)
* @Version: V1.00 (版本号)
* @Create Date: 2016-10-07 (创建日期)
* @Parameters: 无
* @Return: String 下载成功,重定向到upload.jsp
*/
public String download() {
return "download";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
struts2文件上传与下载源码-包含超出指定文件大小提示
共84个文件
jar:49个
xml:8个
java:7个
需积分: 33 24 下载量 132 浏览量
2016-10-17
16:01:54
上传
评论
收藏 23.21MB RAR 举报
温馨提示
struts2文件的上传与下载,包含超出指定文件大小之后的提示。更多详细内容,请参考博客:http://blog.csdn.net/qq_20889581/article/details/52838848
资源推荐
资源详情
资源评论
收起资源包目录
updownload-demo.rar (84个子文件)
updownload-demo
config
beans.xml 4KB
struts.xml 3KB
hibernate.cfg.xml 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 414B
org.eclipse.core.resources.prefs 92B
org.eclipse.wst.common.component 581B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 522B
org.eclipse.jdt.core.prefs 364B
src
cn
ysu
zhy
devmanager
dao
ICommonDao.java 194B
impl
CommonDaoImpl.java 254B
action
DownLoadAction.java 2KB
BaseAction.java 826B
UpLoadAction.java 5KB
domain
service
IService.java 164B
impl
ServiceImpl.java 330B
utils
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 1KB
page
upload.jsp 2KB
download.jsp 1KB
classes
beans.xml 4KB
cn
ysu
zhy
devmanager
dao
ICommonDao.class 190B
impl
CommonDaoImpl.class 665B
action
DownLoadAction.class 2KB
UpLoadAction.class 4KB
BaseAction.class 877B
domain
service
IService.class 239B
impl
ServiceImpl.class 597B
utils
struts.xml 3KB
hibernate.cfg.xml 1KB
lib
freemarker-2.3.15.jar 859KB
commons-collections-3.1.jar 546KB
hibernate-annotations.jar 273KB
com.springsource.org.apache.commons.logging-1.1.1.jar 60KB
org.springframework.asm-3.0.2.RELEASE.jar 52KB
ehcache-1.5.0.jar 258KB
org.springframework.core-3.0.2.RELEASE.jar 350KB
org.springframework.transaction-3.0.2.RELEASE.jar 226KB
dom4j-1.6.1.jar 307KB
com.springsource.org.apache.commons.beanutils-1.8.0.jar 228KB
org.springframework.expression-3.0.2.RELEASE.jar 153KB
com.springsource.org.apache.commons.pool-1.5.3.jar 95KB
org.springframework.aop-3.0.2.RELEASE.jar 313KB
backport-util-concurrent.jar 320KB
standard.jar 384KB
jcommon-1.0.16.jar 302KB
cglib-2.2.jar 272KB
com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar 8.43MB
org.springframework.jdbc-3.0.2.RELEASE.jar 369KB
com.springsource.org.apache.log4j-1.2.15.jar 387KB
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar 120KB
ognl-2.7.3.jar 234KB
mysql-connector-java-5.1.10-bin.jar 707KB
com.springsource.org.aopalliance-1.0.0.jar 5KB
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar 603KB
org.springframework.context-3.0.2.RELEASE.jar 642KB
slf4j-log4j12.jar 8KB
jstl.jar 20KB
javassist-3.9.0.GA.jar 583KB
commons-logging.jar 59KB
hibernate-commons-annotations.jar 65KB
org.springframework.aspects-3.0.2.RELEASE.jar 29KB
struts2-spring-plugin-2.1.8.1.jar 16KB
com.springsource.javax.annotation-1.0.0.jar 6KB
xwork-core-2.1.6.jar 1.49MB
commons-fileupload-1.2.1.jar 56KB
FCKeditor-2.3.jar 18KB
slf4j-api-1.5.8.jar 23KB
struts2-core-2.1.8.1.jar 738KB
com.springsource.org.junit-4.7.0.jar 231KB
com.springsource.net.sf.cglib-2.2.0.jar 319KB
org.springframework.web-3.0.2.RELEASE.jar 374KB
commons-io-1.3.2.jar 86KB
jta-1.1.jar 11KB
commons-lang.jar 237KB
antlr-2.7.6.jar 433KB
org.springframework.beans-3.0.2.RELEASE.jar 537KB
org.springframework.orm-3.0.2.RELEASE.jar 325KB
hibernate3.jar 3.75MB
index.jsp 834B
error.jsp 892B
.classpath 1KB
共 84 条
- 1
资源评论
qq_20889581
- 粉丝: 190
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功