AWS S3 Java SDK的下载PDF获取损坏 [英] aws s3 java sdk download pdf getting corrupted

查看:371
本文介绍了AWS S3 Java SDK的下载PDF获取损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GetObject这个API AWS S3下载文件。简单的文本文件做工精细,但在PDF下载我的文件已损坏。我使用的FileOutputStream和保存内容的文件,但保存的PDF是越来越损坏。

我不太确定正确的Java API来使用这个目的,应该是什么的字节数组,其中读取的字节被写入的大小。

我也很好奇,如果使用SDK直接是有道理的,或者是有开放源码的封装API的使用在Java中,我可以借力。

的FileOutputStream FOUT =新的FileOutputStream(新文件(destFileName));

 字节[] B =新的字节[8192];
 INT读取动作;
    而(真){
     读取动作= input.read(B);
        的System.out.println(读取动作=+读取动作);
        如果(读取动作==  -  1)
         打破;
        fout.write(B);
    }
    fout.flush();
    fout.close();
 

解决方案

要老实跟你说,我愿意打赌的问题是,你写的整个缓冲区的的FileOutputStream 。在传输结束,缓冲区将不会是完全满/覆盖,你最终会写一些字节被遗留下来,从最后一次读取文件的末尾。您需要修改该code,只写实际上从输入流中读取的,而不是整个缓冲区的字节数。

相反

  fout.write(B);
 

尝试

  fout.write(B,0,读取动作);
 

这样,如果你只在上次读100个字节,你只写了前100个字节的缓冲区,而忽略了实际上已经写入文件,其余8092字节。

I am downloading files from aws s3 using the getObject api. Simple text files work fine, but on pdf download my file is corrupted. I am using FileOutputStream and saving contents in a file, but the pdf saved is getting corrupted.

I am not quite sure about the correct java api to use for this purpose and what should be the size of the byte array where the bytes read get written.

I am also curious if using the SDK directly makes sense, or is there are open source wrapper api's available in Java that I could be leveraging.

FileOutputStream fout = new FileOutputStream(new File(destFileName));

 byte[] b = new byte[8192];
 int bytesRead;
    while (true) {
     bytesRead = input.read(b);
        System.out.println("bytesRead = "+bytesRead );
        if (bytesRead==-1) 
         break;
        fout.write(b);
    }        
    fout.flush();
    fout.close();

解决方案

To be honest with you, I'm willing to bet the problem is that you write the entire buffer to the FileOutputStream. At the end of the transmission, the buffer won't be completely full/overwritten and you will end up writing some bytes to the end of the file that were left over from the last read. You need to modify this code to only write the number of bytes that are actually read from the input stream, rather than the entire buffer.

Instead of

fout.write(b);

Try

fout.write(b, 0, bytesRead);

This way, if you only read 100 bytes during the last read, you only write the first 100 bytes of the buffer and ignore the remaining 8092 bytes that were actually already written to the file.

这篇关于AWS S3 Java SDK的下载PDF获取损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