HTTP 서버에서 Mac OS, Linux 인쇄 요청을 처리할 수 없음

3763 단어
공개 HttpIppServerTransport(문자열 경로, int 포트)가 IOException {
서버 = HttpServer.create(new InetSocketAddress(포트), 0);
server.createContext(경로, 새로운 IppHandler());
서버.setExecutor(null);
서버.스타트();
}

개인 클래스 IppHandler는 HttpHandler를 구현합니다. {

    @Override
    public void handle(HttpExchange exchange) throws IOException {

            Headers requestHeaders = exchange.getRequestHeaders();
            //User-agent
            String useragent = requestHeaders.getFirst("User-agent");

        //  int contentLength = Integer.parseInt(requestHeaders.getFirst("Content-length"));

            //System.out.println("contentLength :" + contentLength);

            if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
                try {
                    OutputStream os = exchange.getResponseBody();

                     Map parameters =(Map)exchange.getAttribute("parameters");

                    System.out.println("parameters"+parameters);


                    InputStream is = exchange.getRequestBody();
                    System.out.println("++++++++++++++++++++++++++++++++++++++++");
                    byte[] inputstreamdata=is.readAllBytes();

                    System.out.println("inputstreamdata "+inputstreamdata.length+" bytes");

                    //Writing utf8 in file
                    File file = new File("C:\\Users\\admin\\Desktop\\mydoc.txt");
                    OutputStreamWriter datafile = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");
                    BufferedWriter buffwriter = new BufferedWriter(datafile);// Writing the data with

                    if(inputstreamdata.length>0) {
                     for (int i=0;i<inputstreamdata.length;i++)
                        {

                         byte mybyte=inputstreamdata[i];

                           // System.out.println("byte to binary"+Integer.toBinaryString(mybyte));                             // 1111 1110

                            String resultWithPadZero = String.format("%12s", Integer.toBinaryString(mybyte)).replace(" ", "0");
                            //String resultWithPadZero = String.format(Integer.toBinaryString(mybyte));
                            //System.out.println(resultWithPadZero);  // 00000000000000000000000011111110

                           // System.out.println(printBinary(resultWithPadZero, 8, "|"));  

                            //Writing utf8 data in file
                            buffwriter.write(inputstreamdata[i]);
                            buffwriter.flush();



                         //System.out.println(inputstreamdata[i]);
                        }
                    System.out.println("POSTMETHOD***data written successfully");
                     buffwriter.close();
                    // String response = "Invalid shutdown key\n";
                     //exchange.sendResponseHeaders(HTTP_OK, response.getBytes().length);
                     //exchange.getResponseBody().write(response.getBytes());

                     byte test = (byte) 0x012;

                     //exchange.sendResponseHeaders(test, 0);// Idle - Invalid printer command "Clean".

                    exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);


                     System.out.println("after sendResponseHeaders response code is : "+exchange.getResponseCode());
                    }

                } catch (NumberFormatException | IOException e) {
                }

            } 

    }

}

좋은 웹페이지 즐겨찾기