天天看點

Struts2實作檔案下載下傳功能第一步:首先需要建立一個ACTION:第二步:配置struts.xml:三:對于struts.xml中幾個标簽的介紹:

                  最近项目中需要做一个开发平台,第一个功能就是实现文档对外提供下载功能,项目中用到的是Struts2框架,因此写了一个简单的ACTION在此记录学习。

第一步:首先需要新建一个ACTION:

import ins.framework.web.Struts2Action;
import org.apache.commons.codec.binary.Base64;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 合作方下载资料平台扩展
 * @author  Tanyunlong on 2016/10/11.
 */
public class OpenPlatformAction extends Struts2Action {

    //下载页面点击跳转参数
    private String index;
    //下载文件名
    private String fileName;
    //输入流
    private InputStream fileStream;



    public String openPlatformMethod(){
         System.out.println("开放平台openPlatformMethod 执行====");

         if ("0".equals(index)){
              return  "success0";//跳转到平台首页
         }else if ("1".equals(index)){
              return "success1";//跳到平台概述
         }else if ("2".equals(index)){
              return "success2";//跳到业务流程
         }else if ("3".equals(index)){
               return "success3";//跳到下载接口文档
         }
         return  null;
    }


     public InputStream download(){

         if ("a".endsWith(fileName)){
             fileName="(通用版).docx";
         }else if ("b".endsWith(fileName)){
             fileName="解答.doc";
         }else if ("c".endsWith(fileName)){
             fileName="webservice调用样例-JAVA.java";
         }else if ("d".endsWith(fileName)){
             fileName="webservice调用样例-PHP.php";
         }else if ("e".endsWith(fileName)){
             fileName="已上线省市区.xls";
         }else if ("f".endsWith(fileName)){
             fileName="全国省市区码表.xls";
         }else if ("g".endsWith(fileName)){
             fileName="合作流程图.vsdx";
         }else {
             return null;
         }
        //获取下载文件路径
        String filenamedownload=getServletContext().getRealPath("/webnew/common/"+fileName);
         //转换文件名编码
         if (getRequest().getHeader("User-Agent").toLowerCase().indexOf("firefox")>0){
             try {
                 fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");
             }catch (UnsupportedEncodingException e){
                     e.printStackTrace();
             }

         }else {
             try{
                 fileName= URLEncoder.encode(fileName,"UTF-8");
             }catch (UnsupportedEncodingException e){
                 e.printStackTrace();
             }
         }


         InputStream fis=null;
         try {
             fis=new FileInputStream(filenamedownload);
         }catch (FileNotFoundException e){
             e.printStackTrace();
         }
            return fis;
     }

    public  String fileDownload(){
        System.out.println("fileDownload()============");
        fileStream=download();
        return  SUCCESS;
    }


    public InputStream getFileStream() {
        return fileStream;
    }

    public void setFileStream(InputStream fileStream) {
        this.fileStream = fileStream;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}
           

第二步:配置struts.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="openPlatform" extends="nsp" namespace="/openPlatform">

   <action name="openPlatformMethod" class="OpenPlatformAction" method="openPlatformMethod">
       <result name="success0">/openPlatform/openPlatform.jsp</result>
       <result name="success1"></result>
       <result name="success2"></result>
       <result name="success3">/openPlatform/openPlatform_down.jsp</result>
    </action>
    <action name="fileDownload" class="OpenPlatformAction" method="fileDownload">
    <result name="success" type="stream">
    <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
    <param name="inputName">fileStream</param>
    <param name="contentDisposition">attachment;filename=${fileName}</param>
    <param name="bufferSize">1024</param>
    </result>
    </action>
    </package>
</struts>
           

三:对于struts.xml中几个标签的介绍:

contentType 指定下载文件的文件类型 —— application/octet-stream 表示无限制

inputName 流对象名 —— 比如这里写inputStream,它就会自动去找Action中的getInputStream方法。

contentDisposition 使用经过转码的文件名作为下载文件名 —— 默认格式是attachment;filename="${fileName}",将调用该Action中的getFileName方法。其中:attachment :下载时会打开下载框

bufferSize 下载文件的缓冲大小