Download files from a server to a client

1

I can not download the file and of course the wrong line is in the FileInputStream part, I can not get the client to receive the file he wants, any suggestions to download a file from a server to a client? without apache libraries.

public void rename (String filename){
        File f = new File(path + filename);
        try{
            if(f.exists()){
                System.out.println("Whats the new name?");
                String newname = input.nextLine();
                File newFile = new File(path + newname);
                f.renameTo(newFile);
                System.out.println(">>Rename has been done");
                }
            else
                System.out.println(">>The rename couldnt complete");    }//try
        catch(Exception e){e.printStackTrace();}        
    }//rename

    public void download (String filename){
        File f = new File(path + filename);
        try{if(f.exists()){
         System.out.println("");
         System.out.println("Starting");
         System.out.println(">>Receiving file: "+ filename + "  " );
         System.out.println(">>Please wait...");
            FileInputStream fileInputStream=new FileInputStream(f);
            FileOutputStream fileOutputStream=new FileOutputStream(f);
         byte[] b=new byte[1024];
         while((fileInputStream.read(b))>0)fileOutputStream.write(b);
         fileOutputStream.flush();
         fileOutputStream.close();
         fileInputStream.close();
              System.out.println(">>Task completed!");  }//if
        else{
            System.out.println(">>File not found"); }
        }catch(Exception e){e.printStackTrace();}
    } 

and this is the server class

public class Server {
public static final int PORT = 9000;
static Scanner input = new Scanner (System.in); 
static Commands opeFiles = new Commands();
static int max = 10; // Maximo de conexiones simultaneas

public static void main(String[] args) {
        ServerSocket ss = null; 
        Socket s = null;
    try {
        ss = new ServerSocket(PORT, max);
        System.out.println("- -Kush Server - -");
        while(true){
            s = ss.accept();
            System.out.println("");
            System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
            System.out.println("SERVER WORKING ON ADDRESS -> " + s.getInetAddress().getHostName() + "/" + PORT);
                ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
            String msg = (String)ois.readObject();
            System.out.println("From Client >> " + msg);
            System.out.println("C:>" + msg);
            String[] cmds = msg.split(" ");
            File f = new File(cmds[1]);
            switch(cmds[0]){
            case "create":  
                opeFiles.create(cmds[1]);
                oos.writeObject("File Created");
                break;
            case "read":
                String content = opeFiles.read(cmds[1]);
                if(f.exists()){
                oos.writeObject(content);
                }
                else{
                    oos.writeObject("File not found");
                }
                break;
            case "delete":
                opeFiles.delete(cmds[1]);
                try{
                    if(f.exists()){    
                    oos.writeObject("File Deleted");
                    }
                    else{
                        oos.writeObject("File not found");
                    }   
                }//try
                catch(Exception e){e.printStackTrace(); }
                break;
            case "rename":
                opeFiles.rename(cmds[1]);
                break;
            case "download":
                opeFiles.download(cmds[1]);
                oos.writeObject(f);
                break;
            default:
                System.out.println("Undefined Operation");
                oos.writeObject("Undefined Operation");
                break;
            }//case
        }//while
    }catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try {
            s.close();
            ss.close();
        } catch (Exception e) {
            e.printStackTrace();;
        }
}   

} }

    
asked by Eduardo Gonzalez 17.04.2017 в 01:00
source

0 answers