Java通过套接字发送和接收文件(byte )

我试图开发一个非常简单的客户端/服务器,客户端将文件转换为字节,将其发送到服务器,然后将字节转换回文件。

目前该程序只是创build一个空文件。 我不是一个了不起的Java开发人员,所以任何帮助非常感激。

这是接收客户端发送的服务器部分。

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(); 

感谢您的帮助,我已经设法让它现在工作,所以想我会张贴,以便其他人可以用来帮助他们。

服务器

 public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(4444); } catch (IOException ex) { System.out.println("Can't setup server on this port number. "); } Socket socket = null; InputStream in = null; OutputStream out = null; try { socket = serverSocket.accept(); } catch (IOException ex) { System.out.println("Can't accept client connection. "); } try { in = socket.getInputStream(); } catch (IOException ex) { System.out.println("Can't get socket input stream. "); } try { out = new FileOutputStream("M:\\test2.xml"); } catch (FileNotFoundException ex) { System.out.println("File not found. "); } byte[] bytes = new byte[16*1024]; int count; while ((count = in.read(bytes)) > 0) { out.write(bytes, 0, count); } out.close(); in.close(); socket.close(); serverSocket.close(); } } 

和客户

 public class Client { public static void main(String[] args) throws IOException { Socket socket = null; String host = "127.0.0.1"; socket = new Socket(host, 4444); File file = new File("M:\\test.xml"); // Get the size of the file long length = file.length(); byte[] bytes = new byte[16 * 1024]; InputStream in = new FileInputStream(file); OutputStream out = socket.getOutputStream(); int count; while ((count = in.read(bytes)) > 0) { out.write(bytes, 0, count); } out.close(); in.close(); socket.close(); } } 

在Java中复制stream的正确方法如下所示:

 int count; byte[] buffer = new byte[8192]; // or 4096, or more while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } 

希望我每次在论坛上发布这个消息都有一美元。

这里是服务器打开一个stream到文件并通过networking发送

 import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class SimpleFileServer { public final static int SOCKET_PORT = 5501; public final static String FILE_TO_SEND = "file.txt"; public static void main (String [] args ) throws IOException { FileInputStream fis = null; BufferedInputStream bis = null; OutputStream os = null; ServerSocket servsock = null; Socket sock = null; try { servsock = new ServerSocket(SOCKET_PORT); while (true) { System.out.println("Waiting..."); try { sock = servsock.accept(); System.out.println("Accepted connection : " + sock); // send file File myFile = new File (FILE_TO_SEND); byte [] mybytearray = new byte [(int)myFile.length()]; fis = new FileInputStream(myFile); bis = new BufferedInputStream(fis); bis.read(mybytearray,0,mybytearray.length); os = sock.getOutputStream(); System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)"); os.write(mybytearray,0,mybytearray.length); os.flush(); System.out.println("Done."); } catch (IOException ex) { System.out.println(ex.getMessage()+": An Inbound Connection Was Not Resolved"); } }finally { if (bis != null) bis.close(); if (os != null) os.close(); if (sock!=null) sock.close(); } } } finally { if (servsock != null) servsock.close(); } } } 

这里是客户端Recive通过networking发送的文件

 import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class SimpleFileClient { public final static int SOCKET_PORT = 5501; public final static String SERVER = "127.0.0.1"; public final static String FILE_TO_RECEIVED = "file-rec.txt"; public final static int FILE_SIZE = Integer.MAX_VALUE; public static void main (String [] args ) throws IOException { int bytesRead; int current = 0; FileOutputStream fos = null; BufferedOutputStream bos = null; Socket sock = null; try { sock = new Socket(SERVER, SOCKET_PORT); System.out.println("Connecting..."); // receive file byte [] mybytearray = new byte [FILE_SIZE]; InputStream is = sock.getInputStream(); fos = new FileOutputStream(FILE_TO_RECEIVED); bos = new BufferedOutputStream(fos); bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); bos.write(mybytearray, 0 , current); bos.flush(); System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)"); } finally { if (fos != null) fos.close(); if (bos != null) bos.close(); if (sock != null) sock.close(); } } } 

为避免文件大小的限制,在创build文件大小为byte[] bytes = new byte[(int) length];的数组时,可能会导致抛出Exception java.lang.OutOfMemoryErrorexceptionbyte[] bytes = new byte[(int) length]; ,我们可以做

  byte[] bytearray = new byte[1024*16]; FileInputStream fis = null; try { fis = new FileInputStream(file); OutputStream output= socket.getOututStream(); BufferedInputStream bis = new BufferedInputStream(fis); int readLength = -1; while ((readLength = bis.read(bytearray)) > 0) { output.write(bytearray, 0, readLength); } bis.close(); output.close(); } catch(Exception ex ){ ex.printStackTrace(); } //Excuse the poor exception handling... 

菜鸟,如果你想通过套接字写一个文件到服务器,如何使用fileoutputstream而不是dataoutputstream? dataoutputstream更适合协议级别的读写。 你的代码以字节读写是不合理的。 循环读取和写入是必要的Java io。 还有,你使用缓冲的方式。 冲洗是必要的。 这里是一个代码示例: http : //www.rgagnon.com/javadetails/java-0542.html