I made a Java web server and I'm trying to send an image with the HTML img tag. This is parts of the code:
try {
socket = server.accept();
System.out.println(socket.getRemoteSocketAddress() + " has connected");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
out.write("HTTP/1.1 200 OK\r\n");
out.write("Content-type: text/html\r\n");
out.write("\r\n");
//problem starts
out.write("<img src=\"file:///Users/Reno/Desktop/MyServer/html/images/GG9/fbicon.jpg\" alt=\"Facebook Icon\">");
out.close();
socket.close();
} catch(Exception e) {
System.out.println(e);
e.printStackTrace();
}
I've put in the path to the image file in the src
tag in img tag. The connection works fine but the image doesn't show up and instead the alt text is shown. Before I added an alt text, there is only a blank page on the client's browser when connected to the server.
I also tried changing the src path to "html/images/GG9/fbicon.jpg"
and instead a question mark in blue box shows up. I then changed again to a non-existing src path like "fakepath"
and a question mark also shows up. That means the server or client must have read something with the current src path that starts with "file:///"
.
How can I fix this and please show the correct code.
I've had a similar problem using meteor when creating an <Image/>
element. Here are two possibe solutions:
make sure the image file you wish to import is in a public file usable within your project. In other words, make sure you have the right to acess the image within the folder you have created for it, if you do not, you may or may not see an error message upon load
prioritize using relative paths like : ./mypictures/Image.jpg
rather than absolute paths like project/mypictures/Image.jpg
. Certain browsers like Google Chrome block the upload of Images on the client side if the reference path is absolute for security reasons. Again, you may not always have an error message generated because of this, though you should see in your browser something along the lines of acess not allowed
Hope this helped! D.