• 文件下载


    1,struts2文件下载:

      

    1)Action配置

      // 下载,
        public String downloadFile() throws Exception {
            return SUCCESS;
        }

      // 文件下载(读取文件流的入口,必须返回一个文件输入流)
      public InputStream getDownloadFile() throws FileNotFoundException {
          System.out.println(riskEvent.getFilePath());
          // 图片路径
          String fileName =basePath +"/"+ riskEvent.getFilePath();
          // 如果下载文件名为中文,进行字符编码转换
          getHttpResponse().setHeader("Content-Disposition","attachment;fileName="+riskEvent.getFilePath());
          InputStream inputStream = new FileInputStream(fileName);
          System.out.println(inputStream);
          return inputStream;
      }

    2)xml的配置

          <action name="downloadFile" class="riskSurveyAction" method="downloadFile" > 
                 <result name="success" type="stream"> 
                       <param name="contentDisposition">  
                         attachment;filename="${riskEvent.filePath}"  
                       </param>  
                      <param name="inputName">downloadFile</param>
                     <param name="bufferSize">2048</param> 
                 </result> 
              </action>

    2,自己写的下载

    /**
      * 下载小票
      * @throws IOException
      */
     public void downLoadFile() throws IOException{
      String fileName = riskEvent.getFilePath();
      // 图片路径
      String filePath = basePath +"/"+ fileName.substring(0, fileName.lastIndexOf(".")) + "/"+fileName;
      HttpServletResponse response = getHttpResponse();
      File file = new File(filePath);
      long len = file.length();
      down(file, fileName, response);
     }
     
        public void down(File f,String filename,HttpServletResponse response)
        {
         response.reset();
         response.setHeader("content-disposition","attachment; filename="+filename); //设置下载的文件名
         long fileLength=f.length();
         String length1=String.valueOf(fileLength);
         response.setHeader("Content_Length",length1); //下载文件的大小
         InputStream in=null;
         OutputStream out = null;
         try{
          in = new FileInputStream( f );
          out = response.getOutputStream();
          byte[] buffer = new byte[2097152];
          int ins = in.read(buffer);//读取字节到buffer中
          //ins == -1 时 。就已经是文件的结尾了
          while ( ins != -1 ) {
           out.write(buffer, 0, ins);//将缓存buffer中的数据写到文件中
           ins = in.read(buffer);
          }
          in.close();
          out.flush();
          out.close();
         }catch (Exception e) {
             System.out.println("--下载发生异常--");
             try {
                        in.close();
                        out.flush();
                        out.close();
                    } catch (IOException e1) {
                        System.out.println("--关闭发生异常--");
                        in = null;
                        out = null;
                        e1.printStackTrace();
                    }
                }
        }

  • 相关阅读:
    三种构建器的使用
    java class file
    JVM知识(下)
    JVM知识(上)
    初识JMM
    nginx基础配置加基础实战演示
    GitHub中webhooks的使用
    redis sentinel介绍
    redis主从复制
    【开源】.net 分布式架构之监控平台
  • 原文地址:https://www.cnblogs.com/siashan/p/4276510.html
Copyright © 2020-2023  润新知