2016-12-05 9 views
1

Amazon Web Servicesでホストされている画像をダウンロードしようとしています。私の方法は他のホストでもうまく動作しますが、このURLから画像をダウンロードすると、http://s3-eu-west-1.amazonaws.com/static.melkweg.nl/uploads/images/scaled/event_header/18226が問題になります。それはダウンロードしますが、ファイルはわずか49kbでオープンできません。AWSから画像をダウンロードすると、ファイルが破損する

私は、ApacheのFileUtils copyURLToFile、BufferedInputStream、ImageIOなど、さまざまな方法を試しました。いくつかのエラーを投げます。ほとんどの場合、破損したファイルをダウンロードするだけです。ここで

は、私が試した方法である:それは任意の関心の前に

public static void main(String[] args) 
{ 
    String imageurl = "http://s3-eu-west-1.amazonaws.com/static.melkweg.nl/uploads/images/scaled/event_header/18226"; 
    String name = "downloaded_image.jpg"; 
    String target = "C:/Users/Robotic/Downloads/" + name; 
    Download.downloadImage(imageurl, name); 
    Download.downloadImageCopy(imageurl, target); 
    Download.downloadImageIO(imageurl, target); 
    Download.downloadApache(imageurl, target); 
} 

おかげであれば

public static void downloadApache(String imageurl, String target) 
{ 
    try 
    { 
    File file = new File(target); 
    URL url = new URL(imageurl); 
    FileUtils.copyURLToFile(url, file); 
    } 
    catch(Exception e) 
    { 
     System.err.println("[3]Something went wrong."); 
    } 
} 

public static void downloadImage(String imageurl, String name) 
{ 
    try 
    { 
     URL url = new URL(imageurl); 
     InputStream in = new BufferedInputStream(url.openStream()); 
     OutputStream out = new BufferedOutputStream(new FileOutputStream(name)); 

     for (int i; (i = in.read()) != -1;) { 
      out.write(i); 
     } 
     in.close(); 
     out.close(); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     System.err.println("[0]Something went wrong."); 
    } 
} 

public static void downloadImageIO(String imageurl, String target) 
{ 
    try 
    { 
     URL url = new URL(imageurl);  
     BufferedImage image = ImageIO.read(url); 
     ImageIO.write(image, "jpg", new File(target)); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     System.err.println("[1]Something went wrong."); 
    } 
} 

public static void downloadImageCopy(String imageurl, String target) 
{ 
    try 
    { 
     try (InputStream in = new URL(imageurl).openStream()) { 
      Files.copy(in, Paths.get(target), StandardCopyOption.REPLACE_EXISTING); 
     } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     System.err.println("[2]Something went wrong."); 
    } 
} 

そして、ここでは主な方法です。

+0

を解凍し、あなたが提供したURL上の画像はブラウザでも49キロバイトであることを知っていますか?私は画像をうまく見ることができ、 'Content-Length:49626'とキャッシングを無効にしています – Gelunox

答えて

0

S3から取得しているファイルはgzipで圧縮されているため、読み込む前に解凍する必要があります。

$ wget http://s3-eu-west-1.amazonaws.com/static.melkweg.nl/uploads/images/scaled/event_header/18226 
$ file 18226       
18226: gzip compressed data, from Unix 
0

前述の回答で指摘したように、gzip形式です。 次のメソッドを使用してファイルを取得することができます

public static void downloadApache(String imageurl, String target) { 
    try { 
     File file = new File(target+".gzip"); 
     URL url = new URL(imageurl); 
     FileUtils.copyURLToFile(url, file); 

     byte[] buffer = new byte[1024]; 
     try { 
      java.util.zip.GZIPInputStream gzis = new java.util.zip.GZIPInputStream(new FileInputStream(file)); 
      FileOutputStream out = new FileOutputStream(target); 
      int len; 
      while ((len = gzis.read(buffer)) > 0) { 
       out.write(buffer, 0, len); 
      } 
      gzis.close(); 
      out.close(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 

    } catch (Exception e) { 
     System.err.println("[3]Something went wrong."); 
    } 
} 
関連する問題