Home > Back-end > Zip file through the socket network transmission, but transfer files will appear damage in the past.
Zip file through the socket network transmission, but transfer files will appear damage in the past.
Time:11-29
Problems: I defined a byte array data: byte [], used for temporary read bytes of data, when the size of the byte array is set to 8192 transfer files no problem, should be 8192 bytes BufferedInputStream and BufferedOutputStream the default cache size, when I let the size of the byte array is defined as the 8192 transfer files will appear when the number of the file is damaged, use BufferedInputStream (socket. GetInputStream (), databig); And BufferedOutputStream (socket. GetOutputStream databig); This two constructors should have put the default cache size changed, but the transfer is still in the file is damaged, here I don't know where is wrong, because if the cache size and I define the byte array size doesn't fit, that after I change the default cache size should be able to normal transport, but is not the case, request to explain the great god, thank you,
The receiver code: Import the Java. IO. BufferedInputStream; Import the Java. IO. A DataInputStream; Import the Java. IO. The File; Import the Java. IO. FileOutputStream; The import java.net.ServerSocket; The import java.net.Socket;
Public class Receive { Final static String name="E: \ \ 2. Zip";//receiving storage location file Final static int port=9999; Final static int databig=8192;
Public static void main (String [] args) { The File File=new File (name); Byte [] data=https://bbs.csdn.net/topics/new byte [databig]; Long big=0; Long count=0;
Try { ServerSocket=new ServerSocket server (port); System. The out. Println (" waiting for the sending end connection "); The Socket Socket=server. The accept (); System. The out. Println (" connection success "); file.createNewFile(); FileOutputStream out=new FileOutputStream (file); BufferedInputStream in=new BufferedInputStream (socket. GetInputStream (), databig); A DataInputStream in2=new a DataInputStream (socket. GetInputStream ()); //receive file size Big=in2. ReadLong (); System. The out. Println (big);
While (true) { In. Read (data, 0, the data length); Out. Write (data, 0, the data length); System. The out. Println ((count/big * * 1.0 1.0) * 100); Count +=databig; } } The catch (Exception e) { System. The out. Println (e. oString ()); } System. The out. Println (" receive success "); } }
The sender code: Import the Java. IO. BufferedOutputStream; Import the Java. IO. DataOutputStream; Import the Java. IO. The File; Import the Java. IO. FileInputStream; The import java.net.Socket;
Public class Send { Final static String name="E: \ \ 2. Zip"; Final static String IP="19.168.43.01"; Final static int port=9999; Final static int filebig=8192;
Public static void main (String [] args) { The File File=new File (name); Long big=file. The length (); System. The out. Printf (" total file size (Byte) : "); System. The out. Println (big); Byte [] data=https://bbs.csdn.net/topics/new byte [filebig]; Long count=0;
While (in the read (data, 0, the data length).=1) { System. The out. Printf (" send a file percentage: "); System. The out. Println ((count/big * * 1.0 1.0) * 100); Out. Write (data, 0, the data length); Out. The flush (); Count +=filebig; }
System. The out. Println (" send success "); } The catch (Exception e) { e.printStackTrace(); } } }
CodePudding user response:
Because not receive complete package data, process has been disconnected, you look at the damage of the bales is less than before