• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sending images over server socket to browser page

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I am writing simple web server which accepts requests on socket and sends html text response.
I am receiving requests from browser on the specified port. like http://localhost:5555
Now I also want to send small gif files and want to display them on the browser page. The resulting html page will display text combined with gif images at specific positions.
I am using java ImageIO API for the same. However I am not getting the desired page.

Here is my sample code on Server side: I am listening to requests on serverSocket 5555

PrintWriter socketWriter = new PrintWriter(socket.getOutputStream());
socketWriter.println("HTTP/1.1 " + 200 + " OK");
socketWriter.println("Server: ICS HTTP Server");
socketWriter.println("Date: " + (new Date()));
socketWriter.println("Content-type: text/html");
socketWriter.println("Content-type: image/jpeg");
socketWriter.println("Content-type: image/gif");
socketWriter.println("Cache-Control,no-store,no-cache,must-revalidate");

socketWriter.println("Content-Length: " + output.length());
socketWriter.println("");

//String output is much more with combination of havascript,html and html tables

socketWriter.println(output);

String filePath = c:\images\+ "button1.gif";
File f1 = new File(filePath);

ImageInputStream imgStream1 = ImageIO.createImageInputStream(f1);
long size = imgStream1.length();

BufferedImage bufferedImage1 = ImageIO.read(f1);
boolean success = ImageIO.write(bufferedImage1,"gif",socket.getOutputStream());

socketWriter.close();
socketReader.close();
socket.close();
socket = null;

I will appreciate your help and thanks in advance.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer your immediate question: don't do anything so elaborate as that. The server shouldn't care what the content of the image file is. Just use FileInputStream to read the bytes from the file, and send those bytes unchanged over the connection.

Now, to answer the question you should have asked: why are you writing your own web server, rather than using an existing one? HTTP is actually pretty complicated if you include all the conventions and pseudostandards that have emerged over the years. There are plenty of different webservers to choose from. For a smallish one that's easy to embed in your own programs, try Jetty.



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic