开发者社区> 问答> 正文

pdf.js 跨域问题?报错

我的页面a.html是在apache服务器上面的,但是文件是在tomcat服务器上的,我访问的连接就是http://127.0.0.1/a.html?file=http://127.0.0.1:8080/bb.pdf,访问时报错:Unexpected server response (0) while retrieving PDF,请问该如何解决,非常感谢!

展开
收起
爱吃鱼的程序员 2020-06-09 13:13:18 1449 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    1.在项目中添加两个jar包  http://pan.baidu.com/s/1mi58vhe

    2.在web.xml中添加代码:

    <!--CORSFiltermappingcrossdomainfiltersetting--><filter><filter-name>CORS</filter-name><filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class><init-param><param-name>cors.allowGenericHttpRequests</param-name><param-value>true</param-value></init-param><init-param><param-name>cors.allowOrigin</param-name><param-value>*</param-value></init-param><init-param><param-name>cors.allowSubdomains</param-name><param-value>false</param-value></init-param><init-param><param-name>cors.supportedMethods</param-name><param-value>GET,HEAD,POST,DELETE,OPTIONS</param-value></init-param><init-param><param-name>cors.supportedHeaders</param-name><param-value>content-type,authorization</param-value></init-param><init-param><param-name>cors.supportsCredentials</param-name><param-value>true</param-value></init-param><init-param><param-name>cors.maxAge</param-name><param-value>3600</param-value></init-param></filter><filter-mapping><!--CORSFiltermapping--><filter-name>CORS</filter-name><url-pattern>*</url-pattern></filter-mapping>




    我也是这个问题,搞不定啊卧槽

    引用来自“smileqingyuan”的评论

    1.在项目中添加两个jar包  http://pan.baidu.com/s/1mi58vhe

    2.在web.xml中添加代码:

    <!--CORSFiltermappingcrossdomainfiltersetting--><filter><filter-name>CORS</filter-name><filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class><init-param><param-name>cors.allowGenericHttpRequests</param-name><param-value>true</param-value></init-param><init-param><param-name>cors.allowOrigin</param-name><param-value>*</param-value></init-param><init-param><param-name>cors.allowSubdomains</param-name><param-value>false</param-value></init-param><init-param><param-name>cors.supportedMethods</param-name><param-value>GET,HEAD,POST,DELETE,OPTIONS</param-value></init-param><init-param><param-name>cors.supportedHeaders</param-name><param-value>content-type,authorization</param-value></init-param><init-param><param-name>cors.supportsCredentials</param-name><param-value>true</param-value></init-param><init-param><param-name>cors.maxAge</param-name><param-value>3600</param-value></init-param></filter><filter-mapping><!--CORSFiltermapping--><filter-name>CORS</filter-name><url-pattern>*</url-pattern></filter-mapping>




    测试无效

    测试无效

    fileName=" http://114.215.221.123/ueditor/jsp/upload/file/20170413/1492017493544030070.pdf";
        try{
         //http://114.215.221.123/ueditor/jsp/upload/file/20170413/1492017493544030070.pdf
         //纯下载方式
         response.setContentType("application/x-msdownload");
         response.setHeader("Content-Disposition","attachment;filename="+fileName.substring(fileName.lastIndexOf("/"+1),fileName.length()));
         URLurl=newURL(fileName);
         URLConnectionconn=url.openConnection();
      //    
         byte[]b=readInputStream(conn.getInputStream());
         response.getOutputStream().write(b);
         response.getOutputStream().flush();
         response.getOutputStream().close();
        }catch(Exceptione){
         e.printStackTrace();
        }

    写个SERVLET处理下,

    2020-06-09 15:32:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript函数 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载