java學習

          java學習

           

          Struts2下載文件的程序

          在jsp里寫:
            
          <a href="download.action">點此下載文件</a>

          在struts.xml中:
          <?xml version="1.0" encoding="UTF-8"?>
           
          <!DOCTYPE struts PUBLIC
              
          "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
              
          "http://struts.apache.org/dtds/struts-2.1.7.dtd">
              
          <struts>
                  
          <package name="mydown" extends="struts-default">
                      
          <action name="download" class="com.yjw.web.DownAction">
                          
          <param name="id">1</param>
                          
          <result type="stream">
                          
                          
                              
          <!-- 下載文件的mime類型 -->
                              
          <param name="contentType">${fileType}</param>
                              
          <!-- 下載文件的描述 -->
                              
          <param name="contentDisposition">attachment;filename=${fileName}</param>
                              
          <!-- 設置緩沖區(qū)大小 -->
                              
          <param name="bufferSize">1024</param>
                              
          <!-- 獲得文件名的getxxx方法的名字 ,不包含get-->
                              
          <param name="inputName">inputStream</param>
                          
          </result>
                      
          </action>
                  
          </package>
              
          </struts>
          在action中的程序:
          package com.yjw.web;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import org.apache.struts2.ServletActionContext;
          import com.opensymphony.xwork2.Action;
          public class DownAction implements  Action{
              
          private String fileName;
              
          private String id;
              
          private String fileType;
              
          public String execute() throws Exception {
                  
          return "success";
              }

          public InputStream  getInputStream() throws IOException{
              String path 
          = ServletActionContext.getServletContext().getRealPath("/");
              
          if(id.equals("1")) {
                  path 
          = path + "download/less.pdf";
                  fileName 
          = "css.pdf";
                  fileType 
          = "application/pdf";
              }
           else {
                  path 
          = path + "download/data.xlsx";
                  fileName 
          = "data.xlsx";
                  fileType 
          = "application/vnd.ms-excel";
              }

                  FileInputStream  stream 
          = new  FileInputStream(new File(path));
              
          return stream;
              }

          public String getFileName() {
              
          return fileName;
          }

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

          public String getId() {
              
          return id;
          }

          public void setId(String id) {
              
          this.id = id;
          }

          public String getFileType() {
              
          return fileType;
          }

          public void setFileType(String fileType) {
              
          this.fileType = fileType;
          }

          }

          posted on 2012-04-29 19:36 楊軍威 閱讀(193) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          導航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 山丹县| 屏山县| 崇左市| 诸暨市| 宁乡县| 大城县| 遂溪县| 阜城县| 忻州市| 马山县| 芒康县| 白朗县| 黎平县| 大埔县| 昆明市| 贵定县| 得荣县| 普定县| 米林县| 孝昌县| 汝南县| 嘉祥县| 富锦市| 阿合奇县| 嘉义市| 乡城县| 开原市| 德阳市| 宜兴市| 喜德县| 安泽县| 大厂| 兰西县| 新田县| 甘肃省| 太谷县| 呼玛县| 新巴尔虎左旗| 高青县| 罗平县| 革吉县|