This is a continuation of my first thread on this forum. (thanks gimbal2!)
Now, I would like to properly stop the server. For that, I can do server.stop(); . However, this does not work since the object server is not public, it is contained within the public pc_proxy class.
How do I do that?
Java Code:
import java.net.*;
import java.io.*;
import com.sun.net.httpserver.*;
import java.util.concurrent.Executors;
import java.lang.reflect.Array;
public class pc_proxy {
public static String pc_path = "/pc/";
public static String pc_ext = ".jpg";
public static void main(String[] args) throws Exception {
if ((args.length == 0) || (args.length == 1)) {
System.out.println("\ngebruik: pc_proxy host(inet) port(int) debug(bool)\n");
System.exit(-1);
}
String host = (args[0]);
int port = Integer.decode(args[1]);
HttpServer server = HttpServer.create(new InetSocketAddress(host, port), 0);
server.createContext(pc_path, new pc());
server.createContext("/exit", new exit());
server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
server.start();
}
static class pc implements HttpHandler {
public void handle(HttpExchange pce) throws IOException {
String pclinkpath = pce.getRequestURI().getPath();
String pcnr = pclinkpath.substring(pc_path.length(), pclinkpath.length()-pc_ext.length());
URL url = new URL("http://ift.tt/1BRvnCv" + pcnr);
InputStream is = url.openStream();
ByteArrayOutputStream photo = new ByteArrayOutputStream();
byte[] b = new byte[8192];
int length = 0;
while ((length = is.read(b)) != -1) {
photo.write(b, 0, length);
}
is.close();
OutputStream os = pce.getResponseBody();
pce.getResponseHeaders().set("Content-Type","image/jpeg");
pce.sendResponseHeaders(200, 0);
os.write(photo.toByteArray(), 0, photo.size());
os.close();
}
}
static class exit implements HttpHandler {
public void handle(HttpExchange eexit) throws IOException {
//server.stop(10);
System.exit(0);
}
}
}
No comments:
Post a Comment