I have written a Java program which when executed would open an upload wizard through which a user can select a file. After doing this, the selected file would be transferred/uploaded to a Unix box to a directory which the user specifies and processed further. Here are the steps/actions:
- Browse the file through the upload wizard
- After this the program would prompt for a new directory to be created, the name of which the user can give as an input
- The program creates a new directory (source) and places the selected file in the created directory of the server
- Show the contents of the transferred file in the source (using cat command)
- Prompt the user for a new directory to be created (target), copy the file from the source to target and subsequently show the contents of the file in the target, all with Linux commands.
I'm able to upload the file to the source (step 3). However, the 4th and the 5th steps don't work. Here's the code:
String cmd1 = "cat" + " " + path + s1 + "/" + file;
System.out.println(cmd1);
((ChannelExec)channel).setCommand(cmd1);
Scanner in2 = new Scanner(System.in);
System.out.println("Enter d target directory");
String s = in2.nextLine();
String path2 = "/e/f/";
String d = path2 + s;
String cmd2 = "mkdir" + " " + path2 + s;
((ChannelExec)channel).setCommand(cmd2);
String src = p + "/" + file;
String cmd3 = "cp" + " " + path + s1 + "/" + file + " " + path2 + s;
((ChannelExec)channel).setCommand(cmd3);
String destpath = d + "/" + file;
String cmd4 = "cat" + " " + path2 + s + "/" + file;
I'm not able to make the program work with variables (for user inputs) in the command. However, hardcoded strings like, for eg. cat /a/b/file
seems to be able to work.
Could any one please help me in this regard?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…