2012-09-17 54 views
30

私はすでにイメージがどこにあるのかは知っていますが、簡単にするためにJSoup自体を使用してイメージをダウンロードしたかったのです。私はJSoupを介して答えを見つける前JSoupを使用して画像をダウンロードするにはどうすればよいですか?

//Open a URL Stream 
Response resultImageResponse = Jsoup.connect(imageLocation).cookies(cookies).ignoreContentType(true).execute(); 

// output here 
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new java.io.File(outputFolder + name)); 
//BufferedWriter out = new BufferedWriter(new FileWriter(outputFolder + name)); 
out.write(resultImageResponse.body());   // resultImageResponse.body() is where the image's contents are. 
out.close(); 
+3

これらを使用することができます。 [FileUtils#copyURLToFile](http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html)。 – Sorter

答えて

38

は私も質問を書いて終了しませんでした:これは私がこれまで持っているものである

(これは等の取得クッキー、リファラを、簡素化することです)小さな実験。

//Open a URL Stream 
Response resultImageResponse = Jsoup.connect(imageLocation).cookies(cookies) 
             .ignoreContentType(true).execute(); 

// output here 
FileOutputStream out = (new FileOutputStream(new java.io.File(outputFolder + name))); 
out.write(resultImageResponse.bodyAsBytes()); // resultImageResponse.body() is where the image's contents are. 
out.close(); 
+3

説明:イメージはバイナリー・データであり、文字データではありません。 'Response#body()'は 'String'を返します。代わりに生のバイト配列を取得する必要があります。また、 'Writer'を使用してバイトを文字に変換する場合、' OutputStream'に固執する必要があります。 – BalusC

+0

実際、当初私は多くの点で間違っていました。まあ、少なくとも私はすぐに応答を書くことで私の境界を越えていないことを願っています: - pこのサイトのどこでもこのようなものは少なくとも見つけられませんでした。 –

+1

ありがとう@BalusC!これらの他の回答は私に役立ちました:JSoupからの[image bytes](http://stackoverflow.com/a/12663619/519951)と[バイト配列をファイルに保存する](http://stackoverflow.com/questions/1580038) /バイト配列からイメージファイルへ) – ruhong

1

単にあなたがこの1つのライナー付きjsoupせずにそれを行うことができますmethods-

public static String storeImageIntoFS(String imageUrl, String fileName, String relativePath) { 
    String imagePath = null; 
    try { 
     byte[] bytes = Jsoup.connect(imageUrl).ignoreContentType(true).execute().bodyAsBytes(); 
     ByteBuffer buffer = ByteBuffer.wrap(bytes); 
     String rootTargetDirectory = IMAGE_HOME + "/"+relativePath; 
     imagePath = rootTargetDirectory + "/"+fileName; 
     saveByteBufferImage(buffer, rootTargetDirectory, fileName); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return imagePath; 
} 

public static void saveByteBufferImage(ByteBuffer imageDataBytes, String rootTargetDirectory, String savedFileName) { 
    String uploadInputFile = rootTargetDirectory + "/"+savedFileName; 

    File rootTargetDir = new File(rootTargetDirectory); 
    if (!rootTargetDir.exists()) { 
     boolean created = rootTargetDir.mkdirs(); 
     if (!created) { 
      System.out.println("Error while creating directory for location- "+rootTargetDirectory); 
     } 
    } 
    String[] fileNameParts = savedFileName.split("\\."); 
    String format = fileNameParts[fileNameParts.length-1]; 

    File file = new File(uploadInputFile); 
    BufferedImage bufferedImage; 

    InputStream in = new ByteArrayInputStream(imageDataBytes.array()); 
    try { 
     bufferedImage = ImageIO.read(in); 
     ImageIO.write(bufferedImage, format, file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

}

関連する問題