博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
下载文件downloadFile
阅读量:6283 次
发布时间:2019-06-22

本文共 2447 字,大约阅读时间需要 8 分钟。

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

 

转载于:https://www.cnblogs.com/sierrajuan/p/4962530.html

你可能感兴趣的文章
命令行中使用adb安装apk
查看>>
Apache中关于页面缓存的设置
查看>>
Linux文件系统
查看>>
计算任意两个日期的间隔天数
查看>>
【敏捷开发】驱动测试开发
查看>>
4种常见的MySQL日志类型
查看>>
onclick事件
查看>>
存储过程加密
查看>>
[再寄小读者之数学篇] (2014-04-18 from 352558840@qq.com [南开大学 2014 年高等代数考研试题]一个秩等式)...
查看>>
hrbustoj 1179:下山(DFS+剪枝)
查看>>
C#进程启动实例
查看>>
Atitit .html5刮刮卡的gui实现总结
查看>>
android精品开源项目整理
查看>>
jQuery同步Ajax带来的UI线程阻塞问题及解决办法
查看>>
Python格式化输出
查看>>
mysql oracle静默 一键安装脚本
查看>>
微服务-分解应用程序从而实现更好的部署特性及可伸缩性
查看>>
mac 连接windows 共享内容
查看>>
GPS模块编程之NMEA0183协议
查看>>
Linux常用命令_(安装包管理)
查看>>