黑马程序员技术交流社区

标题: tcp 文件上传 问题 [打印本页]

作者: qixing0918    时间: 2013-11-2 13:33
标题: tcp 文件上传 问题
客户端
  1. public class TCPClientPic {

  2. public static void main(String[] args) throws Exception{

  3. Socket socket = new Socket("127.0.0.1",20001);

  4. InputStream is = socket.getInputStream();
  5. OutputStream os = socket.getOutputStream();

  6. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("d:\\star.jpg")));
  7. BufferedOutputStream bosout = new BufferedOutputStream(os);
  8. BufferedInputStream bisin = new BufferedInputStream(is);
  9. int len = 0;
  10. while((len = bis.read())!=-1){
  11. bosout.write(len);
  12. }
  13. socket.shutdownOutput();
  14. byte[] buf = new byte[30];
  15. len = bisin.read(buf);
  16. System.out.println(Arrays.toString(buf));
  17. System.out.println(new String(buf) );


  18. bis.close();
  19. bisin.close();
  20. bosout.close();
  21. socket.close();

  22. }
  23. }
复制代码
服务器端
  1. public class TCPReceivePic {

  2. public static void main(String[] args) throws Exception {
  3. ServerSocket ss = new ServerSocket(20001);

  4. Socket socket = ss.accept();
  5. System.out.println(socket.getInetAddress().getHostAddress());

  6. InputStream is = socket.getInputStream();
  7. OutputStream os = socket.getOutputStream();
  8. BufferedInputStream bisin = new BufferedInputStream(is);
  9. BufferedOutputStream bosout = new BufferedOutputStream(os);
  10. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
  11. new File("d:\\copyPid.jpg")));

  12. int len = 0;
  13. while ((len = bisin.read()) != -1) {
  14. bos.write(len);
  15. }
  16. bosout.write("nihao".getBytes());

  17. bisin.close();
  18. bos.close();
  19. bisin.close();
  20. socket.close();
  21. ss.close();

  22. }
  23. }
复制代码
客户端结果  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
也就是说我 在服务器端返回的nihao 没有返回 怎么回事 求解!!!!!!











作者: 酱爆    时间: 2013-11-2 19:32
服务器加上bosout.flush()
作者: qixing0918    时间: 2013-11-2 21:19
忘了 bisin.close();

bos.close();

bisin.close();  写了俩个 忘关bosout了 我说的么  谢了
作者: 狼王    时间: 2013-11-3 07:31
加油哈,好好努力,为了黑马




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2