HttpURLConnection 出现意外结果 - 读取远程二进制文件
-
03-07-2019 - |
题
我正在尝试从互联网读取远程二进制文件(例如图像),如下所示:
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;
}
}
}
在本地或本地网络服务器(localhost)上它工作得很好。
但。然后 myUrl 是某个远程 Web 服务器上文件的 URL - 它返回意外结果。例如,从给定文件的来源来看,它似乎重复了一些包(我认为是因为以前的包或某些包被损坏),并且由于这种重复,生成的文件通常比原始文件大 10% 左右。因此文件已损坏,无法使用图像查看器正确打开。
我该如何解决这个问题?
解决方案
read
不一定读取整个缓冲区(特别是如果它位于流的末尾)。
所以改变你的循环:
for (;;) {
int len = in.read(ch);
if (len == -1) {
break;
}
out.write(ch, 0, len);
}
也许将该代码放在某个方法中。
另请注意:
- 使用没有意义
DataInputStream
在这里(虽然readFully
通常很有用)。 始终使用通常的习惯关闭资源(例如流):
final Resource resource = acquire(); try { use(resource); } finally { resource.close(); }
可能不会有太大区别,但 1024 的缓冲区大小有点小。我倾向于随意默认为8192。
不隶属于 StackOverflow