I'm using java code for uploading some files using FTP. When I compile and run the code everything works perfectly, but if I launch it as a windows service using Java Service Launcher, it doesn't connect to the FTP server at all (it just does the rest of the job, that is moving files to archive folder).
Btw, is there any better way for testing child process's output, than writing it's output to a file, than parsing the file content? Here's the code:
Runtime runtime = Runtime.getRuntime();
String[] cmd = {"c:\\ftp\\putskripta.bat"};
Process p1 = runtime.exec(cmd);
p1.waitFor();
File izlaz = new File("C:\\ftp\\izlaz.txt");
int arrlen = 10000;
byte[] infile = new byte[arrlen];
FileInputStream fis = new FileInputStream(izlaz);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
int filelength = dis.read(infile);
String filestring = new String(infile, 0, 10000);
CharSequence[] sekvenca = {"Invalid command", "Not connected"};
if (!filestring.contains(sekvenca[0]) && !filestring.contains(sekvenca[1]))
{
File uploads = new File("C:\\ftp\\Uploads");
File[] uploadfiles = uploads.listFiles();
int godina = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
int mjesec = Calendar.getInstance().get(Calendar.MONTH)+1;
int dan = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
for (int i = 0; i < uploadfiles.length; i++) {
if (uploadfiles[i].getName().startsWith("ARTST") || uploadfiles[i].getName().startsWith("BESTE") || uploadfiles[i].getName().startsWith("LAGER") || uploadfiles[i].getName().startsWith("AVISE") || uploadfiles[i].getName().startsWith("KUNDE") || uploadfiles[i].getName().startsWith("BORDE") || uploadfiles[i].getName().startsWith("ENTLA")) {
File destinacijaFoldera = new File("C:\\ftp\\MovedUploads\\" + godina + "\\" + mjesec + "\\" + dan);
File destinacijaFajla = new File("C:\\ftp\\MovedUploads\\" + godina + "\\" + mjesec + "\\" + dan + "\\" + uploadfiles[i].getName());
if (!destinacijaFoldera.isDirectory()) {
destinacijaFoldera.mkdirs();
}
File temp = new File(destinacijaFoldera, uploadfiles[i].getName() + "_" + String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) + String.valueOf(Calendar.getInstance().get(Calendar.MINUTE)) + String.valueOf(Calendar.getInstance().get(Calendar.SECOND)));
if (!destinacijaFajla.exists()) {
uploadfiles[i].renameTo(destinacijaFajla);
}
else{
uploadfiles[i].renameTo(temp);
}
uploadfiles[i].renameTo(new File(destinacijaFoldera, uploadfiles[i].getName()));
}
}
}
else {izlaz.delete(); throw new Exception("Neuspio pokusaj uploada");}
izlaz.delete();
Just in case, here is the code for "putskripta.bat"
@echo off
cd c:\ftp\Uploads
ftp -s:c:\ftp\putkomande.txt -i localhost > c:\ftp\izlaz.txt