• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to send images over Http Server?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to coderanch so bear with me here. I made a simple webserver, and I can send html pretty easy, but how would I send images?
 
Saloon Keeper
Posts: 4697
117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "send" - it serves HTML alright? But it doesn't serve other file formats? What is different about those other files compared to the HTML files? Both should be files on the server's file system, no? Are they treated differently by the server? Remember that images are binary files, so the server mustn't treat them as text.
 
Jacob Emantsal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:What do you mean by "send" - it serves HTML alright? But it doesn't serve other file formats? What is different about those other files compared to the HTML files? Both should be files on the server's file system, no? Are they treated differently by the server? Remember that images are binary files, so the server mustn't treat them as text.


Well, I dont really try to send send them, what happens is that the path you enter in the browser is that path it looks for. And I know works with the normal files, but maybe I am doing something wrong, here is my entire class.
 
Tim Moores
Saloon Keeper
Posts: 4697
117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're doing in lines 28 to 34 is indeed treating the file as text - that works for HTML (and CSS and JS), but not for anything else like images. You need to replace that by code that streams the bytes (not characters) that make up the file to the client.

You also need to set an appropriate content type header according to what kind of file you're serving: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
 
Jacob Emantsal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:What you're doing in lines 28 to 34 is indeed treating the file as text - that works for HTML (and CSS and JS), but not for anything else like images. You need to replace that by code that streams the bytes (not characters) that make up the file to the client.

You also need to set an appropriate content type header according to what kind of file you're serving: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type


What do you suggest I do then? I haven't worked much with java networking, this is kind of my hello world.
 
Jacob Emantsal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH MY GOODNESS, i figured it out. You led me to it, I just had to stack overflow a little harder, and think about what you said, thank you so much
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!