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;
     }
      
    }

  • 相关阅读:
    nfs-client-provisioner 利用NFS动态提供Kubernetes后端存储卷
    docker-compose简易编写和模板命令
    shell脚本自动过滤尝试多次连接ip并添加到系统黑名单
    Centos 升级glibc 亲测好用
    centos安装Jenkins报错
    centos8 安装docker启动失败
    cenots7 rpm 包升级ssh
    python备份文件(简易)
    Docker 容器基本操作(基础)
    Docker 环境下如何配置你的镜像(基础)
  • 原文地址:https://www.cnblogs.com/lianceng/p/5959180.html
Copyright © 2011-2022 走看看