- <p>class PicClient</p><p>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) throws IOException</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>Socket s = new Socket("169.254.22.63",10003);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>FileInputStream fis = new FileInputStream("F:\\线程生命周期.png");</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>OutputStream os = s.getOutputStream();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>byte[] arr = new byte[1024];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int len = 0;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>while((len=fis.read())!=-1){</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>os.write(arr,0,len);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>//s.shutdownOutput();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>InputStream is = s.getInputStream();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>byte[] arr1 = new byte[1024];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int in = is.read(arr1);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(new String(arr1,0,in));</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>is.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>fis.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>s.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p>}</p><p>class PicServer</p><p>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) throws IOException</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>ServerSocket ss = new ServerSocket(10003);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>Socket s = ss.accept();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>InputStream is = s.getInputStream();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>FileOutputStream os = new FileOutputStream("pic.png");</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>byte[] arr = new byte[1024];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int len =0;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>while((len=is.read())!=-1){</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>os.write(arr,0,len);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>OutputStream os1 = s.getOutputStream();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>os1.write("上传成功".getBytes());</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>os.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>s.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>ss.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p>}</p>
复制代码 运行完后 客户端能看到服务器打的上传成功 也建立文件了,但文件为空 这是为什么?
麻烦帮忙看下
|
|