当前位置: 代码迷 >> Java相关 >> Socket如何传送多个文件??
  详细解决方案

Socket如何传送多个文件??

热度:358   发布时间:2008-01-10 09:26:18.0
Socket如何传送多个文件??
我写了一个传送多个文件的程序,可结果却是把多个文件都写在一个文件里去了,请问如何把文件分开来写入??

主要代码如下:

server:
程序代码:
    public void run() {
        try {
            // get out and in stream
            DataOutputStream dout = new DataOutputStream(s.getOutputStream());
            DataInputStream din = new DataInputStream(s.getInputStream());
            dout.writeUTF("{FileList}" + fileList);
            FileInputStream fis;
            DataInputStream dataIn;
            int length = 0;
            ta.append(din.readUTF() + "\n");
            state = "send";
            
            // send file one by one
            for(File f : fileList) {                
                dout.writeUTF("{FileBegin}" + f);
                fis = new FileInputStream(f);
                dataIn = new DataInputStream(fis);
                byte[] buffer = new byte[1024];// 定义一byte类型的缓冲区

                // send buffer length once
                ta.append("File:  " + f + " send begin!\n");
                while ((length = dataIn.read(buffer)) != -1) {
                    dout.write(buffer, 0, length);
                }
                ta.append("File:  " + f + " send end!\n");
                
                dout.flush();
                fis.close();
                dataIn.close();
            }
            din.close();
            dout.close();
            s.close();

            state = "rest";
        }
        catch(Exception exc) {
            exc.printStackTrace();
        }        
    }
client:
程序代码:
    public void receiveFile() {
        state="receive";
        
        try {
            dout.writeUTF("{ReceiveFiles}");

            // receive each file through one new socket
            for(int i=0; i<list.length; i++) {
                // read file name
                String str = din.readUTF();
                int j=str.lastIndexOf("\\");
                
                // create file output
                File f = new File(path.getPath() + "\\" + str.substring(j+1));
                if(!f.exists())
                    f.createNewFile();
                FileOutputStream fos = new FileOutputStream(f);
                int length=0;
                byte[] buffer = new byte[1024];
                
                // read file buffer and write it into file f
                ta.append("File:  " + f + " receive begin!\n");
                while ((length = din.read(buffer)) != -1) {
                    fos.write(buffer, 0, length);
                }
                
                fos.close();
                ta.append("File:  " + f + " receive end!\n");
            }
            din.close();
            socket.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        
        state="rest";
    }
搜索更多相关的解决方案: Socket  文件  quot  din  get  

----------------解决方案--------------------------------------------------------
自己定一个协议,以发送之前告诉文件名,以及文件的长度
等读完那些长度的字节以后,再读新的文件名,以及新的文件长度就可以了
----------------解决方案--------------------------------------------------------
原帖由 [bold][underline]千里冰封[/underline][/bold] 于 2008-1-10 13:43 发表 [url=http://bbs.bc-cn.net/redirect.php?goto=findpost&pid=1176925&ptid=196796][/url]
自己定一个协议,以发送之前告诉文件名,以及文件的长度
等读完那些长度的字节以后,再读新的文件名,以及新的文件长度就可以了

哦,那这协议自己定,,,额,,,试试 哈
----------------解决方案--------------------------------------------------------
可以了,呵呵
----------------解决方案--------------------------------------------------------
楼主,怎么做的啊,能不能说一下?
----------------解决方案--------------------------------------------------------
  相关解决方案