• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Manipulating files on server

 
Gunja Mehrotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using Apache commons API to connect to a server through FTP.
I want to copy 1 file from one location of server to another.
I am getting the contents of the file using the FTPClient.retrieveFile() function.But now when I want to write these contents to another file on a different location, I am getting error:-
String destPath="/home/gunja/server/myFile.txt";
File ourFile=new File(destPath);
FileOutputStream fop=new FileOutputStream(ourFile);
if(ourFile.exists()){
System.out.println("writing into the file");
fop.write(contents.getBytes());
fop.flush();
fop.close();
}

Here I am getting a FileNotFoundException( java.io.FileNotFoundException: \home\gunja\server\myFile.txt) when creating the file using destPath. It is looking for this path on the local system.
Please help me on this issue.
[ August 04, 2008: Message edited by: Gunja Mehrotra ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Which line of code throws that exception?

I'm assuming that "File ourFile= FileOutputStream fop=new FileOutputStream(ourFile);" is just a typo when you composed this post? Please be sure to PostRealCode.
 
Gunja Mehrotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
yes that mistake I have corrected now.
The line
File ourFile=new File(destPath);
is throwing exception. I want to know if there is a way I can copy the file on server from one location to another.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to upload the file to an FTP server? If so, then you cannot use java.io.File for it. Check out FTPClient.storeFileStream(destPath) - this returns an OutputStream to which you can write.

FTPClient.retrieveFileStream will return an InputStream to read from.
 
Gunja Mehrotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for response but I am not trying to upload from local.I just need to copy some files from one location to another on the same server.Is there a way for that?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileNotFoundException can be thrown for a number of reasons. Maybe the "server" directory doesn't exist, or the process that runs this code doesn't have permissions to access some part of the file path.
 
Gunja Mehrotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No,its not due to missing directory or permissions.
If you look closely its giving java.io.FileNotFoundException:\home\gunja\server\myFile.txt
ie with the inverted slash unlike unix file system, I tried printing the absolute path using ourFile.getAbsolutePath(),it returns:-C:\home\gunja\server\myFile.txt ->i.e. its looking the same path on local machine. How to get away with this?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. I assumed that "/home/gunja/..." was a local directory, because that's the only way you'd be able to access it using the File class.

The FTPClient class has the rename method, which can move files from one directory to another on the server.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you need to copy the file, you still have to use FTPClient.storeFileStream and FTPClient.retrieveFileStream:

Don't forget to use FTPClient.makeDirectory to create the parent folder if needed.
[ August 04, 2008: Message edited by: Rob Prime ]
 
Gunja Mehrotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
I tried the same you sugg
public void copyFile(MyServer server) {

String strSrcPath = "/server/gunja/src/together.txt";
String strDestPath="/server/gunja/dest/together1.txt" ;
InputStream is = null;
OutputStream os = null;
FTPClient client = server.getClient();

try{
is = client.retrieveFileStream(strInBoxPath);
os = client.storeFileStream(strArchivePath);
Util.copyStream(is, os);
}catch(IOException e)
}
e.printStackTrace();
}
finally{
if (is != null)
{
try{
is.close();}
catch(IOException e)
{e.printStackTrace();
}
}
if(os != null)
{try
{os.close();}
catch(IOException e){
e.printStackTrace();
}
}
}


Above code is giving NullPointerException at Util.copyStream(is, os);
this line. I found out that os is null, as I tried printing os and it came null. I am not able to get why its not getting the OutputStream.What is the problem here?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just checked the API, and it seems the file should already exist. To me, that doesn't make much sense - most of the files I upload to an FTP site are new files.

I don't know Commons Net that well, but perhaps you can use FTP.stor() (which is inherited by FTPClient) to create the file, then do the rest.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you wanted to move a file that is on the server already to a different location on the same server?

If so, what's wrong with the FTPClient.rename class I mentioned? Much easier than downloading the file, and then re-uploading it.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that Gunja wants to copy, not move.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'oh! Move along then, nothing to see here. These are not the API calls you're looking for.
[ August 04, 2008: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic