Java exec executePUT(DataInputStream sockInp, DataOutputStream sockOutp)

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

Description

execute PUT

License

Open Source License

Declaration

public static void executePUT(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.FileOutputStream;
import java.util.regex.Pattern;

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

    public static void executePUT(DataInputStream sockInp, DataOutputStream sockOutp) {
        // TODO Auto-generated method stub
        try {/*from   ww w  .  j a v a 2s  .  co  m*/
            String fileName = sockInp.readUTF();
            File putFile = new File(executePWD() + File.separator + getFileName(fileName));

            System.out.println(
                    ":::: PUT file being copied file from client directory path :: " + putFile.getAbsolutePath());

            if (putFile.exists()) {
                sockOutp.writeUTF("File Already Exists");
            } else {
                sockOutp.writeUTF("File does not Exist");
            }
            //----begin sending file to client-----
            FileOutputStream fout = new FileOutputStream(putFile);
            int ch;
            do {
                ch = Integer.parseInt(sockInp.readUTF());
                if (ch != -1) {
                    fout.write(ch);
                }
            } while (ch != -1);
            fout.close();
            //-----end----
            sockOutp.writeUTF("Server: File " + fileName + " uploaded successfully");

        } catch (Exception e) {
            System.out.println(":::: Excception in executing PUT 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;
    }

    private static String getFileName(String param) {
        // TODO Auto-generated method stub
        String retFileName = null;
        try {

            if (param.indexOf('\\') > -1) {
                String[] p = param.split(Pattern.quote("\\"));
                retFileName = p[p.length - 1];
            } else if (param.indexOf('/') > -1) {
                String[] p = param.split("/");
                retFileName = p[p.length - 1];
            } else {
                retFileName = param;
            }
        } catch (Exception e) {
            System.out.println("Exception in getFileName Functions" + e.getMessage());
        }
        return retFileName;
    }
}

Related

  1. executeNativeCommand(String command)
  2. executeProcess(final File workDir, final String... termArray)
  3. executeProcess(final String[] cmds)
  4. executeProcess(String command)
  5. executeProcessAndGetOutputAsStringList(final String command)
  6. executeServer()
  7. executeShellCmdAndReturn(String cmd)
  8. executeShellCommand(String command)
  9. executeShellCommand(String command)