A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 薆情媬証書 于 2013-10-23 09:16 编辑

亲,帮忙看看,这段代码运行也没问题,可是执行后 ,却没有创建新文件,不知是哪里出了问题??
  1. <p> import java.net.*;
  2. import java.io.*;</p><p>class PicClient
  3. {
  4. public static void main(String[] args)throws Exception
  5. {
  6.   Socket s = new Socket(InetAddress.getByName("192.168.1.100"),20000);
  7.   
  8.   FileInputStream fis = new FileInputStream("d:\\1.jpg");</p><p>  OutputStream os= s.getOutputStream();</p><p>  byte[] buf = new byte[1024];
  9.   int len =0;
  10.   while ((len =fis.read(buf)) !=-1)
  11.   {
  12.    os.write(buf, 0 ,len);
  13.   }
  14.   s.shutdownOutput();</p><p>  InputStream is = s.getInputStream();
  15.   len = is.read(buf);
  16.   System.out.println(new String(buf,0,len));
  17. }
  18. }</p><p>class PicServe
  19. {
  20. public static void main(String[] args) throws Exception
  21. {
  22.   ServerSocket ss = new ServerSocket(20000);
  23.   while (true)
  24.   {
  25.    Socket s = ss.accept();</p><p>   new Thread(new PicServeThread(s)).start();
  26.   }
  27.   
  28. }
  29. }</p><p>
  30. class PicServeThread implements Runnable
  31. {
  32. private Socket s;
  33. PicServeThread(Socket s)
  34. {
  35.   this.s=s;
  36. }</p><p> public void run()
  37. {
  38.   File file = new File(s.getInetAddress().getHostAddress()+".jpg");
  39.    
  40.    try
  41.    {
  42.     if(!file.exists())
  43.      file.createNewFile();
  44.     InputStream is = s.getInputStream();</p><p>    FileOutputStream fis = new FileOutputStream(file);</p><p>    byte[] buf = new byte[1024];
  45.     int len = 0;
  46.     while ((len =is.read(buf))!=-1)
  47.     {
  48.      fis.write(buf,0,len);
  49.     }
  50.     System.out.println(s.getInetAddress().getHostAddress()+"上传了"+file.toString());
  51.     OutputStream os = s.getOutputStream();
  52.     os.write("上传成功!".getBytes());
  53.    }
  54.    catch (Exception e)
  55.    {
  56.     throw new RuntimeException("上传失败");
  57.    }
  58.    </p><p> }
  59. }</p>
复制代码
求指点!!!

评分

参与人数 1黑马币 +3 收起 理由
李江 + 3 你这代码写的,谁看了都头疼啊

查看全部评分

6 个回复

倒序浏览
该文件运行后正确的结果应该是有一个图片文件生成的!
回复 使用道具 举报
你D:\\下面有1.jpg这个文件吗?

点评

有啊  发表于 2013-10-23 08:58
回复 使用道具 举报
亲,要注意排版!
回复 使用道具 举报
代码看过了,没有问题,

但是估计你几个地方的参数有问题

问题1:你的ip是否正确192.168.1.100→192.168.0.100
问题2:端口号也有可能被其他程序占用,换一个试试  例如:20000→20001
问题3:源图片文件是否存在  d://1.jpg

如果上述问题都排除那么会在文件目录下生成192.168.0.100.jpg

代码粘帖如下:
  1. import java.net.*;
  2. import java.io.*;

  3. class PicClient {

  4.         public static void main(String[] args) throws Exception {
  5.                 Socket s = new Socket(InetAddress.getByName("192.168.0.100"), 20000);

  6.                 FileInputStream fis = new FileInputStream("d:\\1.jpg");
  7.                 OutputStream os = s.getOutputStream();
  8.                 byte[] buf = new byte[1024];
  9.                 int len = 0;
  10.                 while ((len = fis.read(buf)) != -1) {
  11.                         os.write(buf, 0, len);
  12.                 }
  13.                 s.shutdownOutput();
  14.                 InputStream is = s.getInputStream();
  15.                 len = is.read(buf);
  16.                 System.out.println(new String(buf, 0, len));
  17.         }
  18. }

  19. class PicServe {
  20.         public static void main(String[] args) throws Exception {
  21.                 ServerSocket ss = new ServerSocket(20000);
  22.                 while (true) {
  23.                         Socket s = ss.accept();
  24.                         new Thread(new PicServeThread(s)).start();
  25.                 }

  26.         }
  27. }

  28. class PicServeThread implements Runnable {
  29.         private Socket s;

  30.         PicServeThread(Socket s) {
  31.                 this.s = s;
  32.         }

  33.         public void run() {
  34.                 File file = new File(s.getInetAddress().getHostAddress() + ".jpg");

  35.                 try {
  36.                         if (!file.exists())
  37.                                 file.createNewFile();
  38.                         InputStream is = s.getInputStream();
  39.                         FileOutputStream fis = new FileOutputStream(file);
  40.                         byte[] buf = new byte[1024];
  41.                         int len = 0;
  42.                         while ((len = is.read(buf)) != -1) {
  43.                                 fis.write(buf, 0, len);
  44.                         }
  45.                         System.out.println(s.getInetAddress().getHostAddress() + "上传了"
  46.                                         + file.toString());
  47.                         OutputStream os = s.getOutputStream();
  48.                         os.write("上传成功!".getBytes());
  49.                 } catch (Exception e) {
  50.                         throw new RuntimeException("上传失败");
  51.                 }
  52.         }
  53. }
复制代码

点评

谢谢,不过你说的这三个问题,我都有注意到过,都没问题!!不知为什么!  发表于 2013-10-23 09:00
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马