Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 17:58:20 10月06日 Update

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!

Tags: Bytes methods code interfaces files arrays lieutenant generals methods characters strings examples third parties learning services content again or commas practical deeper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker Huawei MariaDB vpn