控制台信息 



服务器端本次接收长度=4096 
服务器端本次接收长度=4096 
服务器端本次接收长度=4096 
服务器端本次接收长度=4096 
服务器端本次接收长度=-848151048 
服务器端上传文件结束 
java.lang.IndexOutOfBoundsException 
at java.io.BufferedInputStream.read(BufferedInputStream.java:306) 
at java.io.DataInputStream.read(DataInputStream.java:134) 
at boco.com.socket.file.nothing.ServerTread.upLoadFile(ServerTread.java:97) 
at boco.com.socket.file.nothing.ServerTread.run(ServerTread.java:48) /*服务器端*/ 
public class ServerTest_Short {   
    int port = 8821;   
  
    void start() {   
        Socket s = null;   
        try {   
            ServerSocket ss = new ServerSocket(port);   
            while (true) {   
            System.out.println("服务器开始监听"); 
                s = ss.accept(); 
                s.setSoTimeout(10*1000); 
                ServerTread st = new ServerTread(s); 
                st.start(); 
            } 
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
          
    public static void main(String arg[]) {   
        new ServerTest_Short().start();   
    }  
} public class ServerTread extends Thread{ /*套接字对象,由构造函数赋值*/ 
private Socket socket = null; 
/*套接字的输入流,用来接收客户端信息*/ 
    private DataInputStream inputStream = null;  
    /*套接字的输出流,用来向客户端发送信息*/ 
    private DataOutputStream outputStream = null; 
    
public ServerTread(Socket s) 

this.socket = s; 
} /* 
* 子线程运行方法体 
* 作用:从客户端接收报文,判断需要完成的功能 
* 报文格式:int:消息类型 1-上传文件,2-验证文件上传是否成功,3-删除指定文件 
* 根据消息类型,调用不同的方法完成功能 
*/ 
public void run() { 
int messageType = -1; 
   if(socket == null) 
   { 
   System.out.println("socket is null"); 
   return; 
   } 
   try 
   { 
   inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream())); 
   outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); 
   /*读取客户端上送消息类型*/ 
   messageType = inputStream.readInt(); 
   /*1--上传文件*/ 
   if(messageType == 1) 
   { 
   upLoadFile(); 
   } 
   }catch(IOException e){ 
   e.printStackTrace(); 
   } 
}     /* 
     * 上传文件方法 
     * 消息格式:变长文件名(用readUTF实现),long-文件长度,文件流 
     * 流程:1. 从客户端读出文件的绝对路径+文件名,实例化文件输出流对象(FileOutputStream) 
     * 2. 从客户端读出文件的长度 
     * 3. 从客户端读出文件内容,保存在本地服务器 
     */ 
    private void upLoadFile() 
    {    
    System.out.println("服务器端接收到文件上传消息"); 
        try { 
            /*文件绝对路径+文件名,从客户端读出*/ 
            String savePath = "E:\\";   
            /*文件缓冲区大小*/ 
            int bufferSize = 4096; 
            /*文件缓冲区*/ 
            byte[] buf = new byte[bufferSize];   
            /*文件长度*/ 
            long fileLength = 0;   
               
            /*将文件绝对路径+文件名读出*/ 
            savePath += inputStream.readUTF(); 
            
            File file = new File(savePath);             /*声明文件输出流*/ 
            FileOutputStream fileOut = new FileOutputStream(savePath);   
            /*将文件长度读出*/ 
            fileLength = inputStream.readLong();   
               
            System.out.println("文件的长度为:" + fileLength + "\n");   
            System.out.println("开始接收文件!" + "\n"); 
             
            while(true) 
            { 
            int bufSize = inputStream.readInt(); 
            
            System.out.println("服务器端本次接收长度="+bufSize); 
            if(bufSize == -1) 
            { 
            break; 
            } 
            inputStream.read(buf,0,bufSize); 
            fileOut.write(buf,0,bufSize); 
            } 
            
            System.out.println("接收完成,文件存为" + savePath + "\n"); 
            /*关闭文件输出流*/ 
            fileOut.close(); 
            
        } catch (Exception e) {  
        e.printStackTrace(); 
    try { 
socket.close(); 
} catch (IOException e1) { 
// TODO Auto-generated catch block 
e1.printStackTrace(); 
}  
        } 
    System.out.println("服务器端上传文件结束"); 
    return; 
    } 

/*客户端*/ 
public class ClientTest_Short {   
    private ClientSocket_Short cs = null;   
  
    /*IP地址列表*/ 
    private String [] arrIP;   
  
    /*端口*/ 
    private int port; 
    
    /*IP地址*/ 
    private String IPAddress; 
    
    /*配置文件路径*/ 
    private String configFilePath = ""; 
  
    /*套接字对象*/ 
    private Socket socket = null; 
    
    private int repeat; 
  
    /* 
     * 构造方法 
     * 负责调用init方法 
     */ 
    public ClientTest_Short() { 
    init(); 
    } 
    
