本帖最后由 缘木求鱼 于 2012-10-14 20:08 编辑
- 问题在13、37行:
- 为什么缓冲区行不通?
- 注:这个问题有段时间了,我又编辑了遍,复制就能运行的。
- <div class="blockcode"><blockquote>//IPTCPUploadPic.java
- import java.net.*;
- import java.io.*;
- class PicClient{
- public static void main(String[] args)throws Exception{
- Socket s=new Socket("127.0.0.1",1200);
- BufferedReader br=new BufferedReader(new FileReader("end1.bmp"));
- OutputStream os=s.getOutputStream();
- byte[] buf=new byte[1024];
- int len=0;
- while((len=br.read(buf))!=-1)
- os.write(buf,0,len);//?报错?
- s.shutdownOutput();
- InputStream is=s.getInputStream();
- byte[] bufin=new byte[1024];
- int num=is.read(bufin);
- System.out.println(new String(bufin,0,num));
- br.close();
- s.close();
- }
- }
- class PicServer{
- public static void main(String[] args)throws Exception{
- ServerSocket ss=new ServerSocket(1200);
- Socket s=ss.accept();
- String ip=s.getInetAddress().getHostAddress();
- System.out.println("["+ip+"]");
- BufferedReader br=
- new BufferedReader(new InputStreamReader(s.getInputStream()));
- PrintWriter pw=new PrintWriter(new FileWriter("end3.bmp"),true);
- FileOutputStream fos=new FileOutputStream("end3.bmp");
- byte[] buf=new byte[1024];
- int num=0;
- while((num=br.read(buf))!=-1)
- pw.println(num);//?与13行报相同的错?
- PrintWriter pww=new PrintWriter(s.getOutputStream(),true);
- pww.println("upload successful");
- br.close();
- s.close();
- ss.close();
- }
- }
复制代码 |