本帖最后由 应国炎 于 2012-3-19 20:58 编辑
我看毕老师的视频,自己用Buffered...存储后,和直接用FileInputStream有很大区别。2.14M的图片,Buffered..拷的只有1.94M,一直找不到原因- import java.net.*;
- import java.io.*;
- class TcpClient
- {
-
- public static void main(String aas[]) throws Exception
- {
-
-
-
- Socket ds= new Socket("192.168.1.106",10010);
- OutputStream out=ds.getOutputStream();
-
- /*Buffered...........................................................................................................................................................................................*/
- BufferedReader bu=new BufferedReader(new InputStreamReader(new FileInputStream("165.jpg")));
- String line=null;
-
- while((line=bu.readLine())!=null)
- {
- out.write(line.getBytes());
- //out.flush();
- }
-
- /*直接FileInputStream
- FileInputStream bu=new FileInputStream("165.jpg");
-
- byte[] a=new byte[1024];
- int num1=0;
- while((num1=bu.read(a))!=-1)
- {
- out.write(a,0,num1);
- }
- */
-
- ds.shutdownOutput();
- InputStream in=ds.getInputStream();
- byte[] bufIn=new byte[1024];
- int num=in.read(bufIn);
- System.out.println(new String (bufIn,0,num));
- bu.close();
- ds.close();
- in.close();
-
- }
-
- }
- class TcpServer
- {
- public static void main(String add[]) throws Exception
- {
-
- ServerSocket ss=new ServerSocket(10010);
- Socket s=ss.accept();
- InputStream in=s.getInputStream();
- FileOutputStream fos=new FileOutputStream("server.jpg");
-
- byte[] buf=new byte[1024];
- int len=0;
- while((len=in.read(buf))!=-1)
- {
- fos.write(buf,0,len);
- }
- OutputStream out =s.getOutputStream();
- out.write("上传成功".getBytes());
-
-
- fos.close();
- s.close();
- ss.close();
- //out.close();
- }
- }
复制代码 |