zoukankan      html  css  js  c++  java
  • struts2文件上传下载

    文件上传

    1.struts2中文件上传介绍

      struts2文件上传需要使用apache提供的文件上传组件(commons-fileupload.jar和commons-io.jar)。

      struts2文件上传的核心是通过fileupload拦截器实现的。

    2.如何实现文件上传

      1>.添加commons-fileupload和commons-io包

      2>.在jsp页面做如下配置

         将form的method属性值设置为post

         给form标记添加属性enctype="multipart/form-data"(让提交的表单数据,以流的形式提交)

      3>.在action类中添加如下属性,并添加get|set方法

         private File xxx;

         private String xxxFileName;

         private String xxxContentType;

         注意:xxx指代<input type="file">文件域的name值

               struts2的文件上传功能,是将客户端上传的文件保存到一个临时目录,我们要做的事情,就是将临时目录中的文件保存到指定目录。

    文件下载

    在struts2中如何实现文件下载


    1.添加一个文件下载的action类

      public class DownloadAction{

         private InputStream logoStream;

         public InputStream getLogoStream(){
            
            try{

                this.fileName = brand.getLogoUrl();
                
                //获取被下载的文件的绝对路径
                String filePath = ServletActionContext.getRequest().getServletContext().getRealPath(brand.getLogoUrl());
                
                System.out.println(filePath);
                
                //读取被下载的文件
                logoStream = new FileInputStream(filePath);
                
                return logoStream;
            
            }catch(Exception ex){
                
                ex.printStackTrace();
                return null;
            }
        }
      }

    2.在该类中添加一个action方法,返回success

      public class DownloadAction{

        public String download(){

          return "success";

        }

      }

    3.在struts.xml中配置该action,并且result的结果类型为stream

      <action name="download" class="...DownloadAction" method="download">
        <result name="success" type="stream">

        </result>
      </action>

    4.在result中设置参数inputName,该参数的值必须为action类中的一个数据类型为InputStream的属性。

      <action name="download" class="...DownloadAction" method="download">
        <result name="success" type="stream">
          <param name="inputName">logoStream</param>
        </result>
      </action>

    5.控制下载文件的文件名称:在result中添加参数:contentDisposition

     
      <action name="download" class="...DownloadAction" method="download">
        <result name="success" type="stream">
          <param name="inputName">logoStream</param>
          <param name="contentDisposition">attachment;filename="${fileName}"</param>
        </result>
      </action>

      注意:${fileName} 中的fileName为action类中的属性

    6.控制文件的类型:在result中添加参数:contentType

      contentType:

         word : application/msword

         excel : application/vnd.ms-excel

         ppt   : application/vnd.ms-powerpoint

         html  : text/html

         文本文件 : text/plain

         可执行文件  :  application/octet-stream

     
      <action name="download" class="...DownloadAction" method="download">
        <result name="success" type="stream">
          <param name="inputName">logoStream</param>
          <param name="contentDisposition">attachment;filename="${fileName}"</param>
          <param name="contentType">application/octet-stream</param>
        </result>
      </action>

  • 相关阅读:
    Validation failed for one or more entities
    sql 存储过程
    SQL Server分页3种方案比拼
    case when 用法
    C#如何计算代码执行时间
    透过 Jet.OLEDB 读取 Excel里面的数据
    DataBinding?资料系结?资料绑定?
    ASP.NET的OutputCache
    我想写程序#3 之 「简单地设计自己的数据表(Table)」
    我想写程序#1 之 「先确立志向」
  • 原文地址:https://www.cnblogs.com/FriendlyMaKe/p/6424708.html
Copyright © 2011-2022 走看看