Java 如何使用jsoup检查网站上pdf文件的上次修改时间
我想检查特定页面上pdf文件的上次修改时间。 pdf链接是 我正在尝试这样做:Java 如何使用jsoup检查网站上pdf文件的上次修改时间,java,connection,jsoup,Java,Connection,Jsoup,我想检查特定页面上pdf文件的上次修改时间。 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")); 我得到这个错误 UnsupportedMimeTypeExcept
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或application/xhtml+xml
不是
应用程序/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()
test提示我们可以转换需求或纯XML/HTML类型的输入。为此,您只需添加
ignoreContentType(true)
连接到您的连接设置,如
Connection.Response rs2 = Jsoup.connect("http://www.nfib.com/Portals/0/PDF/sbet/sbet201402.pdf")
.ignoreContentType(true)
.execute();
您应该能够读取返回的标题
System.out.println("Header = " + rs2.header("Last-Modified"));
输出:
Header = Mon, 10 Feb 2014 22:54:15 GMT
我知道可以用URL类来完成,但出于好奇,我想用JSOUP来完成。