我需要通过Python / Django中实现的网络服务来提供二进制文件。问题是,当我将原始文件与带有 vbindiff 的已传输文件进行比较时,我看到已传输文件上的尾随字节,可悲的是使它无用。
客户端使用以下方式访问二进制文件并保存在Java中:
HttpURLConnection userdataConnection = null;
URL userdataUrl = null;
try {
userdataUrl = new URL("http://localhost:8000/app/vuforia/10");
userdataConnection = (HttpURLConnection) userdataUrl.openConnection();
userdataConnection.setRequestMethod("GET");
userdataConnection.setRequestProperty("Content-Type", "application/octet-stream");
userdataConnection.connect();
InputStream userdataStream = new BufferedInputStream(userdataConnection.getInputStream());
try (ByteArrayOutputStream fileStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[4094];
while (userdataStream.read(buffer) != -1) {
fileStream.write(buffer);
}
byte[] fileBytes = fileStream.toByteArray();
try (FileOutputStream fos = new FileOutputStream("./test.dat")) {
fos.write(fileBytes);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我认为HttpURLConnection.getInputStream仅读取响应的正文,还是不读取?
此代码在后端提供数据
在views.py中:
if request.method == "GET":
all_data = VuforiaDatabase.objects.all()
data = all_data.get(id=version)
return FileResponse(data.get_dat_bytes())
在models.py中:
def get_dat_bytes(self):
return self.dat_upload.open()
如何进行1:1二进制数据传输?