This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 5124
135
  • 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: 5124
135
  • 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
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!