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的框架却能给我们一个比較简单的方式,以下就一起来看吧: ...
随机推荐
- pytorch API中sgd.py的学习记录
参考:PyTorch与caffe中SGD算法实现的一点小区别 其中公式(3)(4)的符号有问题 变量对应表 程序 参考文章 buf v momentum μ d_p Δf(θ) lr ξ p θ
- axios设置请求头内容
axios设置请求头中的Authorization 和 cookie 信息: GET请求 axios.get(urlString, { headers: { 'Authorization': 'Bea ...
- Linux上面mount 域控的目录 超时 然后提示 error的解决办法
mount error(112): Host is down 故障解决 https://blog.csdn.net/lepton126/article/details/89447713 之前查到过 这 ...
- SQLSever语句(增、删、改、查)一、增:有4种方法1.使用insert插入单行数据
SQL语句(增.删.改.查) 一.增:有4种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:inse ...
- leetCode算法——1TwoSum(两数之和)
描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...
- js中实现输入框类似百度搜索的智能提示效果
说明:我这里显示的数据采用词典(词典在js中自定义的,看下面文字),主要显示key. 页面元素: <style type="text/css">.search { le ...
- Kubernetes---Pod hook
Pod hook(钩子)是由Kubernetes管理的kubelet发起的,当容器中的进程启动前或者容器中的进程终止之前运行,这是包含在容器的生命周期之中.可以同时为Pod中的所有容器都配置 hook ...
- vs code 更改快捷键
选择左下角设置图标,快捷键方式 文件列表修改,搜 list 文件tree list.focusUp -> ctrl+p
- Pygame小游戏练习二
@Python编程从入门到实践 Python项目练习 四.创建Ship类 建立ship.py,创建Ship类,管理飞船行为. # ship.py import pygame class Ship(): ...
- docker 入门5 - 栈 【翻译】
入门,第 5 部分:堆栈 先决条件 安装 Docker 版本 1.13 或更高版本. 获取第 3 部分先决条件中所述的 Docker Compose. 获取 Docker Machine,如第 4 部 ...