Tyler StMartin
Tyler StMartin

Reputation: 17

How do you send an html file along with its images through a socket in java

I'm required for a project to send an html file through a socket in Java. I managed to get the text to appear in the browser but none of the pictures load. I found this code online to help me send the html file in the first place, but I am wondering if there is any way to send the pictures. I have all of the images in an img folder, which is where the html file is located.

public class SimpleFileServer {

    public final static int SOCKET_PORT = 9000;  // you may change this
    public final static String FILE_TO_SEND = "D:\\Project 2\\index.html";  // you may change this

    public static void main (String [] args ) throws IOException {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    OutputStream os = null;
    ServerSocket servsock = null;
    Socket sock = null;
    try {
        servsock = new ServerSocket(SOCKET_PORT);
        while (true) {
            try {
                sock = servsock.accept();

                // send file
                File myFile = new File (FILE_TO_SEND);
                byte [] mybytearray  = new byte [(int)myFile.length()];
                fis = new FileInputStream(myFile);
                bis = new BufferedInputStream(fis);
                bis.read(mybytearray,0,mybytearray.length);
                os = sock.getOutputStream();
                os.write(mybytearray,0,mybytearray.length);
                System.out.println("Done.");
            } finally {
                if (bis != null) bis.close();
                if (os != null) os.close();
                if (sock!=null) sock.close();
            }
        }
            } finally {
                if (servsock != null) servsock.close();
            }
        }
    }

Upvotes: 1

Views: 1857

Answers (3)

maris
maris

Reputation: 776

I think you are trying to achieve the "save as" functionality in web browsers.

You need to save the html file separately and the assets such as image files, embedded contents separately.

Since you already have the images in img folder, you need to alter the src attribute of image tag in the html - to pick the images from the image folder.

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35096

Typically, image files are not included in the html download, but are requested subsequently as the html file is parsed. The problem of why the images are not showing in your case can probably be fixed by correcting the src locations in the tags. IF you do need to download everything together though, I would recommend sending a .zip archive

Upvotes: 0

jefflunt
jefflunt

Reputation: 33954

Are you sure your project does not also require you to parse the HTML and request the images separately? It sounds like you're simulating something like a web server. Generally speaking a browser will download the HTML of a page, parse it, then send follow-up requests to the server for each image or other resource (CSS, off-site Javascript, etc.) contained in the page.

Doing one request per resource can also simplify your server, because it only has to deal with the resource being requested at the time, which pushes some of the logic and complication back onto the client to be able to know which resources to ask for.

Things have changed somewhat in HTTP 2, but that's another matter that is probably outside the scope of your question.

Upvotes: 1

Related Questions