导入:上个星期五加班到很晚(且用了同事的ios手机做测试),在为解决一个bug而烦恼,不知道怎么修,从网上找过资料,但是并没解决方案,且有其他人遇到此问题,但并没人解决,因此写下这篇文章记录一下。
ios13企业微信点击pdf页面直接乱码了
客户的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 = this.getParams();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String encoding = "ISO-8859-1";
String filename = params.getParameterAsString("filename");
String filepath = params.getParameterAsString("filepath");
if(!filepath.startsWith("/uploads/")){
response.getWriter().print("Error: file illegal!");
return NONE;
}
filepath = URLDecoder.decode(filepath, encoding);
String realPath = request.getSession().getServletContext().getRealPath("/");
File file = new File(realPath + filepath);
if(!file.exists()){
return "找不到指定文件";
}
String agent = request.getHeader("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=\"" + MimeUtility.encodeText(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 = response.getOutputStream();
reader = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(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 = request.getContextPath();
response.sendRedirect(contextPath+filepath);
}
总结
并没找到具体的原因,有点可惜。但能解决到客户的问题,也是挺不错的。