aspose file tools*
The moose likes Java in General and the fly likes Manipulating files on server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Manipulating files on server " Watch "Manipulating files on server " New topic
Author

Manipulating files on server

Gunja Mehrotra
Greenhorn

Joined: Jul 17, 2008
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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.


Ping & DNS - my free Android networking tools app
Gunja Mehrotra
Greenhorn

Joined: Jul 17, 2008
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gunja Mehrotra
Greenhorn

Joined: Jul 17, 2008
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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

Joined: Jul 17, 2008
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Jul 17, 2008
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

The problem is that Gunja wants to copy, not move.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Manipulating files on server