- import java.io.*;
- import java.net.*;
- class TcpClient
- {
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("192.168.31.171",10007);
-
- BufferedInputStream bis =
- new BufferedInputStream(new FileInputStream("1.jpg"));
-
- OutputStream out = s.getOutputStream();
- byte[] buf = new byte[1024];
- int len = 0;
- while ((len=bis.read(buf))!=-1)
- {
- out.write(buf,0,len);
- }
-
- s.shutdownOutput();
- BufferedReader bufIn =
- new BufferedReader(new InputStreamReader(s.getInputStream()));
- System.out.println(new String(bufIn.readLine()));
- bis.close();
- s.close();
- }
- }
- /*
- 服务端:
- */
- class TcpServer
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10007);
- Socket s = ss.accept();
- InputStream fis = s.getInputStream();
- FileOutputStream out = new FileOutputStream("Server.jpg");
- byte[] by = new byte[1024];
- int len = 0;
- while((len=fis.read(by))!=-1)
- {
- out.write(by,0,len);
- }
-
- PrintWriter pw2 = new PrintWriter(s.getOutputStream(),true);
- pw2.println("上传成功");
- out.close();
- s.close();
- ss.close();
- }
- }
复制代码 OutputStream out = s.getOutputStream();
上面这行写成下面为什么报错,说不兼容类型?求解
FileOutputStream out = s.getOutputStream(); |