• 下载文件downloadFile


    public static void downLoadFile(InputStream inStream, String fileName)
      {
        if (StringUtils.isBlank(fileName)) {
          fileName = UUID.randomUUID().toString().replaceAll("-", "");
        }
        HttpServletRequest req = getRequest();
        String agent = req.getHeader("User-Agent");
        boolean isMSIE = (agent != null) && (agent.indexOf("MSIE") != -1);
        try
        {
          if (isMSIE) {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            fileName = fileName.replace("+", "%20");
          } else {
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
          }
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
        HttpServletResponse response = getResponse();
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-disposition", "attachment;filename="" + fileName + """);
        response.setContentType("application/octet-stream");
        OutputStream outStream = null;
        try {
          outStream = response.getOutputStream();
          byte[] cache = new byte[1024];
          int length = inStream.read(cache);
          while (length != -1) {
            outStream.write(cache, 0, length);
            length = inStream.read(cache);
          }
        } catch (IOException e) {
          e.printStackTrace();
          try
          {
            outStream.flush();
          } catch (IOException e) {
            e.printStackTrace();
          }
          try {
            outStream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
          try {
            inStream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        finally
        {
          try
          {
            outStream.flush();
          } catch (IOException e) {
            e.printStackTrace();
          }
          try {
            outStream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
          try {
            inStream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    public static HttpServletRequest getRequest()
      {
        ActionContext ac = ActionContext.getContext();
        HttpServletRequest req = null;
        if (ac != null) {
          req = (HttpServletRequest)ac.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
        }
    
        return req;
      }
    public static HttpServletResponse getResponse()
      {
        ActionContext ac = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse)ac.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");
        return response;
      }
  • 相关阅读:
    【Linux基础】linux下修改ls显示的时间格式
    【Teradata】gtwglobal查看
    【Teradata】tdlocaledef修改默认日期配置
    【Linux基础】文件处理实例
    【Linux基础】awk命令
    【teradata】强制解锁
    第1节:保存文档
    Centos7安装MySQL数据库
    MyBatis框架之异常处理
    spring事务源码分析
  • 原文地址:https://www.cnblogs.com/sierrajuan/p/4962530.html
Copyright © 2020-2023  润新知