Server
1.
data:text/plain;charset=utf-8,import java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.OutputStream;%0Aimport java.net.InetAddress;%0Aimport java.net.InetSocketAddress;%0Aimport java.net.ServerSocket;%0Aimport java.net.Socket;%0Aimport java.nio.channels.Channels;%0Aimport java.nio.channels.FileChannel;%0Aimport java.nio.channels.WritableByteChannel;%0Aimport java.util.HashMap;%0Aimport java.util.Scanner;%0Aclass httpserver{%0A%20%20%20%20public static void main(String[] args)throws Exception{%0A%20%20%20%20%20%20%20%20try(ServerSocket serverSocket=new ServerSocket()){%0A%20%20%20%20%20%20%20%20%20%20%20%20HashMap<String,String> hashMap=new HashMap<>();%0A%20%20%20%20%20%20%20%20%20%20%20%20if(1<args.length)try(Scanner scanner=new Scanner(args[1])){%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while(null!=scanner.findInLine("([^\\[\\]]+)\\[([^\\[\\]]*)"))hashMap.put(scanner.match().group(1),scanner.match().group(2));%0A%20%20%20%20%20%20%20%20%20%20%20%20}%0A%20%20%20%20%20%20%20%20%20%20%20%20serverSocket.setReuseAddress(true);%0A%20%20%20%20%20%20%20%20%20%20%20%20serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(),8080));%0A%20%20%20%20%20%20%20%20%20%20%20%20for(;;)try(Socket socket=serverSocket.accept();Scanner scanner=new Scanner(socket.getInputStream(),"utf-8");OutputStream outputStream=socket.getOutputStream()){%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(null==scanner.findInLine("^.+ (.*/)([^?]*\\.([^?]*)|[^?]*)(?:\\?.*)? (.+)"))continue;%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20String child=scanner.match().group(2),extension=scanner.match().group(3),statusCode="200";%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(null==extension)child=(0==child.length()?"index":child)+"."+(extension="html");%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20File file=new File(new File(args[0],scanner.match().group(1)),child);%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(!file.exists()&&!(file=new File(args[0],(statusCode="404")+"."+(extension="html"))).exists())continue;%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try(FileChannel fileInputStreamChannel=new FileInputStream(file).getChannel();WritableByteChannel outputStreamChannel=Channels.newChannel(outputStream)){%0ASystem.out.println(file);%0ASystem.out.println(scanner.match().group(0));%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20long length=fileInputStreamChannel.size();%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20String res=scanner.match().group(4)+" "+statusCode+"\r\n"%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20+"Content-Type: "+hashMap.get(extension)+"\r\n"%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20+"Content-Length: "+length+"\r\n"%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20+"Connection: close\r\n"%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20+"\r\n";%0ASystem.out.println(res);%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20outputStream.write(res.getBytes("utf-8"));%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20long position=0;%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try{%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while(position<length)position+=fileInputStreamChannel.transferTo(position,length-position,outputStreamChannel);%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}catch(Exception e){%0ASystem.out.println(e);%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}%0A%20%20%20%20%20%20%20%20%20%20%20%20}%0A%20%20%20%20%20%20%20%20}%0A%20%20%20%20}%0A}
2.
1をブラウザのアドレスバーに入れて開き、保存しましょう。
拡張子は'java'としてください。
3.
クラスファイル用ディレクトリ、リソース用ディレクトリを作成し、リソースを配置しましょう。
ルートに'404.html'を配置すると、パスが存在しない場合にも対応できます。
4.
javac -d {クラスファイル用ディレクトリパス} {2のファイルパス}
を実行し、クラスファイルを作成しましょう。
5.
java -cp {クラスファイル用ディレクトリパス} httpserver {リソース用ディレクトリパス}
を実行しましょう。
java -cp {クラスファイル用ディレクトリパス} httpserver {リソース用ディレクトリパス} css[text/css]js[text/javascript;charset=utf-8]
のように引数を増やすと、任意の'Content-Type'に対応できます。
6.
'http://localhost:8080/{path}'をブラウザで開くと、リソースを表示します。
'index.html'の場合、
[http://localhost:8080, http://localhost:8080/, http://localhost:8080/index.html]です。
data:...;charset=utf-8,...
URLの一種。改行は'%0A'、' 'は'%20'、'#'は'%23'に置換してあります。
Server
requestをresponseするアプリです。




