On uploading pictures and using request Getinputstream() problem encountered while getting file stream
The image upload function is often used in our web, and there are many ways to obtain it. Here I use request Getinputstream() how to get the file stream. There are two ways to get the file stream. Attach the code
The above is obtained by reading the input stream or writing the byte input stream, so there is no need to obtain the request length
Both methods can obtain the transmitted content, but there are two points to be noted. At first, only the name of the file can be obtained, but there is no content of the file. Here is the front-end code for testing:
I checked on the Internet. Some people say that there is no name attribute in the input tag, which will lead to this problem, but I don't have a name tag. Continue to check, it's like this:
So I think there must be a lack of sending type, so I added enctype ='multipart / form data 'to the form tag, and sure enough, I can get the content.
Then there is the second question. We passed request If the getinputstream () method obtains a file, the obtained content is not only the file, even if there is only one file tag in your form. At the beginning, I made this mistake. I couldn't display it directly by using the obtained byte array or converting it into a file. Later, I realized that I had to separate the contents of the file from the bytes of InputStream. Here is a method. The specific implementation process has not been studied in detail. Attach it first and then take a closer look
The parameters here are the requested content array bytes and the request type. If the type is, we can use request The getcontenttype () method directly obtains the. The return value is also a byte array. The returned byte array is the content of the uploaded image.
If you return to the front end, you can directly use the response.
The above article discusses how to upload pictures using request The problem encountered when getinputstream () gets the file stream is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.