プロジェクトでJavaのソケットを介してhtmlファイルを送信する必要があります。私はブラウザに表示されるテキストを取得することができたが、画像は一切読み込まれなかった。私は最初にhtmlファイルを送るのを助けるためにこのコードをオンラインで見つけましたが、写真を送信する方法があるかどうか疑問に思っています。私はimg
フォルダ内のすべての画像を持っています。これはhtml
ファイルの場所です。どのように画像とともにHTMLファイルをJavaのソケット経由で送信しますか
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();
}
}
}
htmlがブラウザに到着し、が表示され、ブラウザは後で別のソケットでそのURLのGETリクエストを発行します – Exceptyon