String id=s.getInetAddress().getHostAddress();
System.out.println(id+"...connection");
InputStream is=s.getInputStream();
FileOutputStream fos=new FileOutputStream("d:\\Server.jpg");
byte[] buf=new byte[1024];
int len=0;
while((len=is.read(buf))!=-1){
fos.write(buf,0,len);
}
OutputStream out=s.getOutputStream();
out.write("上传成功了".getBytes());
fos.close();
s.close();
package com.hbsi.net;
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.2",9009);
FileInputStream fis=new FileInputStream(f);
OutputStream os=s.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
os.write(buf, 0, len);
}
s.shutdownOutput();
InputStream is=s.getInputStream();
byte[] b=new byte[1024];
int num=is.read(b);
System.out.println(new String(b,0,num));
fis.close();
s.close();
}
}