Results 1 to 2 of 2
  1. #1
    ajmerasunny is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default Archiving File on Shared Folder in Linux using java code

    Hi All,


    I have a requirement wherein I have to ftp a file (Feed.txt) from windows FTP server to Linux machine1 and archive the same file (with timestamp suffixed on file name like Feed.txt_22April) on Linux Machine2. I am

    trying to achieve this through java code. I am able to ftp the file(Feed.txt) from windows ftp server to Linux machine1 /opt/ftproot directory and archiving the same file under /opt/ftproot/archive directory of Linux

    Machine1 with timestamp suffixed on file name.

    My archiving requirement is still not achieved. So, for that I created a shared folder “/temp/shared” on Linux Machine2 and mounted the /temp/shared folder of Linux Machine2 on /opt/ftproot/archive folder of

    Linux Machine1 using following command:

    # mount LinuxMachine2: /temp/shared /opt/ftproot/archive

    Note: I have given full permission on both the folders.

    Logically, archiving the Feed.txt with timestamp in it name under /opt/ftproot/archive folder should create the file in /temp/shared folder of Linux Machine2. But instead of archiving the file on

    /opt/ftproot/archive folder, it archive the file on /opt/ftproot folder.

    In my java code, I have specified copyDir as “/opt/ftproot” and archiveDir as /opt/ftproot/archive”.

    The code is being run from Linux Machine1 as a schedule task.

    Please help.

    PFB the code snippet:

    package com.ftp;

    import com.thortech.xl.scheduler.tasks.SchedulerBaseTask;

    import org.apache.commons.net.ftp.;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;

    import java.util.Calendar;
    import java.util.Date;


    public class FTPService extends SchedulerBaseTask {
    private static String targetDir="";
    private static String archiveDir="";

    public void init() {
    }

    public void execute() {
    try {
    String server = "Machine1

    String username = "username";

    String password = "password";

    String sourceDir = "C:\\Inetpub\\ftproot\\temp.txt";

    //String archiveDir = "/home/ftproot1";

    targetDir = getAttribute("Target Directory");

    FTPClient ftp = new FTPClient();

    /************************************

    Connect to the server

    /
    ftp.connect(server);

    ftp.login(username, password);

    System.out.println("Connected to " server ".");

    String[] names = ftp.listNames();

    for (String name : names) {
    System.out.println("Name = " name);
    }

    /**************************************

    Get all file names that are present in the current working directory

    /
    FTPFile[] files = ftp.listFiles();
    String fileName = "";

    for (int i = 0; i < files.length; i+) {
    if (files[i].isFile()) {
    System.out.println("\t" files[i].getName());

    File file = new File(targetDir File.separator
    files[i].getName());

    FileOutputStream fos = new FileOutputStream(file);

    /************************************************** ****

    Copy the file to target system

    /
    ftp.retrieveFile(files[i].getName(), fos);

    fos.close();


    fileName = files[i].getName();
    }
    }

    ftp.logout();

    ftp.disconnect();



    System.out.println("End of FTP....");
    System.out.println("Calling fileCopy()");
    System.out.println("Target Directory"+targetDir);
    fileCopy(fileName,targetDir);

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static String getDate() {
    String sdate = "";

    try {
    System.out.println("Inside getDate()");
    Calendar date = Calendar.getInstance();

    SimpleDateFormat formatter = new SimpleDateFormat(
    "yyyy-MM-dd HH:mm:ss.0");

    sdate = formatter.format(date.getTime());
    } catch (Exception e) {
    }

    return sdate;
    }

    public void moveFile(String t) {
    try{
    System.out.println("inside moveFile");

    System.out.println("Moving Files");

    File fro = new File(t);
    archiveDir = getAttribute("Archive Directory");
    System.out.println("Archiving Directory"+archiveDir);

    File to = new File(archiveDir);
    boolean archived = fro.renameTo(new File(to, fro.getName()));

    if (archived) {
    System.out.println("FileArchived");
    }


    /String cmd="mv" " "t " " "/home/ftproot/archive";
    System.out.println("Archive Directory"+archiveDir);
    System.out.println("Move Command"+cmd);
    Process SshProc = (Runtime.getRuntime()).exec(cmd);

    /



    } catch (Exception e) {
    e.printStackTrace();
    }
    }



    public void fileCopy(String filename,String Directory) {
    try{
    //String ifile="/home/ftproot/sunny.txt";
    String ifile=targetDir"/"filename;
    String ofile=ifile"_"+getDate();
    File f1 = new File(ifile);

    File f2 = new File(ofile);
    InputStream in = new FileInputStream(f1);

    //For Append the file.
    // OutputStream out = new FileOutputStream(f2,true);

    //For Overwrite the file.
    OutputStream out = new FileOutputStream(f2);

    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0){
    out.write(buf, 0, len);
    }
    in.close();
    out.close();
    System.out.println("File copied.");
    moveFile(ofile);
    }catch(Exception e)
    {
    System.out.println("File copied."+e);
    }

    }


    }

    Cheers,
    Sunny

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    In my java code, I have specified copyDir as “/opt/ftproot” and archiveDir as /opt/ftproot/archive”.
    When the shared folder on machine2:/temp/shared as the /opt/ftproot/archive , when you use /opt/ftproot as the copyDir, wouldn't files get put there, instead of into tthe ../archive/ folder (as parent paths of the mounted folder will resolve to the machine1 filesystem)

Similar Threads

  1. code to create a folder in java
    By radhika123 in forum New To Java
    Replies: 7
    Last Post: 07-21-2011, 12:21 PM
  2. Folder Creation On Shared Hosting Account
    By arpitgadle in forum Java Servlet
    Replies: 2
    Last Post: 10-03-2009, 03:04 PM
  3. Creating Folder in JAVA and Copying File
    By fnoman in forum New To Java
    Replies: 8
    Last Post: 10-18-2008, 08:33 AM
  4. Access Linux shared folder from Windows OS
    By nitinpjairaj in forum Networking
    Replies: 3
    Last Post: 08-26-2008, 07:14 PM
  5. how to access shared file/folder in LAN
    By ksheetiz in forum Networking
    Replies: 1
    Last Post: 05-24-2008, 05:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •