`
yinghuayu1324117
  • 浏览: 67327 次
  • 性别: Icon_minigender_2
  • 来自: 保定
文章分类
社区版块
存档分类

Struts2学习笔记--上传下载

阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics