Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of converting the byte string returned by the interface into a file in Java

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "what is the method of converting the byte strings returned by the interface into files in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of converting the byte string returned by the interface into a file in Java?"

Let's talk about the current demand scenario.

Recently, the company wants to access a third-party service in the project. In this third-party service, a report file needs to be downloaded and fed back through an interface.

This download interface returns a byte string, such as [1pr 2je 3rec 4pr 5pr 6pr 7]. Of course, the real data will not look like this.

But how do we convert this byte string into a file stream?

Try to convert the string directly to the byte array.

I got the byte string first, but there's no way to convert it directly to the byte array byte [].

At this point, I came up with a method, that is, to convert the byte string directly into a string, that is, the following code:

Object obj = "[1 string.getBytes 2 3 7]; String string = (String) obj;byte [] bytes = string.getBytes (); InputStream inputStream = new ByteArrayInputStream (target)"

Unfortunately, this is wrong. I don't know why.

We have no choice but to find another way.

Try again if it is a special match [], and a failure caused by a comma.

So I tried it again, and the code was given to you, as follows:

Object obj = "[1 string.getBytes 2 3 7]; String string = (String) obj;byte [] bytes = string.getBytes (); InputStream inputStream = new ByteArrayInputStream (target)"

As in the code above, we replace the commas and brackets in the string before turning it into a byte array.

Unfortunately, I also failed, and I had no choice but to find another way again.

Finally, I didn't want to waste too much time, so I found out if the third-party service provided a code example.

Wow, there is, so I really gave a code example, which is a bit hasty, but it is very important.

I will post the method of how to rotate a byte array in the code example for you to learn.

Object obj = "[1 string 2 target 3 7 4 5 5 6 7"; String string = (String) obj;ObjectMapper mapper = new ObjectMapper (); byte [] target = mapper.readValue (string, new TypeReference () {})

The best solution is this, can not help but sigh, we still have to make good use of available resources, some cases may be very simple to solve, there is no need to go too deep buckle, I hope you can learn from it.

At this point, I believe you have a deeper understanding of "what is the method of converting the byte string returned by the interface into a file in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 289

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report