java Jackson ObjectMapper 如何将 byte[] 传输到 String 以及如何在没有对象类的情况下翻译它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40023088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does Hymanson ObjectMapper transfer byte[] to String and how can I translate it without object class?
提问by Ringing.Wang
I would like to develop restful service and it will return JSON String to client. Now there is byte[] attribute in my object.
我想开发宁静的服务,它将向客户端返回 JSON 字符串。现在我的对象中有 byte[] 属性。
I use ObjectMapper to translate this object to json and response to client. But I find the byte[] is wrong if I use String.getBytes()to translate the received string. Following is example.
我使用 ObjectMapper 将此对象转换为 json 并响应客户端。但是如果我使用String.getBytes()来翻译接收到的字符串,我发现 byte[] 是错误的。以下是示例。
Pojo class
Pojo 类
public class Pojo {
private byte[] pic;
private String id;
//getter, setter,...etc
}
Prepare data: use image to get byte array
准备数据:使用图像获取字节数组
InputStream inputStream = FileUtils.openInputStream(new File("config/images.jpg"));
byte[] pic = IOUtils.toByteArray(inputStream);
Pojo pojo = new Pojo();
pojo.setId("1");
pojo.setPic(pic);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(pojo);
--Situation 1: use readvalue to object => the image2.jpg is correct
--情况一:使用readvalue对象=> image2.jpg是正确的
Pojo tranPojo = mapper.readValue(json, Pojo.class);
byte[] tranPicPojo = tranPojo.getPic();
InputStream isPojo = new ByteArrayInputStream(tranPicPojo);
File tranFilePojo = new File("config/images2.png");
FileUtils.copyInputStreamToFile(isPojo, tranFilePojo);
--Situation 2: use readvalue to Map and get String => the image3.jpg is broken
--情况二:使用readvalue来Map获取String => image3.jpg坏了
Map<String, String> map = mapper.readValue(json, Map.class);
byte[] tranString = map.get("pic").getBytes();
InputStream isString = new ByteArrayInputStream(tranString);
File tranFileString = new File("config/images3.png");
FileUtils.copyInputStreamToFile(isString, tranFileString);
If I have to use situation 2 to translate the JSON String, how can I do it? Because clients can not get the Pojo.class, so clients only can translate the JSON string themselves.
如果我必须使用情况 2 来翻译 JSON 字符串,我该怎么做?因为客户端无法获取到 Pojo.class,所以客户端只能自己翻译 JSON 字符串。
Thanks a lot!
非常感谢!
回答by Kazaag
Hymanson is serializing byte[]
as a Base64 string as describe in the documentation of the serializer.
Hymanson 正在序列byte[]
化为 Base64 字符串,如序列化程序文档中所述。
The default base64 variant is the MIME without line feed(everything in one line).
默认的 base64 变体是不带换行符的MIME(一行中的所有内容)。
You can change the variant by using the setBae64Varient
on the ObjectMapper
.
您可以使用setBae64Varient
上的来更改变体ObjectMapper
。
回答by Tobb
ObjectMapper
handles this, expressed through a unit-test:
ObjectMapper
处理这个,通过单元测试表示:
@Test
public void byteArrayToAndFromJson() throws IOException {
final byte[] bytes = { 1, 2, 3, 4, 5 };
final ObjectMapper objectMapper = new ObjectMapper();
final byte[] jsoned = objectMapper.readValue(objectMapper.writeValueAsString(bytes), byte[].class);
assertArrayEquals(bytes, jsoned);
}
This is Hymanson 2.x btw..
这是Hyman逊 2.x 顺便说一句..
Here is how to send a file to the browser using Jersey:
以下是使用 Jersey 向浏览器发送文件的方法:
@GET
@Path("/documentFile")
@Produces("image/jpg")
public Response getFile() {
final byte[] yourByteArray = ...;
final String yourFileName = ...;
return Response.ok(yourByteArray)
.header("Content-Disposition", "inline; filename=\"" + yourFilename + "\";")
.build();
Another example, using Spring MVC:
另一个例子,使用 Spring MVC:
@RequestMapping(method = RequestMethod.GET)
public void getFile(final HttpServletResponse response) {
final InputStream yourByteArrayAsStream = ...;
final String yourFileName = ...;
response.setContentType("image/jpg");
try {
output = response.getOutputStream();
IOUtils.copy(yourByteArrayAsStream, output);
} finally {
IOUtils.closeQuietly(yourByteArrayAsStream);
IOUtils.closeQuietly(output);
}
}