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.");
}
}
そして、ここでは主な方法です。
を解凍し、あなたが提供したURL上の画像はブラウザでも49キロバイトであることを知っていますか?私は画像をうまく見ることができ、 'Content-Length:49626'とキャッシングを無効にしています – Gelunox