zoukankan      html  css  js  c++  java
  • SSH下载的方法2

    ------------------------------下作下载方法二---------------------------------------------------
    String msg=null;
      try {
      response.setCharacterEncoding("gb2312");
      response.setContentType("text/html");
      javax.servlet.ServletOutputStream ou = response.getOutputStream();
      //文件名
      String filename=new String(request.getParameter("filename").getBytes("ISO8859_1"),"utf-8").toString();
      //路径
     
      String filepath=ServletActionContext.getServletContext().getRealPath("/upload/"+filename);
      //System.out.println("DownloadFile filepath:" + filepath);
      //System.out.println("DownloadFile filename:" + filename);
      //java.io.File file = new java.io.File(filepath + filename);
      java.io.File file = new java.io.File(filepath);
      if (!file.exists()) {
      System.out.println(file.getAbsolutePath() + " 文件不能存在!");
       msg="抱歉,文件可能过期了!";
       super.addActionError(msg);
       return null;
      }
     
      // 读取文件流
      java.io.FileInputStream fileInputStream = new java.io.FileInputStream(file);
      // 下载文件
      // 设置响应头和下载保存的文件名
      if (filename != null && filename.length() > 0) {
      response.setContentType("application/x-msdownload");//弹出下载的框
      
      response.setContentLength((int) file.length());//下载统计文件大小的进度
      response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"),"iso8859-1") + "");
      //response.setHeader("Content-Length", file.length());
      //下载框的信息
      if (fileInputStream != null) {
      int filelen = fileInputStream.available();
      //文件太大时内存不能一次读出,要循环
      
      byte a[] = new byte[filelen];
     
      fileInputStream.read(a);
      
      ou.write(a);
      }
      fileInputStream.close();
      ou.close();
      }
      } catch (Exception e) {
       e.printStackTrace();
       msg="抱歉,文件可能过期了!";
       super.addActionError(msg);
       
      }
      //解决完成后使用一切正常,但是总抛出java.lang.IllegalStateException异常主要是流还存在
      return null;
      
     }

  • 相关阅读:
    Windows编译openssl3
    【转】FFmpeg采集设备
    构建FFmpeg项目时链接报错avformat_alloc_context未定义
    anaconda代理设置
    静态链接导致的一个bug分析
    Qt如果发送信号过快会如何?
    关闭Edge浏览器多窗口Alt+Tab组合键切换
    [转]Windows上的valgrinddeleaker
    在qt项目中编译错误error ::clock未声明
    使用单元测试驱动开发的方式编写flask应用
  • 原文地址:https://www.cnblogs.com/qgc88/p/SSH_down2.html
Copyright © 2011-2022 走看看