简体   繁体   中英

Java as HTTP server - Is possible get a image via POST method?

I would like to know if is possible get the image via POST method with a HTTP server implemented in Java (With a simple input file form). I already implemented the Java server but I can only get text files via POST method it's because that the my application only copies the file content to another empty file creating the same file with the same characteristics. This does not work with image file or other files, this can only work with text file.

Anyone know how to implement it with images? Some coordinates would be of great help! Thanks in advance!

As far as i know you should create something like it:

Server-side: If you use a servlet that receive data in post you have to get the outputStream from the response. Once you have it it is done because you write the data image on the stream. For example let's suppose your image is a file stored in the server you could do:

    response.setContentLength((int) fileSize);
    byte b[] = new byte[1024];

while ( fOutStream.read(b) != -1)
   response.getOutputStream().write(b);

   fOutStream.close() ; 

Where the fOutStream is the source stream (your image).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM