利用Struts2 實現檔案下載

來源:互聯網
上載者:User

 需求如下:進來項目中需要添加檔案下載Excel功能;決定使用Struts2內建的檔案下載功能Java Code/*
* $Id: FileDownloadAction.java 496318 2007-01-15 13:58:24Z husted $
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.    See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.    The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.    You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.    See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.showcase.filedownload;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

/**
* Demonstrates file resource download.
* Set filePath to the local file resource to download,
* relative to the application root ("/images/struts.gif").
*
*/
public class FileDownloadAction implements Action {
        private String inputPath;
        private String fileName;
        private String contentType;
        /**
         * 輸入資料流
         */
        private InputStream inputStream;
        
        public void setInputStream(InputStream inputStream) {
    this.inputStream = inputStream;
  }

  public String getFileName() {
    return fileName;
  }

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

  public String getContentType() {
    return contentType;
  }

  public void setContentType(String contentType) {
    this.contentType = contentType;
  }

  public void setInputPath(String value) {
                inputPath = value;
        }

        /**
         * 如果設定了inputStream 則將inputStream輸出到瀏覽器
         * 如果通過inputPath擷取檔案,並將檔案輸出至瀏覽器;
         * @return
         * @throws Exception
         */
        public InputStream getInputStream() throws Exception {
          if(inputStream != null){
            return inputStream;
          }
                return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
        }

        public String execute() throws Exception {
          this.setContentType("application/vnd.ms-excel");
          this.setFileName("wk.xls");
          /**
            * 產生Excls檔案
            */
          if(StringUtils.isBlank(inputPath)){
             ByteArrayOutputStream output = new ByteArrayOutputStream();        
             HSSFWorkbook wb = new HSSFWorkbook();
             HSSFSheet sheet = wb.createSheet("new sheet 2");
                    // Create a row and put some cells in it. Rows are 0 based.
                    HSSFRow row = sheet.createRow(0);
                    // Create a cell and put a value in it.
                    HSSFCell cell = row.createCell(0);
                    cell.setCellValue(1);
                    // Or do it on one line.
                    row.createCell(1).setCellValue(1.2);
                    row.createCell(2).setCellValue("This is a string");
                    row.createCell(3).setCellValue(true);            
             wb.write(output);
             InputStream is = new ByteArrayInputStream(output.toByteArray());
             this.setInputStream(is);
          }
//            return is;
                return SUCCESS;
        }

}
 Struts.xml                <action name="download2" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
                        <result name="success" type="stream">
                                <param name="contentType">${contentType}</param>
                                <param name="inputName">inputStream</param>
                                <param name="contentDisposition">filename=${fileName}</param>
                                <param name="bufferSize">4096</param>
                        </result>
                </action> 遺留問題:中文名稱; Struts 如果是圖片等等 會預設開啟。稍後完善

本文出自 “簡單” 部落格,請務必保留此出處http://dba10g.blog.51cto.com/764602/851613

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.