gpt4 book ai didi

java - 将 XML 读入 Java 文件

转载 作者:行者123 更新时间:2023-12-02 08:07:16 24 4
gpt4 key购买 nike

我有一个服务器,它接受 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:套接字已关闭 在 java.net.SocketInputStream.socketRead0( native 方法) 在 java.net.SocketInputStream.read(SocketInputStream.java:129) 在 sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) 在 sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) 在 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();
}
}

}

最佳答案

看起来您正在从 socketReader 读取之前关闭Socket。在关闭 Socket 之前,您必须完全读取该对象的结果。这是全部代码吗?您还可以尝试在关闭之前添加 socketWriter.flush()

编辑:

我注意到您正在向服务器发送 GET 请求,大概是打算发出 HTTP GET?如果是这样,您的请求格式错误。试试这个:

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

您可能最好使用 java.net.URLConnection 或 Apache HttpClient 来完成此任务。

关于java - 将 XML 读入 Java 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7958755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com