使用java从网站保存文件

时间:2015-04-03 06:40:37

标签: java jsoup

我正在尝试构建一个基于jsoup的java应用程序来自动下载电影的英文字幕(我很懒,我知道。这是受启发来自基于类似python的应用)。它应该问你电影的名称,然后从subscene下载一个英文字幕。

我可以访问下载链接但是当我尝试“去”时出现未处理的内容类型错误到那个链接。这是我的代码

public static void main(String[] args) {
    try {
           String videoName = JOptionPane.showInputDialog("Title: ");
         subscene(videoName);
       }
       catch (Exception e) {
           System.out.println(e.getMessage());
       }
}

public static void subscene(String videoName){
       try {
           String siteName = "http://www.subscene.com";
           String[] splits = videoName.split("\\s+");
           String codeName = "";
           String text = "";
           if(splits.length>1){
               for(int i=0;i<splits.length;i++){
                   codeName = codeName+splits[i]+"-";
               }
               videoName = codeName.substring(0, videoName.length());
           }
           System.out.println("videoName is "+videoName);
          // String url = "http://www.subscene.com/subtitles/"+videoName+"/english";
           String url = "http://www.subscene.com/subtitles/title?q="+videoName+"&l=";
           System.out.println("url is "+url);
           Document doc = Jsoup.connect(url).get();
           Element exact = doc.select("h2.exact").first();
           Element yuel = exact.nextElementSibling();
           Elements lis = yuel.children();

               System.out.println(lis.first().children().text());
               String hRef = lis.select("div.title > a").attr("href");
               hRef = siteName+hRef+"/english";

           System.out.println("hRef is "+hRef);
           doc = Jsoup.connect(hRef).get();

           Element nonHI = doc.select("td.a40").first();
           Element papa = nonHI.parent();
           Element link = papa.select("a").first();
           text = link.text();
           System.out.println("Subtitle is "+text);
           hRef = link.attr("href");
           hRef = siteName+hRef;

           Document subDownloadPage = Jsoup.connect(hRef).get();
           hRef = siteName+subDownloadPage.select("a#downloadButton").attr("href");
           Jsoup.connect(hRef).get(); //<-- Here's where the problem lies

           }
           catch (java.io.IOException e) {
               System.out.println(e.getMessage());
           }
   }

有人可以帮助我,所以我不必手动下载潜水员吗?

我刚发现使用

java.awt.Desktop.getDesktop().browse(java.net.URI.create(hRef));

而不是

Jsoup.connect(hRef).get();

提示我保存文件后下载文件。但是我不想被提示,因为这样我就无法读取下载的zip文件的名称(我想在使用java保存后将其解压缩)。

2 个答案:

答案 0 :(得分:4)

假设您的文件很小,您可以这样做。请注意,您可以告诉Jsoup忽略内容类型。

// get the file content
Connection connection = Jsoup.connect(path);
connection.timeout(5000);
Connection.Response resultImageResponse = connection.ignoreContentType(true).execute();

// save to file
FileOutputStream out = new FileOutputStream(localFile);
out.write(resultImageResponse.bodyAsBytes());
out.close();

我建议在保存之前验证内容。 因为某些服务器只能在找不到文件时返回HTML页面,即断开的超链接。

...
String body = resultImageResponse.body();
if (body == null || body.toLowerCase().contains("<body>"))
{
  throw new IllegalStateException("invalid file content");
}
...

答案 1 :(得分:2)

下面:

Document subDownloadPage = Jsoup.connect(hRef).get();
hRef = siteName+subDownloadPage.select("a#downloadButton").attr("href");
//specifically here
Jsoup.connect(hRef).get();

看起来jsoup希望Jsoup.connect(hRef)的结果应该是一个HTML或一些它能够解析的文本,这就是消息说明的原因:

  

未处理的内容类型。必须是text / *,application / xml或application / xhtml + xml

我手动执行了您的代码,您尝试访问的最后一个网址返回的内容类型为application/x-zip-compressed,因此是异常的原因。

要下载此文件,您应该使用其他方法。您可以使用旧的但仍然有用的URLConnectionURL或使用像Apache HttpComponents这样的第三方库来触发GET请求并将结果检索为InputStream,将其包装成适当的编写器并将文件写入磁盘。

以下是使用URL执行此操作的示例:

URL url = new URL(hRef);
InputStream in = url.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\foo.zip"));
final int BUFFER_SIZE = 1024 * 4;
byte[] buffer = new byte[BUFFER_SIZE];
BufferedInputStream bis = new BufferedInputStream(in);
int length;
while ( (length = bis.read(buffer)) > 0 ) {
    out.write(buffer, 0, length);
}
out.close();
in.close();