views:

368

answers:

2

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
A: 

By default, Windows Services run under the LocalSystem account, which does not have network access. Reconfigure your service to use an account that has access to your FTP site, such as NetworkService, or an account you create.

thx for the swift answer :DI'm not using java code for connecting to the ftp server. The code is in the batch script. Beside that, I created one more service for download from ftp server, and that application works well as a service. I even tried run first 4 lines of code alone (the upload part) as a windows service and it worked. I'm pretty confident that the problem is in the rest of the code.Is there any problem with windows services creating new processes (im calling batch script as a new process)? Could that be a problem?
Eedoh
A: 

I don't have a better way of getting the results back from the child process, but I have had success using ftp without creating a child process. The Apache Commons Net package has methods to do the FTP from within the java program.

G_A
Thanx a lot. I tried this library and it worked perfectly well on my testing environment, now I have better control of process. However, I'm having problems connecting to production server. That client's server is running old OS/400 operating system. It seems it is using some old syntax and doesn't recognize standard commands (it allows me only to login). Do you have any experience related to this?
Eedoh
I have only used this against windows servers and it worked well. I see in the javadoc that there is a configure which takes an FTPClientConfig which has predefined settings including SYST_OS400. Have you tried that?
G_A
Yeah, I've figoured it out, right after I asked you the question :D. You were very helpful, thanks again. I'm really sorry I can't vote for your answer, because I'm newbie here, and don't have reputation of 15 :(
Eedoh
If this is the answer you accept as the one that gave you a solution, you should click the little check mark under the vote arrows. It's similar to voting, but more importantly it will help others to know that the question has been answered.
G_A