HttpURLConnection 出现意外结果 - 读取远程二进制文件
我正在尝试从互联网上读取远程二进制文件(例如图像),如下所示:
HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection(); //myUrl - URL object pointing for some location
if(connection.getResponseCode() == 200){
File temp = File.createTempFile("blabla", fileName); //fileName - string name of file
FileOutputStream out = new FileOutputStream(temp);
int fileSize = Integer.parseInt(connection.getHeaderField("content-length"));
int counter = 0;
DataInputStream in = new DataInputStream(connection.getInputStream());
byte ch[] = new byte[1024];
System.out.println(counter);
while((counter += in.read(ch)) > 0){
out.write(ch);
if(counter == fileSize){
out.close();
break;
}
}
}
在本地使用本地网络服务器(本地主机),它工作得很好。
但。 然后 myUrl 是某个远程 Web 服务器上文件的 URL - 它返回意外结果。 例如,从给定文件的来源来看,它似乎重复了一些包(我认为是因为以前的包或某些包被损坏),并且由于这种重复,生成的文件通常比原始文件大 10% 左右。 因此文件已损坏,无法使用图像查看器正确打开。
我该如何解决这个问题?
I'm trying to read a remote binary file (say, image) from internet like this:
HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection(); //myUrl - URL object pointing for some location
if(connection.getResponseCode() == 200){
File temp = File.createTempFile("blabla", fileName); //fileName - string name of file
FileOutputStream out = new FileOutputStream(temp);
int fileSize = Integer.parseInt(connection.getHeaderField("content-length"));
int counter = 0;
DataInputStream in = new DataInputStream(connection.getInputStream());
byte ch[] = new byte[1024];
System.out.println(counter);
while((counter += in.read(ch)) > 0){
out.write(ch);
if(counter == fileSize){
out.close();
break;
}
}
}
Locally of with local web server (localhost) it works perfectly.
But. Then myUrl is URL of file on some remote web server - it returns unexpected results. For instance, from sources of given files it seems that it repeats some packages (I think because of corruption of previous ones or someting) and the resulting file usually is about 10% bigger than original one because of this repeats. So file is corrupted and cannot be opened correctly with image viewers.
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
read
不一定读取整个缓冲区(特别是如果它位于流的末尾)。因此,改变你的循环:
也许将该代码放在某个方法中。
另请注意:
DataInputStream
没有意义(尽管readFully
通常很有用)。始终使用通常的习惯用法关闭资源(例如流):
可能不会有太大区别,但 1024 的缓冲区大小有点小。 我倾向于任意默认为 8192。
read
does not necessarily read the entire buffer (particularly if it is at the end of the stream).So change your loop:
Perhaps put that code in a method somewhere.
Also note:
DataInputStream
here (althoughreadFully
is often useful).Always close resource (such as streams) with the usual idiom:
Probably won't make much difference, but a buffer size of 1024 is a bit small. I tend to default to 8192 arbitrarily.