PHPSCOKET sends large files, and the receiving end is JAVA. Client (PHP): send. php [php]? Php?fpfsockopen(127.0.0., 1024,javaserrno,javaserrstr,10%%%%filename2012_07_23.zip; file to be sent fwrite ($ fp, $ filename. rn); write file client (PHP ):
Send. php
[Php]
$ Fp = fsockopen ("maid", 1024, $ errno, $ errstr, 10 );
$ Filename = '22_07_23.zip '; // file to be sent
Fwrite ($ fp, $ filename. "\ r \ n"); // write the file name in java. readLine () .. the first line is the file name.
$ Handle = fopen ($ filename, "r ");
$ Contents = fread ($ handle, filesize ($ filename ));
// Fwrite ($ fp, $ contents); // small files can be sent in this way, but large files must be segmented.
$ Data_size = 1024*1; // 1 M each time
$ Data_count = ceil (strlen ($ contents)/$ data_size); // how many pieces of data are there?
For ($ I = 0; $ I <$ data_count; $ I ++)
{
$ Data = substr ($ contents, $ I * $ data_size, $ data_size); // write data to the transport socket
Fwrite ($ fp, $ data); www.2cto.com
}
Fclose ($ fp );
?>
Server (JAVA ):
MyApp. java
[Java]
Import java. io .*;
Import java.net .*;
Import java. util. Date;
Import java. SQL .*;
Public class MyApp
{
Private int x;
Public MyApp ()
{
X = 0;
}
Public static void main (String args []) {
Int I = 1, port = 1024;
ServerSocket server = null;
Socket client = null;
Try {
Server = new ServerSocket (port );
System. out. println ("Web Server is listening on port" + server. getLocalPort ());
For (;;){
Client = server. accept ();
// Accept client connection requests
New WebThread (client, I). start ();
I ++;
}
} Catch (Exception e) {System. out. println (e );}
}
}
WebThread. java
[Java]
Import java. io .*;
Import java.net .*;
Import java. util. Date;
Class WebThread extends Thread {
Socket socket; // The socket character connecting to the Web browser
Int counter; // counter
Public WebThread (Socket cl, int c ){
Socket = cl;
Counter = c;
}
Public void run () // thread body
{
Try
{
DataInputStream inputStream = null;
Try
{
InputStream = new DataInputStream (new BufferedInputStream (socket. getInputStream ()));
} Catch (Exception e)
{
}
Try
{
String savePath = "E :\\";
Int buffer size = 8192;
Byte [] buf = new byte [bufferSize];
String filename = new String ();
Filename = inputStream. readLine ();
String saveFilePath = new String ();
SaveFilePath = savePath + "\" + filename;
DataOutputStream fileOut = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (saveFilePath ))));
System. out. println ("File name:" + filename );
While (true)
{
Int read = 0;
If (inputStream! = Null)
{
Read = inputStream. read (buf );
}
If (read =-1 ){
Break;
}
// System. out. println (buf. toString ());
FileOut. write (buf, 0, read );
}
System. out. println ("received successfully, the file is saved as" + saveFilePath + "\ n ");
FileOut. flush ();
FileOut. close ();
InputStream. close ();
} Catch (Exception e ){
System. out. println ("receive message error" + e. toString () + "\ n ");
Return;
}
} Catch (Exception e ){
}
}
}
Author: junqing124
Sending (PHP): send. php [php]? Php $ fp = fsockopen (127.0.0.1, 1024, $ errno, $ errstr, 10); $ filename = 2012_07_23.zip; // file to be sent fwrite ($ fp, $ filename. \ r \ n); // write text...