Read and Write from Remote Shared directory

Hello everyone,

I'm working on an enterprise portal. One of the things i was asked to do is access a remote directory (like "\\machine\share\") and be able to, at least, list the files, download to the portal user any of them, and be able to upload some as well. Also my goal is to create dinamycally a directory inside the shared one for every record I insert into a database for, for example, creating a course (each course has an attachment of files with the schedules, curriculum, etc. in pdf or doc files).

Can anyone point me in the right direction on how to star this? are there any API's, packages or something like that that i can use?

Thank you in advance.

[693 byte] By [buckleya] at [2007-10-3 2:11:52]
# 1

Hi,

As long as the machine is in the LAN, you should be able to use java.io.File to carry out any of the operations required.

For example, you should be able to do something like this

import java.io.File;

public class RemoteFile

{

private File myFile;

public RemoteFile()

{

myFile = new File("\\\\deepcore\\CMS");

File[] files = myFile.listFiles();

for (int i = 0; i < files.length; i++)

{

System.out.println(files[i].getName());

}

}

/**

* @param args

*/

public static void main(String[] args)

{

new RemoteFile();

}

}

Similarly you should be able to read/write files from/to the share in the remote location.

Hope this helps.

Thanks,

-Lakshmi-

LakshmiPeshina at 2007-7-14 19:10:50 > top of Java-index,Java Essentials,New To Java...
# 2

I have allready done that type of Project

I use FTP to Connect remote server. there is api call

com.enterprisedt.net.ftp you can download it free

also you can get the document of that API.

there is som sample code

import com.enterprisedt.net.ftp.*;

class remote

{

private String localPath,remotePath;

private FTPClient ftpClient;

public remote()

{

try

{

localPath="localpath to download";

remotePath="remote path from server";

ftpClient = new FTPClient();

FTPMessageCollector listener = new FTPMessageCollector();

ftpClient.setMessageListener(listener);

ftpClient.setRemoteHost(ip of the remote host);

ftpClient.setTimeout(10 * 60 * 1000);

System.out.println("connecting to the Remote Server....");

ftpClient.connect();

System.out.println("connected to the Remote Server ");

ftpClient.login(username,password);

ftpClient.setConnectMode(FTPConnectMode.PASV);

ftpClient.setType(FTPTransferType.ASCII);

downloadFiles();

}

catch(FTPException ftpe)

{

System.out.println(ftpe.toString());

}

catch(Exception e)

{

System.out.println(e.toString());

}

}

public void downloadFiles()

{

try

{

String [] remoteFileList;

String localFileName;

remoteFileList=ftpClient.dir(remotePath + "/");//get the Remote

//file list

for(int j=0;j<remoteFileList.length;j++)

{

( localFileName =localfile //filter only fileneme from remotFileList[J])

ftpClient.get(localPath + localFileName, remoteFileList[j]);

System.out.println("Successfuly Downloaded " +

}

ftpClient.quit();

}

}

catch(Exception e)

{

System.out.println(e.toString());

}

}

}

from Jinath

Message was edited by:

JBN>

JBNa at 2007-7-14 19:10:50 > top of Java-index,Java Essentials,New To Java...
# 3

Thank you very much for your help. I tried to access the shared directory locally and had no problem at all.

But a problem persists. When i deply my code to the portal server, it doen't access the directory because it doen't have the same access rights i have. So, my question is: is there a way to, at the same time i access the shared folder with the File class to send also the user-pass required for it to have access ?

Thank you again

buckleya at 2007-7-14 19:10:50 > top of Java-index,Java Essentials,New To Java...