Struts2学习笔记--上传下载
标签:
===================================
文件上传的原理:
表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:
1) application/x-www-form-urlencoded:这是默认编码方式,它只处理表单域里的value属性值,采用这种编码方式的表单会将表单域的值处理成URL编码方式。
2) multipart/form-data:这种编码方式的表单会以二进制流的方式来处理表单数据,这种编码方式会把文件域指定文件的内容也封装到请求参数里。
3) text/plain:这种方式主要适用于直接通过表单发送邮件的方式。
文件上传是web应用经常用到的一个知识。原理是,通过为表单元素设置enctype=”multipart/form-data”属性,让表单提交的数据以二进制编码的方式提交,在接收此请求的Servlet中用二进制流来获取内容,就可以取得上传文件的内容,从而实现文件的上传。
在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 ([url]http://commons.apache.org/fileupload/[/url]),另一个是Oreilly组织ns-fileupload-1.2.jar和commons-io-1.3.1.jar到lib
===== jsp =====
form 的 enctype 设置为 multipart/form-data
============== UploadAction ==============
private String username;
private String password;
private File file; // 对应文件域
private String fileFileName; // 前面的的COS框架([url]http://www.servlets.com/cos/[/url])。
利用这两个框架都能很方便的实现文件的上传。
==================================
Struts2上传文件增加commoFile属性的名字 + FileName(固定的)
private String fileContent; // 前面的File属性的名字 + Content // setter... getter...
String execute() throws Exception { InputStream is = new FileInputStream( file );
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName());
OutputStream os = new FileOutputStream( destFile );
byte[] buffer = new byte[400];
int length = 0;
while( (length = is.read(buffer)) > 0 ) {
os.write(buffer,0,length);
} is.close();
os.close();
return SUCCESS;
}
================= 中文问题 =================
不过我全部用UTF-8并未遇到中文问题 struts2-core包 struts-default.xml ----拦截器的设置 org.apache.struts2.default.properties ----全局属性的设置 33行 strusts.i18n.encoding=UTF-8 默认UTF-8 可以在struts.xml下进行设置 设置字符集 设置上传文件缓存 其他属性 struts.multipart.parser=jakarta struts2采用那种方式上传 pell cos struts.multipart.maxSize=2097152 默认上传文件最大的请求大小2M struts.action.extension=action 整个url的后缀名
================ 上传多个文件 ================
有两种方式:
1.数组 File[] file 文件
String[] fileFileName 文件名
String[] fileContentType 文件类型
2.集合 List file List fileFileName List fileContentType
-------- action中: --------
String execute() {
for(int i = 0; i < file.size(); i++) {
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName().get(i)); ...
}
return SUCCESS;
}
------ jsp中: ------
多个file时,file的名字要一致,都要叫file,它会自动set到跟file名对应的List中去
======================== 上传任意个文件 ========================
|
------ JS: ------
funcation addMore() {
var td = document.getElementById("more"); //生成一个换行符
var br = document.createElement("br"); //创建一个input组件
var input = document.createElement("input");
var button = document.createElement("input"); //指定类型 为 file 为文件上传
input.type = "file"; //指定组件的名字
input.name = "file";
button.type = "button";
button.value = "删除"; //为删除按钮注册一个事件
button.onclick = function() {
//alert("删除按钮");
//删除一行
td.removeChild(br);
td.removeChild(input);
td.removeChild(button); } //将创建的组件加到
|
td.appendChild(br);
td.appendChild(input);
td.appendChild(button); }
======================= 限制上传类型 ======================= org.apache.struts2.interceptor.FileUploadInterceptor类 Long maximumSize:最大上传大小
---
每一个文件的大小,不是总和 String allowedTypes:允许的类型
------------- struts.xml -------------
/upload.jsp 加入一个上传文件的拦截器并设置其属性 409600 单个上传文件最大不能超过400K ... mime类型,多个用逗号分开
** 加入默认的拦截器
注:后缀可以到tomcat\conf\web.xml中找中的字符串
-------------- upload.jsp --------------
添加
----------------------
更改显示的错误信息
----------------------
org.apache.struts2中 找到struts-messages.properties
-----------------------
上传文件类型不匹配 struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" {2}
-----------------------
上传文件大小超出规定 struts.messages.error.file.too.large=File too large: {0} "{1}" {2}
-----------------------
上传文件出错 struts.messages.error.uploading=Error uploading: {0} 创建一个全局的属性文件 /src/messages.properties struts.messages.error.content.type.not.allowed=不支持上传该类型的文件 struts.messages.error.file.too.large=上传文件过大,请重试 struts.messages.error.uploading=上传文件时发生错误
--------- 国际化 ---------
messages_en_US.properties messages_zh_CN.properties
分享到:
相关推荐
Struts2环境搭建 Struts2类型转换 Struts2输入校验 拦截器 文件的上传与下载 Struts2的国际化 深入探析 struts2与servlet API耦合方法
Struts2学习资料,里面有struts2的异常处理,上传下载,国际化
基于 Struts2.1.8 包括Struts2的基本应用、文件上传、拦截器、输入校验、国际化、OGNL表达式、Struts2标签等内容。
struts2学习笔记和源码,struts2原理。文件上传、i18n等等
NULL 博文链接:https://zhaolianyang.iteye.com/blog/859890
java笔面试题1-6章 Java相关课程系列笔记之一Java学习...Java相关课程系列笔记之十三Struts2学习笔记 Java相关课程系列笔记之十四Hibernate学习笔记 Java相关课程系列笔记之十五Spring学习笔记 资源都已上传,自己下载
\contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...
NULL 博文链接:https://zhaolianyang.iteye.com/blog/883505
NULL 博文链接:https://zhaolianyang.iteye.com/blog/888978
NULL 博文链接:https://zhaolianyang.iteye.com/blog/903956
NULL 博文链接:https://zhaolianyang.iteye.com/blog/890983
Struts 1.3 备忘笔记 【资源特点】 1、以项目形式组织,包含所有的源代码 2、内含详细的注释说明 3、知识点较全面 【内容目录】 01 Struts_01HandworkApply : 手工配置Struct应用程序,演示用户提交数据后服务器的...
多文件上传 博文链接:https://wuzhaohuixy-qq-com.iteye.com/blog/710102
Struts 2.1.8 学习源码内容 Struts2_01FirstDemo : 跑通第一个Struts2的实例 Struts2_02CURD : 关于Struts2的增、删、改和查 实际业务中数据来自数据库,从DAO层查询...Struts2_06FileUpload : Struts2上传文件的使用
我上传的所有资源都是免费的,欢迎愿意学习的同学下载
java(1) 整合入门(spring,struts,hibernate的整合)资料(1) 中大软件工厂项目前培训资料(1) 完整笔记+源码(1) ssh(1) C#查询参数化例子(1) 学习笔记+完整源码(1) 介绍与深入(1) 学习笔记(不含整合)(1) C#(1) .CHM格式...
1-JSP+JDBC_假分页笔记.pdf 2-JSP+JDBC_真分页(基于Oracle数据库分页)笔记.pdf 3-JSP+DAO和MVC+DAO(基于...9-Struts高级部分(1)(解决重复提交、上传组件)笔记.pdf 54留言管理程序_Struts + Spring + Hibernate笔记.pdf
6、SpringMVC和Struts2的区别 第二天 1、高级参数绑定 a)数组类型的参数绑定 b)List类型的绑定 2、@RequestMapping注解的使用 3、Controller方法返回值 4、SpringMVC中异常处理 5、图片上传处理 6、Json数据交互 7...
cxf学习笔记.详细的描述了我学习cxf的过程.服务端采用 cxf+spring的方式;客户端采用spring+struts的方法。内容如下 1:最简单的hello world 2:集合类的传输 3:大数据的传输(上传下载) 4:安全性 4.1:用户令牌...