您的位置:首页 > 理论基础 > 计算机网络

TCP-多线程并发上传图片

2016-08-16 10:59 260 查看
//TCP-多线程并发上传图片
import java.io.*;
import java.net.*;

/*思路
客户端
1,服务端点.
2,读取客户端已有的图片数据.
3,通过socket 输出流将数据发给服务端.
4,读取服务端反馈信息.
5,关闭
*/
class  PicClient
{
public static void main(String[] args) throws Exception
{
if (args.length!=1)//意思是args只能传1个参数,不是1个参数都是错的
{
System.out.println("请选择一个JPG格式的图片");
return;
}

File file = new File(args[0]);//通过主函数传值的形式为file指定路径

if (!(file.exist() && file.isFile()))
{
System.out.out.println("该对象文件不存在,或者不是文件");
return;
}

if (!file.getName().endsWith(".jpg"))//文件名称末尾.jpg
{
System.out.out.println("该文件不是jpg文件,请重新选择jpg文件");
}

if (file.length()>1024*1024*5)//文件超过5M
{
System.out.out.println("文件超过5M,拒绝上传");
}

Socket s = new Socket("192.168.1.254",10007);
FileInputStream fis = new FileInputStream(file);//文件字节流
OutputStream out = s.getOutputStream();

byte[] buf = new byte[1024];//字节数组缓冲区
int len = 0;//长度
while ((len=fis.read(buf))!=-1)//读取文件字节流fis并写入buf,-1为结束
{
out.write(buf,0,len);
}
s.shutdoenOutput();//结束标记 发送给服务端 表示数据已写完

InputStream in = s.getInputStream();//读取服务端信息
byte[] bufIn = new byte[1024];//字节数组缓冲区
int num = in.read(bufIn);//长度
System.out.println(new String(bufIn,0,num));//new String(byte[],offset,length)转化字节数组byte[]为String

fis.close();//关io流
s.close();//关Socket
}
}

/*服务端
这个服务端有个局限性.当A客户端连接上以后.被服务端获取到.服务端执行具体流程.
这时B客户端连接,只有等待.
因为服务端还没有处理完A客户端的请求,还没有循环回来执行下次accept方法.所以
暂时获取不到B客户端对象.

那么为了可以让多个客户端同时并发访问服务端.
那么服务端最好就是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求.
这就是多线程实现.

如何定义线程呢?
只要明确了每一个客户端要在服务端执行的代码即可,将代码存入run方法
*/

class PicThread implements Runnable
{
private Socket s;//因为要封装客户端Socket s,所以必须先定义成员变量
PicThread(Socket s)//构造器
{
this.s = s;
}
public void run()
{
int count = 1;//定义一个计数器count,命名文件用
String ip = s.getInetAddress().getHostAddress();//IP放try外面,catch里面才能输出IP
try
{
System.out.println(ip+"...connected");

InputStream in = s.getInputStream();//读取Socket流

File file = new File(ip++"("+(count)+")"+".jpg");//防止上传的文件覆盖掉,写个命名规则,封装成File对象file
while (file.exists())//如果file存在,exists()方法 判断file对象是否存在//先封装count为(1),再判断(1)是否存在,是,则count++
file = new File(ip++"("+(count++)+")"+".jpg");//防止上传的文件覆盖掉,写个命名规则,封装成File对象file

FileOutputStream fos = new FileOutputStream(file);//写文件
byte[] buf = new tyte[1024];//缓冲区
int len = 0;
while ((len=in.read(buf))!=-1)//写缓冲区
{
fos.write(buf,0,len);//写文件
}
//s.shutdownOutput();//s调用shutdownOutput() //告诉服务端数据已写完,不然会阻塞accept()

OutputStream out = s.getOutputStream();//给Socket写返回信息
out.write("上传成功!".getByte());//getByte()方法,String转成字节数组byte[]来发送
fos.close();
s.close();
}
catch (Exception e)
{
throw new RuntimeException(ip+"上传失败");//IP放外面,catch里面才能输出IP
}
}
}
class  PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10007);

while (true)
{
Socket s = ss.accept();//拿Socket对象
new Thread(new PicThread(s)).start();//开启线程 //Thread()参数里面放要开的线程PicThread
//构造器里面参数放(s),是因为getInputStream()要用到socket s
}
//ss.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息