Bookmark Topic Watch Topic
  • New Topic

how to retreive an image from server location  RSS feed

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator


hi everybody, i have a proble. please give a solution for me.

i have a jsp which uploads images to the server from client. i have sucessfully done it with assistance. now the problem is i have to retreive the image from server to client. i have retreived the file. the sample code is

File f=new File(getServletContext().getInitParameter("IMAGE-PATH"),"1.png");
FileInputStream fin=new FileInputStream(f);
PrintWriter p=response.getWriter();
response.setContentType("image/jpeg");
int i=0;
while(i!=-1)
{
i=fin.read();
p.write(i);
}
fin.close();

this code is working.

if i am trying to output some text with the image it is not working. only the image is being displayed. text is not being displayed. the sample code is

File f=new File(getServletContext().getInitParameter("IMAGE-PATH"),"1.png");
FileInputStream fin=new FileInputStream(f);
PrintWriter p=response.getWriter();
response.setContentType("image/jpeg");
int i=0;
while(i!=-1)
{
i=fin.read();
p.write(i);
}
fin.close();
p.write("abcdef");=================================>this part is not beeing displayed.


more over i want the image to be displayed as a link. so i have written the code link this

out.print("<img src='"+getServletContext().getInitParameter("IMAGE-PATH")+"1.png' />");

this code is working sucessfully if the client and server both are the same machine. if the client and serevr are both different machines this code is not working. i understood the problem. but i am unable to fix the problem.


the problems are:
1) i need to get the image as well as the text
2) i need the image to be displayed as a link
 
Tim Holloway
Bartender
Posts: 18775
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
This isn't the right forum to post that kind of question in. you should have asked in the Servlets forum where more people would know what you're talking about and where it wouldn't obscure questions relating to an unrelated forum's topic.

You can't send text and an image in the same response stream. HTTP doesn't work like that. Only a single object can be written in a response.

To get a complex response, such as text and image, you need to return a compounding object such as an HTML stream containing URL references that return the image and the text, each as a separate request/response. Or, since HTML is itself a text medium, you could put the text directly in the web page. But you'd still have to render the image by using an <a href="..."> element.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
sorry i havent noticed that. i was in a hurry
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12148
255
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closed this topic as Anusha has now opened the same question in the Servlets forum.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!