• 文件下载


    //文件下载
    在标签里制定文件的路径:
    <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;
     }
      
    }

  • 相关阅读:
    python之用循环实现五子棋小程序
    python实现百钱买百鸡
    逻辑综合工具DesignCompiler使用教程
    塑料封装可靠性问题浅析
    python发展历程
    Python02期预科课程笔记索引
    python之虚拟环境
    linux 的IP配置和网络问题的排查
    appache等的安装笔记x32
    4 计算1到100的和
  • 原文地址:https://www.cnblogs.com/lianceng/p/5959180.html
Copyright © 2020-2023  润新知