读入XML以归档Java

时间:2011-10-31 19:37:09

标签: java sockets

我有一个服务器接受WSDL请求并逐行发回WSDL的XML。我知道请求正在被正确接收和处理,因为我让服务器在写入服务器套接字时将XML打印到控制台。我现在的问题是我的客户端应用程序应该在xml中读取,然后使用xml打印出方法签名。我将使用DOM / DocumentBuilder从WSDL中获取方法签名的各个部分,但我需要先将读入的行放入文件中。我怎样才能做到这一点?目前我正试图这样做:

        //request WSDL from server
        System.out.println("Client requesting \"MathServices?wsdl\"...");
        socketWriter.write("GET MathServices?wsdl");
        socketWriter.close();

        //read XML response into file       
        try {
            File wsdlXML = new File("MathServices.xml");
            FileOutputStream wsdlXmlWriter = new FileOutputStream(wsdlXML);

            String xmlLine;
            while((xmlLine = socketReader.readLine()) != null){
                wsdlXmlWriter.write(xmlLine.getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

但是我收到了这个错误:

请求“MathServices?wsdl”的客户端... java.net.SocketException:socket关闭     在java.net.SocketInputStream.socketRead0(本机方法)     在java.net.SocketInputStream.read(SocketInputStream.java:129)     at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)     at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)     at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)     在java.io.InputStreamReader.read(InputStreamReader.java:167)     在java.io.BufferedReader.fill(BufferedReader.java:136)     在java.io.BufferedReader.readLine(BufferedReader.java:299)     在java.io.BufferedReader.readLine(BufferedReader.java:362)     在client.Client.main(Client.java:50)

编辑:服务器代码

package server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;

public class RequestHandler extends Thread {

    Object block;
    ServerSocket serverSocket;
    BufferedReader socketReader;
    PrintWriter socketWriter;

    public RequestHandler(Object block, ServerSocket serverSocket){
        this.block = block;
        this.serverSocket = serverSocket;
    }

    @Override
    public void run() {
        try{
            System.out.println("Waiting for connection...");
            Socket clientSocket = serverSocket.accept();
            System.out.println("Connection made.");

            synchronized(block){
                System.out.print("Notifying server thread...");
                block.notify();
                System.out.println("...done");
                System.out.println();
            }

            System.out.println("Setting up streams...");
            socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));            
            socketWriter = new PrintWriter(clientSocket.getOutputStream(), true);
            System.out.println("Reading request");

            String input;
            while((input = socketReader.readLine()) != "\n"){
                //System.out.println("Input: " +input);

                if(input.startsWith("GET")){
                    System.out.println("GET received.");
                    getResource(input);
                }
            }

            socketWriter.close();
            socketReader.close();
            clientSocket.close();

            System.out.println("Streams closed.");
        }catch(IOException e){
            System.out.println("IOException!");
            e.printStackTrace();
        }
    }

    public void getResource(String getRequest){
        String[] parts = getRequest.split("\\s+");
        String filename = parts[1].substring(1);

        if(filename.equals("MathServices?wsdl")){
            filename = "MathServices.wsdl";
        }
        System.out.println(filename);
        File resource = new File(filename);             
        sendResponse(resource, 1);
    }

    public void sendResponse(File resource, int type){
        System.out.println(resource.getAbsolutePath());

        Scanner fileReader;

        try {
            fileReader = new Scanner(resource);

            while(fileReader.hasNext()){
                String line = fileReader.nextLine();
                System.out.println(line);
                socketWriter.println(line);
            }
            socketWriter.println("\n");
            System.out.println("end of response");
            socketWriter.close();

        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:2)

在阅读Socket之前,您似乎正在关闭socketReader。在关闭Socket之前,您必须完全读取该对象的结果。这是所有的代码吗?您也可以在关闭之前尝试添加socketWriter.flush()

修改

我注意到您向服务器发送GET请求,可能是打算发出HTTP GET?如果是这样,您的请求就会出错。试试这个:

socketWriter.write("GET /MathServices?wsdl HTTP/1.1\r\n\r\n");

使用java.net.URLConnection或Apache HttpClient完成此任务可能会更好。