我有一個服務器,它接受一個WSDL請求並逐行回發WSDL的XML。我知道請求正在被正確接收和處理,因爲當服務器寫入服務器套接字時,服務器會將XML輸出到控制檯。我現在的問題是我的客戶端應用程序應該在xml中讀取,然後使用xml打印方法簽名。我將使用DOM/DocumentBuilder從WSDL獲取方法簽名的各個部分,但我需要首先將讀入的行放入一個文件中。我怎樣才能做到這一點?目前,我試圖做這種方式:用XML讀取文件Java
//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(本機方法) 在java.net.SocketInputStream.read(SocketInputStream.java:129) 在sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) 在sun.nio。 cs.StreamDecoder.implRead(StreamDecoder.java:306) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)在java.io.BufferedReader.readLine(BufferedReader.java:299) at(java.io.BufferedReader.java:136)上的java.io.InputStreamReader.read(InputStreamReader.java:167) (BufferedReader.java:136) 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被設置爲autoflush。至於關閉,如果我沒有關閉socketWriter,那麼我的請求永遠不會寫入服務器,也不會返回任何內容。它會直接掛在那裏,直到插座關閉。 –
我想我現在看到了這個問題。我正在修改我的答案。 – laz
抱歉不清楚,但我開發了服務器。這有點複雜,但基本上它只是接受來自客戶端的任何輸入,並且如果它以「GET」開始,則它將空格之後的部分解析爲文件名。然後(服務器)將該文件名傳遞給sendResponse()方法逐行讀取文件,然後將其寫入服務器套接字的輸出。然後,客戶端應該逐行讀取並將行寫入一個新文件,製作一個供DOM使用的XML文件。 –