我在java中编写了一个代码,用于从客户端向服务器发送文件。服务器端将对文件执行操作并将其发送回客户端。我已经完成了在服务器端执行操作但在客户端没有收到文件的情况。这是参考代码
客户方:
private void receiveFileBack() throws IOException {
int bytesRead;
int port;
ServerSocket serverSocket = null;
// ServerSocket serverSocketx = new ServerSocket(0);
//port = serverSocketx.getLocalPort();
serverSocket = new ServerSocket(4444);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.print("accepted connection"+clientSocket);
InputStream in = clientSocket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
System.out.print(fileName+"..........");
File f =new File("serverOut\\"+fileName);
OutputStream output = new FileOutputStream("serverOut\\"+fileName);
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0 && (bytesRead = clientData.read(buffer, 0,
(int)Math.min(buffer.length, size))) != -1) {
serverStatus.append("Reading file..\n");
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
// clientSocket.close();
// Closing the FileOutputStream handle
output.close();
output.flush();
}
}
服务器端:
public void sendFileBack(String fileName)
throws UnknownHostException,FileNotFoundException, IOException {
Socket sock = new Socket("192.168.0.103", 13267);
String tmpName=fName.replace(".mcrl2", ".lps");
File myFile = new File("out" + "\\" +tmpName );
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile)
BufferedInputStream bis = new BufferedInputStream(fis)
//bis.read(mybytearray, 0, mybytearray.length);
DataInputStream dis = new DataInputStream(bis)
dis.readFully(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
//Sending file name and file size to the server
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(myFile.getName());
dos.writeLong(mybytearray.length);
dos.write(mybytearray, 0, mybytearray.length);
dos.flush();
//Sending file data to the server
os.write(mybytearray, 0, mybytearray.length);
os.flush();
console.append("\n\n Output file transfered successfully \n \n");
//Closing socket
sock.close();
}
//this is receiving file method back from server
堆栈跟踪:
java.lang.NullPointerException
abp.lpsaccepted connectionSocket[addr=/127.0.0.1,port=53150,localport=4444]Jan 12, 2014 10:05:31 PM mcrl2.MainFrame$41 run
SEVERE: null
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.net.SocketInputStream.read(SocketInputStream.java:203)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at mcrl2.MainFrame.receiveFileBack(MainFrame.java:1353)
at mcrl2.MainFrame.access$4200(MainFrame.java:48)
at mcrl2.MainFrame$41.run(MainFrame.java:1402)
BUILD SUCCESSFUL (total time: 40 seconds)
任何帮助将不胜感激。请告诉我哪里出错了。 提前致谢