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

    //文件下载
    在标签里制定文件的路径:
    <a href="download.action?fileName=16.1.txt">点击此处下载文档</a>  
    制定的文件是web工程中的


    <action name="download" class="action.FileDownAction">
        <param name="inputPath">/image</param>
        <result name="success" type="stream">
         <param name="contentType">application/octet-stream</param>
         <param name="inputName">inputStream</param>
         <param name="contentDisposition">
          arrachment;filename="${fileName}"
         </param>
         <param name="bufferSize">4096</param>
        </result>
        </action>


    创建真正文件下载类FileDownAction

    package action;

    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    public class FileDownAction extends ActionSupport {

     private String inputPath;
     private String fileName;
     private InputStream inputStream;
     private String contentType;
     public String getInputPath() {
      return inputPath;
     }
     public void setInputPath(String inputPath) {
      this.inputPath = inputPath;
     }
     public String getFileName() {
      return fileName;
     }
     public void setFileName(String fileName) {
      this.fileName = fileName;
     }
     public InputStream getInputStream() throws Exception {
      String path =ServletActionContext.getServletContext().getRealPath(inputPath);
      return new BufferedInputStream(new FileInputStream(path+"\"+fileName));
     }
     public void setInputStream(InputStream inputStream) {
      this.inputStream = inputStream;
     }
     public String getContentType() {
      return contentType;
     }
     public void setContentType(String contentType) {
      this.contentType = contentType;
     }
     @Override
     public String execute() throws Exception {
      return SUCCESS;
     }
      
    }

  • 相关阅读:
    GridView只显示日期问题
    自定义一个选择日期的用户控件
    母版页所带来的路径问题
    C#之旅(一): 泛型 和IComparable、IComparer
    使用HttpWebRequest来秒杀
    NameValueCollection Dictionary区别
    在C#中使用代理的方式触发事件 (委托和事件 )(二)(转)
    SQL2005语句实现行转列,列转行
    值类型和引用类型的区别?(转)
    2010年年终总结
  • 原文地址:https://www.cnblogs.com/lianceng/p/5959180.html
Copyright © 2011-2022 走看看