AWS S3 Java SDK的下载PDF获取损坏AWS、Java、PDF、SDK

由网友(满腔的辛酸)分享简介:我使用GetObject这个API AWS S3下载文件。简单的文本文件做工精细,但在PDF下载我的文件已损坏。我使用的FileOutputStream和保存内容的文件,但保存的PDF是越来越损坏。 我不太确定正确的Java API来使用这个目的,应该是什么的字节数组,其中读取的字节被写入的大小。 我也很好奇,如果使用...

我使用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.

阅读全文

相关推荐

最新文章