读书人

在Java中实现TCP协议编程续之实现文本

发布时间: 2012-09-16 17:33:17 作者: rapoo

在Java中实现TCP协议编程续之实现文本和图片的上传代码

上传文本文件:

服务器端:

package com.hbsi.tcp;

import java.net.*;
import java.io.*;

public class UploadServer {

/**
* @param args
*/
public static void main(String[] args) throws Exception{

ServerSocket ss=new ServerSocket(9009);

Socket s=ss.accept();

String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"....connection");

BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));

PrintWriter pw=new PrintWriter(new FileWriter("c:\\server.txt"),true);

String line=null;

while((line=brin.readLine())!=null){
/*if(line.equals("over"))
break;*/
pw.println(line);
}

//向客户端发出上传成功提示
PrintWriter pwout=new PrintWriter(s.getOutputStream(),true);
pwout.println("上传成功");

pw.close();
s.close();
ss.close();

}

}

客户端:

package com.hbsi.tcp;
import java.net.*;
import java.io.*;

public class UploadClient {

/**
* @param args
*/
public static void main(String[] args) throws Exception{

Socket s=new Socket("192.168.49.234",9009);

//读取要上传的文本文件,为了提高效率,使用缓冲

BufferedReader br=new BufferedReader(new FileReader("d:\\2.txt"));

//将读到的文件内容输出到服务器端
PrintWriter pwout=new PrintWriter(s.getOutputStream(),true);

String line=null;

while((line=br.readLine())!=null){
pwout.println(line);
}
//pwout.println("over");

s.shutdownOutput();

//读取服务端发过来的成功提示
BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=brin.readLine();
System.out.println(str);


br.close();
s.close();

}

}

上传图片:

服务器端:

package com.hbsi.tcp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class JpgServer{

/**
* @param args
*/
public static void main(String[] args) throws Exception{

ServerSocket ss=new ServerSocket(9009);

while(true){

Socket s=ss.accept();

new Thread(new JpgThread(s)).start();
}




//ss.close();

}

}


class JpgThread implements Runnable{

private Socket s;

public JpgThread(Socket s){
this.s=s;
}

@Override
public void run() {
int count=1;
try{
String ip=s.getInetAddress().getHostAddress();

System.out.println(ip+"---connection");
InputStream in=s.getInputStream();

File dir=new File("c:\\picture");

File f=new File(dir,ip+"("+count+").jpg");

while(f.exists())
f=new File(dir,ip+"("+(count++)+").jpg");

FileOutputStream fos=new FileOutputStream(f);

byte[] buf=new byte[1024];

int len=0;

while((len=in.read(buf))!=-1){
fos.write(buf,0,len);
}

OutputStream out=s.getOutputStream();
out.write("上传成功了".getBytes());

fos.close();

s.close();
}catch(Exception e){
e.printStackTrace();
}

}

}

客户端:

package com.hbsi.tcp;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class JpgClient{

/**
* @param args
*/
public static void main(String[] args)throws Exception {

if(args.length==0){
System.out.println("请指定一个jpg文件");
return;
}
File f=new File(args[0]);

Socket s=new Socket("192.168.49.234",9009);

FileInputStream fis=new FileInputStream(f);

OutputStream out=s.getOutputStream();

byte[] buf=new byte[1024];

int len=0;

while((len=fis.read(buf))!=-1){
out.write(buf, 0, len);
}

s.shutdownOutput();

InputStream in=s.getInputStream();
byte[] b=new byte[1024];
int num=in.read(b);
System.out.println(new String(b,0,num));

fis.close();

s.close();

}

}

读书人网 >编程

热点推荐