    /* 
     * 初始化方法 
     * 负责从配置文件中读取IP地址列表及端口 
     */ 
    private void init() 
    { 
    /*配置文件路径,从配置文件中读取服务器端的IP和端口*/ 
    configFilePath = "E:\\EclipseWork\\SocketShortConnection\\WebRoot\\WEB-INF\\system.properties"; 
    /*从配置文件中读取重发次数*/ 
    repeat = Integer.parseInt(SystemUtil.readPropertiesValue(configFilePath, "socket.repeat")); 
    /*从配置文件读取端口*/ 
    port = Integer.parseInt(SystemUtil.readPropertiesValue(configFilePath, "socket.port")); 
    /*从配置文件读取IP地址列表,以","分割*/ 
    String ips = SystemUtil.readPropertiesValue(configFilePath, "socket.ip"); 
    arrIP = ips.split("\\,"); 
    } 
    
    /* 
     * 向服务器端建立连接方法 
     */ 
    private boolean createConnection(String IP,int port) {   
    /*实例化套接字工具类*/ 
        cs = new ClientSocket_Short(IP, port);   
        try { 
        socket = null; 
        /*主动向服务器端建立连接,并返回Socket对象*/ 
            socket = cs.CreateConnection();   
            if(socket != null) 
            { 
            System.out.println("连接服务器成功!IP=[" + IP +"] port=[" + port + "]");   
            } 
            else 
            { 
            System.out.println("socket is null"); 
            } 
            return true;   
        } catch (Exception e) { 
        e.printStackTrace(); 
        System.out.print("连接服务器失败!!!IP=[" + IP +"] port=[" + port + "]");    
            return false;   
        }   
    }           /* 
     * 向服务器端发送文件方法 
     * 消息格式:变长文件名(用readUTF实现),long-文件长度,文件流 
     */ 
    private boolean sendUploadFile(String filePath) 
    { 
    /*文件发送是否成功标志*/ 
    boolean sendFileFlag = false; 
    /*上传文件的输入流*/ 
    DataInputStream fis = null; 
    /*与服务器通信的输出流*/ 
    DataOutputStream ps = null; 
    /*建立连接是否成功标志*/ 
    boolean connectionFlag = false;  
    
    
/*向服务器端建立连接*/ 
connectionFlag = createConnection(IPAddress,port); 
/*如果与服务器端连接未建立成功,则记错误日志,程序退出*/ 
if(connectionFlag == false) 

System.out.println("向服务器[" + IPAddress + ":" + port + "]建立连接失败,要发送的文件名=["+filePath+"]"); 
sendFileFlag = false; 
return sendFileFlag; 
}    
    try 
    { 
    /*实例化文件对象*/ 
            File fi = new File(filePath); 
            /*实例化需要上传文件的输入流*/ 
            fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath))); 
            /*实例化与服务器通信的输出流*/ 
            ps = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); 
            /*向服务器端写入一个int,1-文件上传功能*/ 
            ps.writeInt(1); 
            /*刷新缓冲区*/ 
            ps.flush(); 
            /*向服务器端写入文件名*/ 
            ps.writeUTF(fi.getName()); 
            /*刷新缓冲区*/ 
            ps.flush();   
            /*向服务器写入文件长度*/ 
            ps.writeLong(fi.length()); 
            long fileLength = fi.length(); 
            /*刷新缓冲区*/ 
            ps.flush(); 
            /*定义上传文件的缓冲区长度*/ 
            int bufferSize = 4096; 
            /*开辟缓冲区数组*/ 
            byte[] buf = new byte[bufferSize];   
            long iRemain = fileLength - bufferSize;             while(true) 
            { 
                System.out.println("文件长度="+fileLength+";bufferSize="+bufferSize+";iRemain="+iRemain); 
            if(iRemain < 0) 
            { 
            int read = 0; 
            iRemain = Math.abs(iRemain); 
            
            ps.writeInt((int)iRemain); 
            ps.flush(); 
            read = fis.read(buf, 0, (int)iRemain); 
//            ps.write(buf,0,(int)iRemain); 
            if(read != -1) 
            { 
            System.out.println("read is unequal -1=" + read); 
            ps.writeInt(read); 
            fis.read(buf, 0, read); 
//            ps.write(buf,0,read); 
            } 
            else 
            { 
            System.out.println("read is ok"); 
            } 
            break; 
            } 
            fis.read(buf,0,bufferSize); 
            ps.writeInt(bufferSize); 
            System.out.println("bufferSize ========" + bufferSize); 
            ps.flush(); 
            ps.write(buf); 
            ps.flush(); 
            iRemain = iRemain - bufferSize; 
            } 
            ps.writeInt(-1); 
            /*刷新缓冲区*/ 
            ps.flush(); 
            
            sendFileFlag = true;     }catch(IOException e){ 
    e.printStackTrace(); 
    }finally{ 
           try 
           { 
           ps.flush(); 
               fis.close(); 
               socket.close(); 
           }catch(IOException e){ 
           e.printStackTrace(); 
           } 
    } 
    return sendFileFlag; 
    } 
      public static void main(String arg[]) {   
    ClientTest_Short c = new ClientTest_Short(); 
        c.sendUploadFile("D:\\OOo_3.2.0rc5_20100203_Win32Intel_install_wJRE_zh-CN.exe"); 
    } 
}