问题描述
尝试使用apache httpclient库下载文件,并且导致文件小于原始文件(大约32-32kb,正常文件大小为92-93)时出现问题,并且无法在pdf查看器中正常打开。
有人可以解释一下为什么会发生这种情况吗? (使用firefox下载此文件有时可能导致文件被完全下载,有时被部分下载)
这里是我通过URL下载文件的代码
URL url = new URL(pathtofile);
final URLConnection connection = url.openConnection();
final InputStream is = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(C://result1.pdf);
byte buffer [] = new byte [1024];
int bytesRead;
while((bytesRead = is.read(buffer))> = 0){
fos.write(buffer,0,bytesRead);
}
fos.flush();
fos.close();
is.close();
尝试使用HttpClient apache库下载此文件,结果相同。
UPDATED:使用网络工具监控流量我发现通过Firefox和应用程序接收文件之间的区别。
使用Firefox首先HttpPayloadLine是:
HTTPPayloadLine:83 Td
/ F2 5.80476 Tf
(A:\040Asinis\04017.12.10\04008:32\040laboratorij)Tj
100 Tz
1 1 1 rg
/ F1 5.80476 Tf
0 0 0 rg
104.4856 0 Td
< 0145> Tj
1 1 1 rg
0 0 0 rg
3.62799 0.72565 Td
/ F2 5.80476 Tf
(\040)Tj
1 1 1 rg
0.83137 0.81569 0.78431 RG
ET
51
应用程序首先HttpPayload是
此测量是通过Microsoft网络监视器进行的。 最近更新毕竟,这是一个服务器问题,确定文件下载成功 尝试更改为 你螨获得0个字节,但是并不意味着它的完成。还只写你收到的字节不是缓冲区。 Trying to download file with apache httpclient library and have a problem with resulting file being smaller than the original (approximately 32-32kb, when normal file size is 92-93) and cannot be opened normally in pdf viewer.Can someone explain me why this can be happening ? (Using firefox to download this file can sometimes lead to file being downloaded fully and sometimes being downloaded partly) Here is code I was using to download file via URL P.S. Was trying to download this file using HttpClient apache library, same result. UPDATED: Monitoring traffic with network tool I found the difference between receiving file via Firefox and application. With Firefox first HttpPayloadLine was : With application first HttpPayload was This measurements was taken via Microsoft Network Monitor LAST UPDATE It was a server problem after all, after they fixed that files are downloaded successful Try changing to you mite get 0 bytes back but that does not mean its finished.Also write only bytes that you received not buffer. 这篇关于Http java文件下载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! HTTPPayloadLine:CWgC,ú&ÿ3@ί¯¯×××תªªªªªªªªª$ $ $ $ $ $ b¤Ãðð'È/CÈAø¯ªÍ
übA«1ÿÅç«VɬZòYóóy7»ÇH.o²e< qZna3l±°¥þ6ñþ[2YÚ1ì³Eë-ÓÊÏ$ y:tÎà![ËÅS¤¿É ¢è,þ|ºs¨¢¢ÝÝ〜〜ÒÒÒ;;;ÒÒÒÒÒÒÒÒøøøøøøøøø>>>>>>>>>>>>>>>> b
while((bytesRead = in.read(buffer))!= -1){
byte [] tmp = ArrayUtils.subarray(buffer,0,bytesRead);
FO s.write(tmp);
}
URL url = new URL("pathtofile");
final URLConnection connection = url.openConnection();
final InputStream is = connection.getInputStream();
FileOutputStream fos = new FileOutputStream("C://result1.pdf");
byte buffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) >= 0) {
fos.write(buffer, 0, bytesRead);
}
fos.flush();
fos.close();
is.close();
HTTPPayloadLine: 83 Td/F2 5.80476 Tf(A:\040Asinis\04017.12.10\04008:32\040laboratorij) Tj100 Tz1 1 1 rg/F1 5.80476 Tf0 0 0 rg104.4856 0 Td<0145> Tj1 1 1 rg0 0 0 rg3.62799 0.72565 Td/F2 5.80476 Tf(\040) Tj1 1 1 rg0.83137 0.81569 0.78431 RGET51
HTTPPayloadLine: CWgC,ú&ÿ3@Î"ݯV¨®~×>×)\ªleÚl
µï½ci¤Ãðð'È/CÈAø¯ª ÍübA«1Ãÿ Åç«VɬZòYóóy7»ÇH.o²e<qZna3l±°¥þ6ñþ[2YÚ1ì³Eë-ÓÊÏ$y:tÎà![ËÅS¤¿É¡¢è,þ|ºs¨)@¢Qâ¯ÝF~}oµÒ>¦ OAxz³äÒ.ß9æÃZ¤ùÒ¨*«øUή+4×
while ((bytesRead = in.read(buffer)) != -1) {
byte[] tmp = ArrayUtils.subarray(buffer, 0, bytesRead);
fos.write(tmp);
}