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

How to use ByteArrayOutputStream to download files

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use ByteArrayOutputStream to download files", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use ByteArrayOutputStream to download files.

Download the file using ByteArrayOutputStream / / File name String filepath = ServletActionContext.getServletContext () .getRealPath (farmerQrCode.getQrCodeUrl ()); File file = new File (filepath); String fileName = new Date (). GetTime () + ".png"; / / set request information HttpServletResponse response = ServletActionContext.getResponse (); response.setContentType (response.getContentType ()); response.setHeader ("Content-disposition", "attachment" Filename= "+ fileName); ByteArrayOutputStream baos = new ByteArrayOutputStream (); int len = 0; FileInputStream inputStream = new FileInputStream (file); byte [] buffer = new byte [3]; while ((len = inputStream.read (buffer))! =-1) {baos.write (buffer, 0, len);} byte [] bytes = baos.toByteArray () Response.setHeader ("Content-Length", String.valueOf (bytes.length)); BufferedOutputStream bos = null; bos = new BufferedOutputStream (response.getOutputStream ()); bos.write (bytes); bos.close (); baos.close (); export the data using POI, then download / / process the HSSFWorkbook wb here, and finally add this code when you want to export the file. ByteArrayOutputStream baos = new ByteArrayOutputStream (); response.setContentType (response.getContentType ()); response.setHeader ("Content-disposition", "attachment; filename=monthPayment.xls"); wb.write (baos); byte [] bytes = baos.toByteArray (); response.setHeader ("Content-Length", String.valueOf (bytes.length)); BufferedOutputStream bos = null; bos = new BufferedOutputStream (response.getOutputStream ()) Bos.write (bytes); bos.close (); baos.close ()

1. Use the inputStream.read (buffer) method to write the contents of the txt text to the buffer array in segments.

The length of the buffer array is specified to be 3, so "hello world!" This set of data of length 11 is written to the buffer array four times.

When inputStream.read (buffer) writes all the data to the buffer array, it finally returns a value with a len of-1, indicating that the data has been fully read.

2. Use the outStream.write (buffer, 0, len) method to overlay the values written to the buffer array each time into the memory buffer in the body of the while loop.

3. Use the outStream.toByteArray () method to convert the data stream in the memory buffer into a byte array.

4. Finally, convert the character array to a string and return return new String (data).

Use ByteArrayOutputStream to solve the problem of IO garbled code

Today, when using the S3 interface for ceph storage, we need to implement an interface for io download.

You need to convert InputStream to byte []. At first, yes, it goes like this:

Byte [] buf = new byte [(int) fileSize]; InputStream in = ossObject.getObjectContent (); try {for (int n = 0; n! =-1;) {n = in.read (buf, 0, buf.length);}} catch (IOException e) {log.error (e.getMessage ()) } finally {try {in.close ();} catch (IOException e) {log.error (e.getMessage ());}}

However, if the downloaded file is a little larger, there will be garbled code.

So the method recommended on the Internet is changed and byte cache is used to convert InputStream to byte []:

Private static byte [] inputToByte (InputStream inStream, int fileSize) throws IOException {ByteArrayOutputStream swapStream = new ByteArrayOutputStream (); byte [] buff = new byte [fileSize]; int rc; while ((rc = inStream.read (buff, 0, fileSize)) > 0) {swapStream.write (buff, 0, rc);} return swapStream.toByteArray ();}

The situation of garbled code will be solved!

At this point, I believe you have a deeper understanding of "how to use ByteArrayOutputStream to download files". 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: 0

*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