Scenario 1: Read a remote image file through URL and return a byte []. In the future, these bytes can be uploaded to a remote machine through httpclient.
1.1 If these bytes [] are uploaded, only a part of the image can be displayed. In the end, it is suspected that the upload tool httpclient is incomplete due to network problems. However, the problem may be due to a problem in reading the URL to obtain byte.
Error: The bufferedinputstream and read methods are used.
Private byte [] readfileimage (string simageurl) throws ioexception, serviceexception {URL url = new URL (simageurl); urlconnection = URL. openconnection (); bufferedinputstream = new bufferedinputstream (urlconnection. getinputstream (); int Len = bufferedinputstream. available ();// This method is applicable to local files. However, available cannot return the actual file size for remote files. The file size must be accumulated in part of the file until it is read.If (LEN> 1024*1224*5) {Throw new serviceexception ("the post image is larger than 5 m");} byte [] bytes = new byte [Len]; int r = bufferedinputstream. read (bytes); If (Len! = R) {bytes = NULL; bufferedinputstream. Close (); throw new ioexception ("incorrect file reading");} bufferedinputstream. Close (); Return bytes ;}
1.2 use bytearrayoutputstream and tobytearray Methods
1.2.1 incorrect method: When writing to bytearrayoutputstream, if it is not written according to the read length, it is written according to buffer. length. This will cause the image to become blurred.
Private byte [] getimagebytearray (inputstream INS) {byte [] buffer = new byte [1024]; system. out. println (buffer. length); bytearrayoutputstream baos = new bytearrayoutputstream (); try {Boolean iscomplete = false; while (! Iscomplete) {If (INS. Read (buffer )! =-1) {baos. Write (buffer); // equivalent to baos. Write (buffer, 0, // buffer. Length. // Baos. Flush (); // The image change is irrelevant to the call of flush.} Else {iscomplete = true;} baos. Flush (); // It doesn't matter if DEST is bytearrayoutputstream.} Catch (exception e) {logging. daodao_sns.error (E);} byte [] res = baos. tobytearray (); try {ins. close (); baos. close ();} catch (ioexception E2) {logging. daodao_sns.error (E2);} return res ;}
1.2.2 correct write:The number of bytes read from SRC to buffer each time is precisely written to dest. If DEST corresponds to file, flush will work.
private byte[] getImageByteArray(InputStream ins) { byte[] buffer = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int length = -1; while ((length = ins.read(buffer)) != -1) { baos.write(buffer, 0, length); // baos.flush(); } } catch (IOException e) { Logging.DAODAO_SNS.error(e); } byte[] res = baos.toByteArray(); try { ins.close(); baos.close(); } catch (IOException e2) { Logging.DAODAO_SNS.error(e2); } return res; }
2. Scenario: Read byte [] from File
Public static byte [] readfileimage (string filename) throws ioexception {bufferedinputstream = new bufferedinputstream (New fileinputstream (filename); int Len = bufferedinputstream. available (); byte [] bytes = new byte [Len]; int r = bufferedinputstream. read (bytes); If (Len! = R) {bytes = NULL; throw new ioexception ("incorrect file reading");} bufferedinputstream. Close (); Return bytes ;}
3. About the flush operation of outputstream:
Flush function is to explicitly ask Java program to write something into the disk, which means doing Io. say if we are using a bufferedoutputstream, when we do write ("something"), it is stored in the buffer, but not in the disk yet. when you call flush, it will write the stuffs in your buffer to the disk. usually, you don't need to call the flush by yourself, cause it requires Io and lower down the speed. the stream will automatically flush when the buffer is full. unless you want some contents being updated to the disk, you can use flush.
4. Do not use available to allocate buffer for Remote File Reading.
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. the next invocation might be the same thread or another thread. A single read or skip of this variable bytes will not block, but may read or skip fewer bytes.
Note that while some implementations of inputstream will return the total number of bytes in the stream, please will not. it is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
In short,InputStream.available()
Is not half as useful as you think it is.
If you need to detect the end of the stream,read()
From it and it detect if the result is-1
. Do not useavailable()
.