日期:2014-05-20 浏览次数:20834 次
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class PicClient {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 10007);
FileInputStream fis = new FileInputStream("E:\\壁纸\\05.jpg");
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = s.getOutputStream();
byte bf[] = new byte[1024];
int len = 0;
while ((len = bis.read(bf)) != -1) {
os.write(bf, 0, len);
}
s.shutdownOutput();
BufferedInputStream back = new BufferedInputStream(s.getInputStream());
byte[] bk = new byte[1024];
int lb = back.read(bk) ;
System.out.println(new String(bk, 0, lb));
bis.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class PicServer {
public static void main(String[] args){
try {
ServerSocket ss=new ServerSocket(10007);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.print(ip);
InputStream is=s.getInputStream();
BufferedInputStream bis=new BufferedInputStream(is);
FileOutputStream fos=new FileOutputStream("E:\\05.jpg");
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte[] bt=new byte[1024];
int len=0;
while((len=bis.read(bt))!=-1){
bos.write(bt, 0, len);
}
BufferedOutputStream bac=new BufferedOutputStream(s.getOutputStream());
bac.write("图片传送成功!".getBytes());
bos.close();
ss.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
byte[] bk=new byte[1024];
int lb=0;
int i;
while((i=back.read())!=-1){
lb++;
}
System.out.println(lb);
System.out.println(new String(bk,0,lb));