Java exec executeGET(DataInputStream sockInp, DataOutputStream sockOutp)

Here you can find the source of executeGET(DataInputStream sockInp, DataOutputStream sockOutp)

Description

execute GET

License

Open Source License

Declaration

public static void executeGET(DataInputStream sockInp, DataOutputStream sockOutp) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;

public class Main {
    public static final String ROOT = "user.dir";

    public static void executeGET(DataInputStream sockInp, DataOutputStream sockOutp) {
        // TODO Auto-generated method stub
        try {//  www.  j a va 2s .co m
            String fileName = sockInp.readUTF();
            File getFile = null;
            if (fileName.indexOf(File.separator) > -1) {
                getFile = new File(fileName);
            } else {
                getFile = new File(executePWD() + File.separator + fileName);
            }
            //File getFile = new File(fileName);
            System.out.println(":::: getting file from Server path :: " + getFile.getAbsolutePath());
            if (getFile.exists()) {
                sockOutp.writeUTF("READY");//send status1
                System.out.println(":::: Server ready to send the file... ");
                //----begin sending file to client-----
                FileInputStream fin = new FileInputStream(getFile);
                int ch;
                do {
                    ch = fin.read();
                    sockOutp.writeUTF(String.valueOf(ch));
                } while (ch != -1);
                fin.close();
                //-----end----
                sockOutp.writeUTF("Server: File " + fileName + " sent successfully");
            } else {
                sockOutp.writeUTF("File Not Found");//send status2
                System.out.println(":::: GET file does not exist ::::");
            }

        } catch (Exception e) {
            System.out.println("::::Exception in Get file function:: " + e.getMessage());
        }
    }

    public static String executePWD() {
        // TODO Auto-generated method stub
        String currentDirectory = null;
        try {
            currentDirectory = System.getProperty(ROOT);
        } catch (Exception e) {
            System.out.println("----Exception in PWD command execution -----" + e.getMessage());
        }
        return currentDirectory;
    }
}

Related

  1. executeCommandLine(String command)
  2. executeCommandLineReturnAll( final String[] command)
  3. executeCommandLinux(final String _command)
  4. executeDELETE(String parameters)
  5. executeDotCommand(final File dotFile)
  6. executeGetStatus(ProcessBuilder pb)
  7. executeIt(String command)
  8. executeLocalCommand(String[] command)
  9. executeLS()