两个问题:1,是你把文件写入流中之后没有flush();2,要判断一下d:\\2.txt是否存在
客户端:
public class Test22 {
/**
* @param args
* @throws Exception
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, Exception {
// TODO Auto-generated method stub
Socket s = new Socket("192.168.0.6",10000);
BufferedInputStream bfri =
new BufferedInputStream(new FileInputStream("D:\\1.txt"));
BufferedOutputStream buo =
new BufferedOutputStream(s.getOutputStream());
int len;
while((len = bfri.read())!=-1)
{
buo.write(len);
buo.flush();
}
bfri.close();
buo.close();
}
}
服务端:
public class Test23 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10000);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
BufferedInputStream bufi =
new BufferedInputStream(s.getInputStream());
File f = new File("d:\\2.txt");
if(!(f.exists()))
f.createNewFile();
BufferedOutputStream bufo =
new BufferedOutputStream(new FileOutputStream(f));
int len;
while((len = bufi.read())!=-1)
{
bufo.write(len);
bufo.flush();
}
}
} |