我想检查特定页面上 pdf 文件的最后修改时间。 pdf链接为http://www.nfib.com/Portals/0/PDF/sbet/sbet201402.pdf
我正在尝试这样做:
Connection.Response rs2 = Jsoup.connect("http://www.nfib.com/Portals/0/PDF/sbet/sbet201402.pdf").execute();
System.out.println("Header = " + rs2.header("Last-Modified"));
我收到此错误
UnsupportedMimeTypeException
最佳答案
如果不必使用 Jsoup 完成,您可以使用标准 URL 和 URLConnection 类,例如
URL url = new URL("http://www.nfib.com/Portals/0/PDF/sbet/sbet201402.pdf");
URLConnection connection = url.openConnection();
System.out.println("Header = " + connection.getHeaderField("Last-Modified"));
<小时/>
您需要记住,Jsoup 是为解析 HTML/XML 而设计的,因此默认情况下它需要以下类型
text/*, application/xml, or application/xhtml+xml
不是
application/pdf
.
如果你看一下处理它的代码,它看起来像
if (contentType != null && !req.ignoreContentType() && (!(contentType.startsWith("text/") || contentType.startsWith("application/xml") || contentType.startsWith("application/xhtml+xml"))))
throw new UnsupportedMimeTypeException("Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml",
contentType, req.url().toString());
但是 !req.ignoreContentType()
测试提示我们可以关闭需求或纯 XML/HTML 类型输入。为此,您只需添加
ignoreContentType(true)
您的连接设置,例如
Connection.Response rs2 = Jsoup.connect("http://www.nfib.com/Portals/0/PDF/sbet/sbet201402.pdf")
.ignoreContentType(true)
.execute();
并且您应该能够读取返回的 header
System.out.println("Header = " + rs2.header("Last-Modified"));
输出:
Header = Mon, 10 Feb 2014 22:54:15 GMT
关于java - 如何使用 jsoup 检查网站上 pdf 文件的最后修改时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22323700/