내 프로젝트는 서버와 클라이언트 클래스를 작성하는 것입니다. 서버는 CSV에서 XML로 변환 할 파일의 경로를 수신 할 때까지 기다린 다음 파일 내용을 새 파일로 저장하는 클라이언트로 다시 읽어야합니다. 그 중 90 %가 지금까지 완료되었지만 마지막에는 파일이 비어 있지만 서버 측에서 XML 파일이 생성된다는 것뿐입니다. 파일을 바이트 배열로 읽어서 새로운 파일로 읽을 수 있도록 전송하십시오.
이
는 서버 연결 처리 방법public void handleConnection(InputStream sockInput, OutputStream sockOutput){
CsvToXml csv = new CsvToXml();
String csvAddress;
while(true)
{
byte[] buffer = new byte[4094];
int bytes_read = 0;
byte[] bytes_out = new byte[4094];
try {
bytes_read = sockInput.read(buffer, 0, buffer.length);
csvAddress = new String(buffer, 0, bytes_read);
System.err.println(csvAddress);
csv.convertFile(csvAddress , "C:/Users/Arian/Desktop/xmlFile.xml", ",");
if(bytes_read < 0){
System.err.println("Tried to read from socket, but returned < 0. Closing socket.");
return;
}
System.out.println("Server Received "+ bytes_read + "bytes, data=" + (new String(buffer, 0, bytes_read)));
bytes_out = readXml();
bytes_read = sockInput.read(bytes_out, 0, bytes_out.length);
sockOutput.write(bytes_out);
sockOutput.flush();
} catch(Exception e){
System.err.println("Exception reading/writing from/to socket, e=" +e);
e.printStackTrace(System.err);
return;
}
}
}
내가 읽고 바이트 배열로 XML 파일을 넣고 그것을 돌려 그래서이 부분이다.
public byte[] readXml(){
File file = new File("C:/Users/Arian/Desktop/xmlFile.xml");
byte[] xmlFile = new byte[(int)file.length()];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
fileInputStream.read(xmlFile);
fileInputStream.close();
} catch (Exception e) {
System.err.println("File not found or unreadable.");
e.printStackTrace();
}
return xmlFile;
}
이 내가 다른 게시물에 고개를 한 나는 많은 방법을 시도했습니다 {
System.err.println("Opening connection to "+serverHostname+" port "+serverPort);
try {
sock = new Socket(serverHostname, serverPort);
sockInput = sock.getInputStream();
sockOutput = sock.getOutputStream();
} catch (IOException e) {
e.printStackTrace(System.err);
return;
}
System.err.println("About to start reading/rwriting to/from socket");
for(int i = 1; i <= iterations; i++){
try{
sockOutput.write(data, 0, data.length);
File file = new File("C:/Users/Arian/Desktop/NewFile.xml");
byte[] buffer = new byte[sockInput.available()];
sockOutput = new FileOutputStream(file);
int len;
sockInput.read(buffer);
sockOutput.write(buffer);
sockOutput.flush();
} catch(IOException e){
e.printStackTrace(System.err);
}
System.err.println("Done reading/writing to/from socket, closing socket.");
try {
sock.close();
} catch (IOException e){
System.err.println("Exception closing socket.");
e.printStackTrace(System.err);
}
System.err.println("Exiting.");
}
}
클라이언트 inputStream을과의 OutputStream 방법 (반복 int)를
공공 무효 startConversion입니다 그리고 그들 중 아무 것도 성공하지 못했습니다. 나는 두 개 이상의 링크를 게시 할 수 없으므로 내 서버와 클라이언트 클래스를 heres한다. 어쨌든 내 변환 클래스는 괜찮다.
은 - 클라이언트 클래스 http://pastebin.com/CF6BJTMP -UPDATE 서버 클래스 :
나는이 내 클라이언트 클래스 내부 for 루프를 변경하고 그것은 여전히 작동하지 않습니다. 클라이언트가 디버깅을 멈추는 위치에 주석을 달았습니다.
for(int i = 1; i <= iterations; i++){
try{
sockOutput.write(data, 0, data.length);
File file = new File("C:/Users/Arian/Desktop/NewFile.xml");
byte[] buffer = new byte[8192];
sockOutput = new FileOutputStream(file);
int count;
while((count = sockInput.read(buffer)) > 0){ //The client stops debugging here.
sockOutput.write(buffer, 0, count);
}
sockOutput.flush();
} catch(IOException e){
e.printStackTrace(System.err);
}
많은 코드가 있습니다. 실행 중에 코드를 디버깅하거나 단계별로 실행하는 경우 90 % 이상을 차지한다고 말하면 어디에서 잘못 될까요? 마지막에 파일을 쓰는 경우에만 사소한 문제가됩니다. – mattias
클라이언트를 디버깅 할 때 아무 것도하지 않습니다. 입력 및 출력 스트림을 처리하는 for 루프 바로 앞에 중단 점을 넣으면 즉시 바로 루프로 점프하고 중단 점에서 중지합니다. –
중단 점에서 중지해야합니다. 중단 점이라고합니다. 그 후에는 프로그램 흐름을 계속 진행해야합니다. 예를 들어 이클립스에서는 버튼 F5-F8을 사용하여 다른 작업을 수행 할 수 있습니다. – mattias