public class PicClient {
public static void main(String[] args) throws Exception {
//
Socket s = new Socket("192.168.1.126",10001);
FileInputStream fis = new FileInputStream("c:\\1.bmp");
OutputStream fos = s.getOutputStream();
int len = 0;
byte[] buf = new byte[1024];
while((len = fis.read(buf))!=-1){
fos.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
public class PicServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10001);
while(true){
Socket s = ss.accept();
new Thread(new ServerThread(s)).start();
}
//ss.close();
}
}
public class ServerThread implements Runnable{
private Socket s ;
ServerThread(Socket s){
this.s = s;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
FileOutputStream os = new FileOutputStream("1.jpg");
InputStream in = s.getInputStream();
int len = 0;
byte[] buf = new byte[1024];
while((len = in.read(buf))!=-1){
os.write(buf,0,len);
}
OutputStream outPut = s.getOutputStream();
outPut.write("上传成功".getBytes());
os.close();
s.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |