Recently, there is a requirement in the project: when the foreign currency funds in the system are dispatched, a TXT file is generated and the TXT file is sent to another system (Kondor). Generated files naturally use Outputstreamwirter, send files in two ways, one is to write a similar to the FTP function of the program, the other is to use Java to invoke the shell, in the shell to complete the file send operation. We chose the latter, that is, after the completion of the foreign currency funds scheduling work, using Java OutputStreamWriter to generate a TXT file, and then use Java to invoke the shell script, in the shell script to complete the FTP file to the Kondor system work.
The following is a Java program Javashellutil.java
Import Java.io.bufferedreader;import java.io.file;import java.io.fileoutputstream;import java.io.IOException; Import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.outputstreamwriter;import Java.text.dateformat;import Java.text.simpledateformat;import java.util.Date; public class Javashellutil {//basic path private static final String BasePath = "/tmp/";//location of log files recording shell execution (absolute path) Private stat IC final String executeshelllogfile = basepath + "ExecuteShell.log"; The file name (absolute path) of the shell that sent the file to the Kondor system is private static final String Sendkondorshellname = basepath + "sendkondorfile.sh"; public int Executeshell (String shellcommand) throws IOException {int success = 0; StringBuffer StringBuffer = new StringBuffer (); BufferedReader BufferedReader = null;//formatted datetime, logging is used dateformat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm: SS "); try {stringbuffer.append (New Date ())). Append ("Prepare to execute Shell command"). Append (Shellcommand). Append ("\ r \ n" ); Process pid = null; string[] cmd = {"/biN/sh ","-C ", shellcommand};//executes the shell command PID = Runtime.getruntime (). exec (CMD), if (pid! = null) {stringbuffer.append (" process number: "). Append (Pid.tostring ()). Append (" \ r \ n ");//bufferedreader is used to read the output of the shell BufferedReader = new BufferedReader (new InputStreamReader (Pid.getinputstream ()), 1024x768);p id.waitfor ();} else {stringbuffer.append ("no pid\r\n");} Stringbuffer.append (Dateformat.format (New Date ())). Append ("Execution of the shell command \ r \ n \ r \ n"); String line = null;//reads the output from the shell and adds it to StringBuffer while (BufferedReader! = null && (line = Bufferedreader.readline ()) = null) {Stringbuffer.append (line). Append ("\ r \ n");}} catch (Exception IoE) stringbuffer.append ("Exception occurred executing shell command: \ r \ n"). Append (Ioe.getmessage ()). Append ("\ r \ n"); finally {if (BufferedReader! = null) {OutputStreamWriter OutputStreamWriter = null;try {bufferedreader.close ();// Output the execution of the shell to the log file OutputStream outputstream = new FileOutputStream (executeshelllogfile); outputstreamwriter = new OutputStreamWriter (OutputStream, "UTF-8"); Outputstreamwriter.writE (stringbuffer.tostring ());} catch (Exception e) {e.printstacktrace ();} finally {outputstreamwriter.close ();}} Success = 1;} return success;} }
The following is the shell script sendkondorfile.sh, the role of the shell script is the ftp file to the specified location: #!/bin/sh
#日志文件的位置
Logfile= "/opt/fms2_kondor/sendkondorfile.log"
#Kondor系统的IP地址, the generated file will be sent to this address
kondor_ip=192.168.1.200
#FTP用户名
Ftp_username=kondor
#FTP密码
Ftp_password=kondor
#要发送的文件的绝对路径
Filepath= ""
#要发送的文件的文件名
Filename= ""
#如果Shell命令带有参数, the first parameter is assigned to filepath, and the second parameter is assigned to the filename
If [$#-ge "1"]
Then
Filepath=$1
Else
echo "No file path"
echo "no file path \ n" >
>
$logFile
Return
Fi
If [$#-ge "2"]
Then
Filename=$2
Else
echo "No file name"
echo "no file name \ n" >
>
$logFile
Return
Fi
echo "The file to be sent is ${filepath}/${filename}"
CD ${filepath}
LS $fileName
If (test $?-eq 0)
Then
echo "Ready to send file: ${filepath}/${filename}"
Else
echo "File ${filepath}/${filename} does not exist"
echo "File ${filepath}/${filename} does not exist \ n" >
>
$logFile
Return
Fi
Ftp-n ${kondor_ip} <
<
_end
User ${ftp_username} ${ftp_password}
Asc
Prompt
Put $fileName
Bye
_end
echo "' Date +%y-%m-%d '%h:%m:%s ' sent the file ${filepath}/${filename}"
echo "' Date +%y-%m-%d '%h:%m:%s ' sent a file ${filepath}/${filename}\n" >
>
$logFile
The call method is: javashellutil javashellutil = new Javashellutil ();
parameter is the shell command to execute, which is to send the Tmp.pdf file under the/temp directory to 192.168.1.200 by calling the shell script sendkondorfile.sh
int success = Javashellutil.executeshell ("Sh/tmp/sendkondorfile.sh/temp tmp.pdf");
Java Invoke Shell command