哥们,图片是字节流,不能用字符流来处理,下面我该的,你用时,改下ip地址,还有图片地址
- import java.io.*;
- import java.net.*;
- class PicClient
- {
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("192.168.0.102",10009);
-
- //读取本地的图片数据
- // BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\1.jpg")));
-
- FileInputStream fis = new FileInputStream("c:\\1.jpg");
-
- //获取Socket的输出流
- // PrintWriter out = new PrintWriter(s.getOutputStream(),true);
- OutputStream out = s.getOutputStream();
-
-
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = fis.read(buf))!=-1)
- {
- out.write(buf,0,len);
- }
- s.shutdownOutput();
-
- //获取socket的输入流,读取服务端的反馈信息并打印
- BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = bufin.readLine();
- System.out.println(str);
- //关闭资源
- fis.close();
- s.close();
- }
- }
- class PicServer
- {
- public static void main(String[] args)throws Exception
- {
- ServerSocket ss = new ServerSocket(10009);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+":"+"has been connected");
-
-
- InputStream bufin = s.getInputStream();
-
- OutputStream out = new FileOutputStream("c:\\2.jpg");
-
-
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = bufin.read(buf))!= -1)
- {
- out.write(buf,0,len);
- }
- //给客户端反馈信息
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("文件上传成功");
-
- //关闭资源
- out.close();
- s.close();
- }
- }
复制代码 |