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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 沈文杰 中级黑马   /  2013-3-26 17:19  /  2172 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.itheima;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;

  9. class UpLoadThread implements Runnable{
  10.         private Socket s ;
  11.        
  12.         UpLoadThread(Socket s){
  13.                 this.s = s;
  14.         }
  15.        
  16.         public void run(){
  17.                 int count=1;
  18.                 String ip = s.getInetAddress().getHostAddress();
  19.                 try{
  20.                         System.out.println(ip+"::::connect");
  21.                        
  22.                         InputStream ips = s.getInputStream();
  23.                        
  24.                        
  25.                         File file = new File(ip+"("+(count)+")"+".txt");
  26.                         if(file.exists())
  27.                                 file = new File(ip+"("+(count++)+")"+".txt");
  28.                        
  29.                         FileOutputStream fos = new FileOutputStream(file);                       
  30.                         byte[] arr = new byte[1024];
  31.                         int len = 0;
  32.                         while((len = ips.read(arr))!=-1){
  33.                                 fos.write(arr,0,len);
  34.                                
  35.                         }
  36.                        
  37.                         OutputStream ops = s.getOutputStream();
  38.                        
  39.                         ops.write("文件上传成功".getBytes());
  40.                         ops.flush();
  41.                        
  42.                         fos.close();
  43.                         s.close();
  44.                 }
  45.                 catch(Exception e){
  46.                         throw new RuntimeException("文件上传失败!");
  47.                 }       
  48.         }
  49. }

  50. class UpLoadClient{
  51.        
  52.         public static void main(String[] args)throws Exception{
  53.                
  54.                 Socket s = new Socket("127.0.0.1",10009);
  55.                 OutputStream ops = s.getOutputStream();
  56.                
  57.                 FileInputStream fis = new FileInputStream("d:\\demo.txt");
  58.                 byte[] arr = new byte[1024];
  59.                 int len =0;
  60.                 while((len = fis.read(arr))!=-1){
  61.                         ops.write(arr,0,len);
  62.                        
  63.                 }
  64.                 s.shutdownOutput();
  65.                
  66.                 InputStream ips = s.getInputStream();
  67.                 byte[] bt = new byte[1024];
  68.                 int num = ips.read(bt);
  69.                 System.out.println(new String(bt,0,num));
  70.                
  71.         }
  72. }

  73. class UpLoadServer{
  74.         public static void main(String[] args)throws Exception{
  75.                 ServerSocket ss = new ServerSocket(10009);
  76.                
  77.                 while(true){
  78.                         Socket s = ss.accept();
  79.                         new Thread(new UpLoadThread(s)).start();
  80.                 }
  81.        
  82.         }
  83. }
复制代码
运行时抛出上传失败异常了;实际已经上传成功了,但是服务端返回的信息没接收到。。不知道怎么回事啊求解

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

5 个回复

倒序浏览
回复 使用道具 举报
帮帮忙啊
回复 使用道具 举报
是不是你的第42行代码可以去掉啊,这里可以不用刷新了。你试试吧
回复 使用道具 举报
黄玉昆 发表于 2013-3-26 18:57
是不是你的第42行代码可以去掉啊,这里可以不用刷新了。你试试吧

我试过还是不行啊...
回复 使用道具 举报
本帖最后由 贾振凯 于 2013-3-28 22:27 编辑

public class UpLoadThread implements Runnable{
    private Socket s;
    private int count=1;//此处把count改成成员变量用于线程共享记录重名文件数
    /*UpLoadThread(Socket s){
            this.s = s;
    } */
    public void setSocket(Socket s){//在服务器端辅助UpLoadThread对象共享的工具方法
     this.s = s;
    }
    public void run(){
        String ip = s.getInetAddress().getHostAddress();
        try{
            System.out.println(ip+"::::connect");
            InputStream ips = s.getInputStream();
            File file = new File(ip+"("+(count)+")"+".txt");
            while(file.exists()){//此处改为while,可保证即使你服务器重启,依然可以确保文件名称编排连续
                    file = new File(ip+"("+(++count)+")"+".txt");//count++改为++count
            }
            FileOutputStream fos = new FileOutputStream(file);                        
            byte[] arr = new byte[1024];
            int len = 0;
            while((len = ips.read(arr))!=-1){
                    fos.write( arr,0,len);
            }
            
            OutputStream ops = s.getOutputStream();
            ops.write("文件上传成功".getBytes());
            
            fos.close();
            s.close();
        }
        catch(Exception e){
                throw new RuntimeException("文件上传失败!");
        }        
    }
}


class UpLoadClient{

    public static void main(String[] args)throws Exception{
     
        Socket s = new Socket("127.0.0.1",10009);
        
        OutputStream ops = s.getOutputStream();
        FileInputStream fis = new FileInputStream("D:\\demo.txt");
        byte[] arr = new byte[1024];
        int len =0;
        while((len = fis.read(arr))!=-1){
                ops.write(arr,0,len);
        }
        s.shutdownOutput();//调用此方法以后上传线程就可以收到一个-1的读取结束标志位
        InputStream ips = s.getInputStream();
        byte[] bt = new byte[1024];
        int num = ips.read(bt);
        System.out.println(new String(bt,0,num));
        fis.close();
        s.close();
    }
}

public class UpLoadServer{

    public static void main(String[] args)throws Exception{
        ServerSocket ss = new ServerSocket(10009);
      //此处共享,就不用每个线程都在创建一个UpLoadThread对象啦,且可以共享count成员变量
        UpLoadThread ult = new UpLoadThread();
        while(true){
            Socket s = ss.accept();
            ult.setSocket(s);
            new Thread(ult).start();
        }
    }
}



搞了挺长时间也就修了修边边角角的东西,感觉这些细节跟你抛出的那个异常也没啥关系,现在在我这是运行正常啦,另外请教下

s.shutdownOutput();//调用此方法以后上传线程就可以收到一个-1的读取结束标志位


我关于这句话的了解对不对,感觉是这个样子





评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马