导入:上个星期五加班到很晚(且用了同事的ios手机做测试),在为解决一个bug而烦恼,不知道怎么修,从网上找过资料,但是并没解决方案,且有其他人遇到此问题,但并没人解决,因此写下这篇文章记录一下。
客户的USER-AGENT
agentMozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 wxwork/3.0.14 MicroMessenger/7.0.1 Language/zh
点击下载的pdf的后台接口逻辑
OutputStream os = null;
BufferedInputStream reader = null;
try {ParamsTable params = Params();HttpServletRequest request = Request();HttpServletResponse response = Response();String encoding = "ISO-8859-1";String filename = ParameterAsString("filename");String filepath = ParameterAsString("filepath");if(!filepath.startsWith("/uploads/")){Writer().print("Error: file illegal!");return NONE;}filepath = URLDecoder.decode(filepath, encoding);String realPath = Session().getServletContext().getRealPath("/");File file = new File(realPath + filepath);if(!ists()){return "找不到指定文件";}String agent = Header("USER-AGENT");response.setContentType("application/octet-stream;charset=ISO-8859-1");if(null != agent){if(-1 != agent.indexOf("Firefox")) {response.setHeader("Content-Disposition", "attachment;filename="" + deText(filename, encoding, "B") + """);} else if(-1 != agent.indexOf("Trident") || -1 != agent.indexOf("MSIE")) {response.setHeader("Content-Disposition", "attachment;filename="" + filename + """);} else {response.setHeader("Content-Disposition", "attachment;filename="" + URLDecoder.decode(filename, encoding) + """);}} else {response.setHeader("Content-Disposition", "attachment;filename="" + URLDecoder.decode(filename, encoding) + """);}os = OutputStream();reader = new BufferedInputStream(new FileInputStream(file));py(reader, os);
} catch (IOException e) {e.printStackTrace();return "下载失败";
} finally {try{if (os != null) {reader.close();}if ( reader != null) {reader.close();}} catch(Exception e) {e.printStackTrace();}
}
看了很久,用流的形式去下载pdf一直有问题,而安卓上是没有问题,因此很难看到问题,后来,我更改他的下载的方式,直接返回路径给客户端,这样能成功了。
加多这两行代码:
if(agent.indexOf("iPhone")>0 && agent.indexOf("13")>0 ){String contextPath = ContextPath();response.sendRedirect(contextPath+filepath);
}
并没找到具体的原因,有点可惜。但能解决到客户的问题,也是挺不错的。
本文发布于:2024-01-30 13:01:08,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170659087120184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |