套接字服务器下载文件夹包含但不包含特定文件

时间:2014-06-17 17:47:20

标签: java android sockets

我在这里遇到麻烦,在这里我建立了这个Socket服务器来下载文件,它下载文件夹里面的文件,但它没有下载指定的文件名,返回null,你能帮助我,我应该在哪里更改这个服务器这样做了吗?

    sOutput = new ObjectOutputStream(socket.getOutputStream());
            sInput  = new ObjectInputStream(socket.getInputStream());
            // read the username
            OutputStream os = socket.getOutputStream(); 

            DataOutputStream dos = new DataOutputStream(os);

            String Request = (String) sInput.readObject();  
            System.out.println("request is:"+Request);


                  String[] todoname=Request.split("\\@reza-hp");
                  String name=todoname[0];
       System.out.println("Connecting...");  



     File filcheck = new File("D://Users//ProfileImages//"+name+"
 //"+"ProfileImage,imagechange_1,"+name+",.jpg"); 
     System.out.println(filcheck);
     if (filcheck.exists()){

        fil = new File("D://Users//ProfileImages//"+name+"
 //"+"ProfileImage,imagechange_1,"+name+",.jpg"); 
     }else{

        fil = new File("D://Users//Default//"); 
     }
       System.out.println(fil);
       File[] Files=fil.listFiles();  
       System.out.println(Files);
       for (int count=0;count < Files.length;count ++){  
           System.out.println(Files[count].getName());  

       }  

       os = socket.getOutputStream();    
       dos = new DataOutputStream(os);   

       dos.writeInt(Files.length);  

       for (int count=0;count<Files.length;count ++){  
             dos.writeUTF(Files[count].getName());  

       }  
       for (int count=0;count<Files.length;count ++){  

             int filesize = (int) Files[count].length();  
             dos.writeInt(filesize);  
       }  



       for (int count=0;count<Files.length;count ++){  

        int filesize = (int) Files[count].length();  
       byte [] buffer = new byte [filesize];  

       FileInputStream fis = new FileInputStream(Files[count].toString());    
       BufferedInputStream bis = new BufferedInputStream(fis);    

       //Sending file name and file size to the server    
       bis.read(buffer, 0, buffer.length); //This line is important  
       dos.write(buffer, 0, buffer.length);     

       fis.close();
       dos.flush();   

       //close socket connection  
      // socket.close();  

   }  



               // Toast.makeText(getApplicationContext(),"Transfer file is  
 completed!!", Toast.LENGTH_LONG).show();   

       dos.close();
       os.close(); 
       //socket.close();  

感谢

1 个答案:

答案 0 :(得分:0)

修好了,在这里我发布了singe文件下载器,问题是文件包含来自任何类型,图像,音乐,视频的下载,两者都非常适合谁需要使用,祝你好运。

    String Type;
    Socket socket;
    ObjectInputStream sInput;
    ObjectOutputStream sOutput;
    // my unique id (easier for deconnection)
    int id;



    // Constructore
    ClientThread(Socket socket) throws InterruptedException {
        // a unique id
        id = ++uniqueId;
        this.socket = socket;
        /* Creating both Data Stream */
        System.out.println("Thread trying to create Object Input/Output 
  Streams");
    while (!jobdone){
        try
        {
            // create output first
            sOutput = new ObjectOutputStream(socket.getOutputStream());
            sInput  = new ObjectInputStream(socket.getInputStream());
            // read the username
            OutputStream os = socket.getOutputStream(); 

            DataOutputStream dos = new DataOutputStream(os);

            String Request = (String) sInput.readObject();  
            System.out.println("request is:"+Request);


                  String[] todoname=Request.split("\\@reza-hp");
                  String name=todoname[0];
       System.out.println("Connecting...");  



     File filcheck = new File("D://Users//ProfileImages//"+name+"
 //"+"ProfileImage,imagechange_1,"+name+",.jpg"); 
     System.out.println(filcheck);
     if (filcheck.exists()){

        fil = new File("D://Users//ProfileImages//"+name+"
 //"+"ProfileImage,imagechange_1,"+name+",.jpg"); 
     }else{

        fil = new File("D://Users//Default//"); 
     }
       System.out.println(fil);
     //  File[] Files=fil.listFiles();  

  //     for (int count=0;count < Files.length;count ++){  
           System.out.println(fil.getName());  

    //   }  

       os = socket.getOutputStream();    
       dos = new DataOutputStream(os);   

       dos.writeInt(1);  

    //   for (int count=0;count<Files.length;count ++){  
             dos.writeUTF(fil.getName());  

     //  }  
     //  for (int count=0;count<Files.length;count ++){  

             int filesize = (int) fil.length();  
             dos.writeInt(filesize);  
    //   }  



    //  for (int count=0;count<Files.length;count ++){  


       byte [] buffer = new byte [filesize];  

       FileInputStream fis = new FileInputStream(fil.toString());    
       BufferedInputStream bis = new BufferedInputStream(fis);    

       //Sending file name and file size to the server    
       bis.read(buffer, 0, buffer.length); //This line is important  
       dos.write(buffer, 0, buffer.length);     

       fis.close();
       dos.flush();   

       //close socket connection  
      // socket.close();  





               // Toast.makeText(getApplicationContext(),"Transfer file is  
completed!!", Toast.LENGTH_LONG).show();   

       dos.close();
       os.close(); 
       //socket.close();  


   }