从AWS下载图像会给我提供损坏的文件

时间:2016-12-05 10:56:04

标签: java apache amazon-web-services

我正在尝试下载托管在Amazon Web Services上的图片。我的方法在任何其他主机上都可以正常工作,但是从这个网址下载图片,例如http://s3-eu-west-1.amazonaws.com/static.melkweg.nl/uploads/images/scaled/event_header/18226,这给我带来了麻烦。它确实下载了,但文件只有49kb大,无法打开。

我尝试了不同的方法,例如Apache的FileUtils copyURLToFile,BufferedInputStream,ImageIO等。有些抛出错误,大多数只是下载一个损坏的文件。

以下是我尝试过的方法:

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.");
    }
}

如果有任何兴趣,这是主要的方法

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);
}

提前致谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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.");
    }
}