我正在嘗試開(kāi)發(fā)一個(gè)非常簡(jiǎn)單的客戶端/服務(wù)器,其中客戶端將文件轉(zhuǎn)換為字節(jié),將其發(fā)送到服務(wù)器,然后將字節(jié)轉(zhuǎn)換回文件。當(dāng)前,程序僅創(chuàng)建一個(gè)空文件。我不是一名出色的Java開(kāi)發(fā)人員,因此不勝感激。這是接收客戶端發(fā)送的內(nèi)容的服務(wù)器部分。ServerSocket serverSocket = null; serverSocket = new ServerSocket(4444); Socket socket = null; socket = serverSocket.accept(); DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); byte[] bytes = new byte[1024]; in.read(bytes); System.out.println(bytes); FileOutputStream fos = new FileOutputStream("C:\\test2.xml"); fos.write(bytes);這是客戶端部分Socket socket = null; DataOutputStream out = null; DataInputStream in = null; String host = "127.0.0.1"; socket = new Socket(host, 4444); out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); File file = new File("C:\\test.xml"); //InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); if (length > Integer.MAX_VALUE) { System.out.println("File is too large."); } byte[] bytes = new byte[(int) length]; //out.write(bytes); System.out.println(bytes); out.close(); in.close(); socket.close();
添加回答
舉報(bào)
0/150
提交
取消