java - byte array to image file in java
问题描述
attached is an image file which i convert to byte array and writes back the byte array to a image file . it works fine . the value of the converted byte array is [B@5bc79255.
same file is attached ina n incident in HP service manager. and am using java wo connect trhough web service and download the attachment file as byte array and write to a image file . in this situation i face problem. this byte array which i retrieve from HPSM through the webservice function provided by them is [B@6e0e048a
as you can see values are also different. but surprisingly when i write [B@6e0e048a in to a image file , the size of the file is sero and nothing is there inside the file !!! where as if i convert the same file as byte array and writes back in to some image file its reproduced correctly!!
解决方案
如果您使用 servlet 来检索字节并且图像是 base64 图像,请使用:
while ((line = request.getReader().readLine()) != null)
stringBuffer.append(line);
String base64Image = imageData.toString().split(",")[1];
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
ByteArrayInputStream finalStream = new ByteArrayInputStream(stringBuffer);
并将 finalStream 写入文件。
推荐阅读
- django - 在 django 应用程序中获取所有模型名称时出错
- c++ - 如何在 C++ 中迭代 Aws::Crt::Map
- javascript - 如何知道在分组堆叠条(chartjs)中单击了哪个堆栈?
- java - 观察java中的实时数据列表
- lua - 如何更改 Lua 脚本中的文本颜色?
- javascript - 如何在不先下载每个分辨率的情况下获取每个分辨率的文件大小 ytdl-core
- python - 对于 python 中的以下代码,我收到错误--AttributeError: 'str' object has no attribute 'next'
- sonarlint-eclipse - 如何检查 SonarLint 插件是否真的在 Eclipse 中分析我的代码?
- python - 根据python中列的条件更新行值
- javascript - 在另一个 JavaScript 文件中扩展 JavaScript 类时遇到问题 - GULP