서버에서 일부 파일을 다운로드하기 위해 다음 코드를 작성했지만이 코드가 전체 응답 (inputStream)을 읽지 못하는 것이 문제입니다. 파일 크기는 7.5MB이고 매번 5.5MB를 얻는 반면 어도비 리더는 파일이 손상되었다고 불평합니다. 내가 잘못 여기서 뭐하는 거지URLConnection inputStream이 완전히 읽히지 않습니다.
모든 아이디어를 여기에 코드
import java.net.URLConnection;
public class Downloader {
URL url;
public Downloader(){
try {
url = new URL("https://d396qusza40orc.cloudfront.net/algs4partI/slides%2F13StacksAndQueues.pdf");
FileOutputStream outStream;
ObjectOutputStream oStream;
try {
URLConnection con = url.openConnection();
InputStream inStream = con.getInputStream();
outStream = new FileOutputStream("data.pdf");
oStream = new ObjectOutputStream(outStream);
int bytesRead;
int totalBytesRead = 0;
byte[] buffer = new byte[100000];
while((bytesRead = inStream.read(buffer)) > 0){
//outStream.write(buffer, 0 , bytesRead);
oStream.write(buffer, 0, bytesRead);
buffer = new byte[100000];
totalBytesRead += bytesRead;
}
System.out.println("Total Bytes read are = " + totalBytesRead);
oStream.close();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
Downloader d = new Downloader();
}
}는 무엇입니까? 미리 감사드립니다.
'ObjectOutputStream'을 사용하는 이유가 있습니까? –
파일의 실제 크기는 5,383KB입니다. 그리고'ObjectOutputStream'을 제거하고'FileInputStream'에 쓰는 것은 나를 위해 완벽하게 작동했습니다. –
읽을 때마다 새 버퍼를 만들 필요가 없습니다. 당신은 단지 가비지 컬렉터를위한 작업을 만들고 있습니다. – EJP