php接收大文件,PHP SCOKET发送大文件,接收端JAVA_PHP教程

此博客展示了如何使用PHP的fsockopen函数与JAVA的ServerSocket和Socket进行交互,实现在客户端通过PHP发送文件到服务端JAVA应用程序,并在服务端接收并保存文件的过程。主要涉及网络编程和文件I/O操作。
摘要由CSDN通过智能技术生成

客户端(PHP):

send.php

[php]

$fp = fsockopen(“127.0.0.1”, 1024, $errno, $errstr, 10);

$filename = ‘2012_07_23.zip’; //要发送的文件

fwrite($fp, $filename . “rn”); //写入文件名 java端用.readLine()..第一行就是文件名

$handle = fopen($filename, “r”);

$contents = fread($handle, filesize($filename));

//fwrite($fp,$contents); //小文件可以这样发,但大文件请分段

$data_size = 1024 * 1; //每次1M

$data_count = ceil( strlen($contents) / $data_size ); //有多少块数据

for( $i = 0; $i < $data_count; $i ++ )

{

$data = substr( $contents, $i * $data_size, $data_size ); //写入到传输socket

fwrite($fp,$data); www.2cto.com

}

fclose($fp);

?>

服务端(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();

//接受客户机的连接请求

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;//连接Web浏览器的socket字

int counter;//计数器

public WebThread(Socket cl,int c){

socket=cl;

counter=c;

}

public void run()//线程体

{

try

{

DataInputStream inputStream = null;

try

{

inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

}catch(Exception e)

{

}

try

{

String savePath = “E:\”;

int bufferSize = 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 BufferedOutputStream(new FileOutputStream(saveFilePath))));

System.out.println(“文件长名为:” + 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(“接收完成,文件存为” + saveFilePath + “n”);

fileOut.flush();

fileOut.close();

inputStream.close();

} catch (Exception e) {

System.out.println(“接收消息错误” + e.toString() + “n”);

return;

}

}catch(Exception e){

}

}

}

作者:junqing124

www.bkjia.comtrueTechArticle客户端(PHP): send.php [php] ?php $fp = fsockopen(127.0.0.1, 1024, $errno, $errstr, 10); $filename = 2012_07_23.zip; //要发送的文件 fwrite($fp, $filename . rn); //写入文…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值