struts2之多文件上传与拦截器(8)
前台jsp
<s:form action="uploadAction" enctype="multipart/form-data" method="post">
<label>上传文件:</label><br/>
<s:file name="myfiles"></s:file> <br/>
<s:file name="myfiles"></s:file> <br/>
<s:file name="myfiles"></s:file> <br/>
<s:file name="myfiles"></s:file> <br/>
<s:submit value="提交"></s:submit>
</s:form>
action
public class UploadAction extends ActionSupport {
//三个全局属性注意命名规则,属性名的前半部分保持一致,不然报空值
//上传的文件(旧文件)
private File[] myfiles;
//上传的文件名(旧文件)
private String[] myfilesFileName;
//上传文件类型(旧文件)
private String[] myfilesContentType;
//封装上传方法
public void copy(File myfile,String myfileFileName,String path){
//生成新的文件名(使用uuid)
String newmyfilename = UUIDUtil.getUUID()+myfileFileName.substring(myfileFileName.lastIndexOf("."));
//上传文件的位置
String filepath = path+File.pathSeparator+newmyfilename;
System.out.println("filepath = "+filepath);
//构建新文件
File newfile = new File(filepath);
//读入写出 从旧文件读内容到新文件
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//将旧文件封装到输入流
fis = new FileInputStream(myfile);
//将新文件封装到输出流
fos = new FileOutputStream(newfile);
//设置一个字节数组缓冲内容
byte [] bt = new byte[1024];
int len = 0;
/**
* 循环读取缓冲区的内容
* 输入流不断的将字节读入到缓冲区(旧文件到缓冲区)
* 输出流不断的将字节写出到新文件(缓冲区到新文件)
*/
while((len = fis.read(bt))!=-1){
fos.write(bt, 0, len);
}
fos.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//处理上传请求
public String upload(){
//指定上传的位置(因为只有一个,所以不用再循环)
String path = ServletActionContext.getServletContext().getRealPath("upload");
for (int i = 0; i < myfiles.length; i++) {
copy(myfiles[i], myfilesFileName[i], path);
}
return SUCCESS;
}
public File[] getMyfiles() {
return myfiles;
}
public void setMyfiles(File[] myfiles) {
this.myfiles = myfiles;
}
public String[] getMyfilesFileName() {
return myfilesFileName;
}
public void setMyfilesFileName(String[] myfilesFileName) {
this.myfilesFileName = myfilesFileName;
}
public String[] getMyfilesContentType() {
return myfilesContentType;
}
public void setMyfilesContentType(String[] myfilesContentType) {
this.myfilesContentType = myfilesContentType;
}
}
struts.xml
<!-- struts2中文件上传拦截
struts2 的核心包下的default.properties文件里有默认的大小为struts.multipart.maxSize=2097152,也就是2M. 这是struts2默认拦截,
解决方法:在struts.xml配置文件中,添加
<constant name="struts.multipart.maxSize" value="10485760"/>
这里的value单位为B,即10485760B = 100MB。
-->
<constant name="struts.multipart.maxSize" value="104857600"/>
<!-- 该常量用于读取国际化文件
name表示国际化资源
value表示国际化文件所在位置,
注意:国际化文件不要写后缀 -->
<constant name="struts.custom.i18n.resources" value="com.oak.action.myUpload"></constant>
<package name="upload" namespace="/" extends="struts-default">
<action name="uploadAction" class="com.oak.action.UploadAction" method="upload">
<!-- fileUpload拦截器是系统拦截器,只需要引用,单词是固定的 -->
<interceptor-ref name="fileUpload">
<!-- 允许用户上传文件的大小,单位是字节 10M -->
<param name="maximumSize">10485760</param>
<!-- 允许用户上传文件的扩展名,如果不设置,则不受限,多个可以以逗号分隔 -->
<param name="allowedExtensions">.jpg,.txt,.jsp</param>
</interceptor-ref>
<!--还需要引用系统默认的拦截器-->
<interceptor-ref name="validationWorkflowStack"></interceptor-ref>
<interceptor-ref name="basicStack"></interceptor-ref>
<result>
/welcome.jsp
</result>
<!-- 用于输出错误信息到页面 -->
<result name="input">
/upload.jsp
</result>
</action>
</package>

struts2之多文件上传与拦截器(8)的更多相关文章
- struts2 基础3 文件上传、拦截器
文件上传: 1.将头设置为enctype=”multipart/form-data” <form action="${pageContext.request.contextPath } ...
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2 spring-mv ...
- springmvc文件上传和拦截器
文件上传 用到这两个包 配置视图解析器:springmvc配置文件配置 <!-- id必须要是"multipartResolver" --> <bean id=& ...
- 2017/2/12:springMVC的简单文件上传跟拦截器
1.写文件上传的界面jsp代码如下重点为文件上传标签的类型 2.写登录成功跟失败的界面:成功自己写 3.写springMVC的文件上传的controller的方法 4.最后一步配置spring-ser ...
- struts2 笔记02 文件上传、文件下载、类型转换器、国际化的支持
Struts2的上传 1. Struts2默认采用了apache commons-fileupload 2. Struts2支持三种类型的上传组件 3. 需要引入commons-fileupload ...
- [转]Struts2多个文件上传
转载至:http://blog.csdn.net/hanxiaoshuang123/article/details/7342091 Struts2多个文件上传多个文件上传分为List集合和数组,下面我 ...
- Struts2学习总结——文件上传与下载
Struts2文件上传与下载 1.1.1新建一个Maven项目(demo02) 在此添加Web构面以及 struts2 构面 1.2.1配置Maven依赖(pom.xml 文件) <?xml v ...
- Struts2 之 实现文件上传和下载
Struts2 之 实现文件上传和下载 必须要引入的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 01.文件上传需要分别在struts.xm ...
- 笨鸟先飞之Java(一)--使用struts2框架实现文件上传
无论是.net还是Java,我们最常接触到的就是文件的上传和下载功能,在Java里要实现这两个经常使用功能会有非常多种解决方案,可是struts2的框架却能给我们一个比較简单的方式,以下就一起来看吧: ...
随机推荐
- Nginx反向代理简单配置
一.首先在IIS中部署两个站点,localhost:86 .localhost:5000 二.修改C:\windows\system32\drivers\etc\hosts文件,增加 127.0.0. ...
- STM32PWM之——定时器(1)
定时器功能简介 STM32 一共有 8 个都为 16 位的定时器.其中 TIM6. TIM7 是基本定时器:TIM2.TIM3. TIM4. TIM5 是通用定时器: TIM1 和 TIM8 是高级定 ...
- AndroidStudio下载安装教程(图文教程)
场景 Android Studio 中文社区: http://www.android-studio.org/ 下载安装包,这里选择64位Windows 等待下载完成. 注: 博客: https://b ...
- IDEA配置之tomcat相关配置
1. tomcat起服务时, 日志乱码 -server -XX:PermSize=512M -XX:MaxPermSize=1024m -Dfile.encoding=UTF-8 设置tomcat参数
- DP_Sumsets
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...
- DP+线段树维护矩阵(2019牛客暑期多校训练营(第二场))--MAZE
题意:https://ac.nowcoder.com/acm/contest/882/E 给你01矩阵,有两种操作:1是把一个位置0变1.1变0,2是问你从第一行i开始,到最后一行j有几种走法.你只能 ...
- .Net C# RSA签名和验签
帮助类 using System; using System.Text; using System.IO; using System.Security.Cryptography; namespace ...
- 【原创】大叔问题定位分享(36)openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil
openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil This function returns nil if the request ...
- 编写Dockerfile自定义镜像
要求 编写一个Dockerfile自定义centos镜像,要求在容器内部可以使用vim和ifconfig命令,并且登入落脚点为/usr/local 编写Dockerfile FROM centos M ...
- 如何使用classnames模块库为react动态添加class类样式
摘要 在react中添加动态的css时,传统的方式较为繁琐,今天刚好学习到一个模块库可以便捷的解决这个问题.对的,它就是“classnames”. classnames模块库 npm安装 npm in ...