我使用图形API将一些示例上传到OneDrive。我可以通过OkHttp成功上传文件(文本文件)。但是,当涉及图像时,我在打开图像后会收到此消息
Error interpreting JPEG image file (Not a JPEG file: starts with 0xef 0xbf)
将图像上传到onedrive之后。 .svg
格式的图像成功上传,但是.jpg, .png
格式的图像上传到一个驱动器时损坏。
以下是我的处理方式
将图像转换为字节数组,然后将该字节数组转换(写入)到上传文件。
将图像转换为字节数组的代码
byte[] contentBuilder = IOUtils.toByteArray(file.getObjectContent());
用于将字节数组作为原始格式上传到onedrive的代码。
String url = "https://graph.microsoft.com/v1.0/drive/root:/" + step.file_name + ":/content";
Request request = new Request.Builder().url(url)
.put(RequestBody.create(MediaType.parse("image/*"), step.file_data))
//step.file_data contains the byte array.
.addHeader("Authorization", String.format("Bearer %s", step.getAccess_token()))
.build();
Response response = okHttpClient.newCall(request).execute();
此方法适用于文本文件和.svg格式的图像,但不适用于其他格式的图像。我浏览了许多参考资料/网站,但找不到方法。可能是什么原因! 谢谢你:)提前