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

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

  • 相关阅读:
    android 实现非GPS 手机定位
    Android 杂谈
    Android完全退出程序代码
    DB2数据库使用系统性能优化深入讨论
    DB2 数据库打算:失失最佳功能的准绳
    教你疾速驾驭DB2数据库中的相关饬令3
    教你快速把握DB2数据库中的相干命令5
    DB2中更新施行方案的几个罕见的方法2
    Oracle与DB2、MySQL取前10笔记实的比拟2
    JDBC衔接DB2、Oracle、MySQL、PostgreSQL
  • 原文地址:https://www.cnblogs.com/siashan/p/4276510.html
Copyright © 2011-2022 走看看