my dog learned polymorphism*
The moose likes Sockets and Internet Protocols and the fly likes Problem with InputStream using org.apache.commons.net Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Problem with InputStream using org.apache.commons.net" Watch "Problem with InputStream using org.apache.commons.net" New topic
Author

Problem with InputStream using org.apache.commons.net

samind crasta
Greenhorn

Joined: Nov 03, 2004
Posts: 2
Hi,

I am using org.apache.commons.net to ftp to the remote machine.I am successful in getting the directory and file listing in ftp server.

But when I try to read the properties of a word doucument there is a problem.

The code is given below:

public class Ftp {



public static void main(String[] args) {
try {
FTPClient ftp=new FTPClient();

String server = "172.16.56.216";

String directory = "/main/book/dam";

ftp.connect(server);
ftp.login(args[0], args[1]);
System.out.println("Connected to " + server + ".");

FTPFile[] files = ftp.listFiles(directory);

for (int i = 0; i < files.length; i++) {
checkDiectory(files[i], directory,ftp);
System.out.print(ftp.getReplyString());
}
System.out.print(ftp.getReplyString());

ftp.disconnect();

} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Main Error");
e.printStackTrace();
}

}

/**
* @param file
* @param directory
* @param ftp
*/
public static void checkDiectory(FTPFile file, String directory, FTPClient ftp) {
// TODO Auto-generated method stub
ArrayList resultSet=new ArrayList();

if (file.isDirectory()) {

String dirCont = directory + "/" + file.getName();
System.out.println("Directory" + dirCont);
FTPFile[] fileIn = new FTPFile[512];
try {
fileIn = ftp.listFiles(dirCont);
for (int i = 0; i < fileIn.length; i++) {
checkDiectory(fileIn[i], dirCont,ftp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else if (file.isFile()) {
String dirCont =new String();
dirCont = directory + "/" + file.getName();
System.out.println("File" + dirCont);

try {
/*****The problem is here****/
InputStreamin=ftp.retrieveFileStream(dirCont);
/*****************************/
//It works fine for first word document retrieveFileStream returns null for other word documents.

System.out.println("Stream"+in);
//ExtractionUtil extUtil=new ExtractionUtil();
// String content=new String();
//content=extUtil.getMSDocContent(in);
//System.out.println("Content"+content);
// in.close();
WordDocument word = new WordDocumen(dirCont);
word.getDocumentProperties(in);

//word.getDocumentContent(in);
resultSet.add(word);
in.close();

Iterator it=resultSet.iterator();

Object obj=null;
while(it.hasNext()){
obj = it.next();
if (obj instanceof WordDocument) {
System.out.println("DOC");
Document wor = (Document) obj;
System.out.println("Doc Author : " + wor.getDocumentProperties().getAuthor());
//System.out.println("Doc Content:"+wor.getDocumentContent());
}

}
System.out.print(ftp.getReplyString());
//System.out.println("Author"+properties.getAuthor());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("ERROR");
e.printStackTrace();


}


}

}

}
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
You need to provide a bit more info. For example, WHAT IS THE PROBLEM tht you encounter? I noticed you have a typo:

checkDiectory(fileIn[i], dirCont,ftp);

Shouldn't that be checkDirectory?

Is this the WordDocument class you're talking about?

http://jakarta.apache.org/poi/apidocs/org/apache/poi/hdf/extractor/WordDocument.html
Shreya Kumar
Greenhorn

Joined: Dec 16, 2005
Posts: 3
Hi
I am having a problem with retrieving files that are over (approx) 50 kb using the ftpClient.retrieveFileStream method of org.apache.commons.net.ftp.
It just hangs. I have no problems with smaller files. Also I am able to retrieve the larger files using the ftpClient.retrieveFile method.However I really do not want to write files on the local system.
Has anyone faced this issue and found a solution ?
Thanks
Kurt Kurniawan
Greenhorn

Joined: Jan 03, 2006
Posts: 2
Hello,

I'm having similar problem with Shreya.

Have you found any resolution to the problem yet ?
Would you care to share if you have the solution ?

Thx
Kurt Kurniawan
Greenhorn

Joined: Jan 03, 2006
Posts: 2
Hi all,

just to share the resolution that I have for the problem:

these are the snipets of the code:

FTPClient ftp = new FTPClient();

ftp.connect("abc");
boolean status=ftp.login("xxx","xxx");

System.out.println("login-status:" +status);

status=ftp.changeWorkingDirectory("/xfer/PAYOUT");
System.out.println("chdir-status:" +status);
ftp.setFileType(FTP.ASCII_FILE_TYPE);
ftp.enterLocalActiveMode();

InputStream ios = ftp.retrieveFileStream("bigw_test");
FTPClient ftp2 = new FTPClient();
ftp2.connect("abcd");
status=ftp2.login("xxx","xxx");
System.out.println("login-status abcd:" +status);

status=ftp2.changeWorkingDirectory("/home/wmuser");
System.out.println("chdir-status abcd:" +status);
ftp2.setFileType(FTP.ASCII_FILE_TYPE);

status=ftp2.storeFile("blah.dat", ios);
System.out.println("put file-status:" +status);
boolean commandOK=ftp.completePendingCommand();
System.out.println("pending cmd complete-status egatedev:" +commandOK);

ftp.disconnect();
ftp2.disconnect();
System.out.println("ok");

My original problem was the completePendingCommand method somehow hangs if the file being retrieved is larger than 125 KB. That happened when I placed the completePendingCommand method right after the retrieveFileStream method.

I modified the code by placing the completePendingCommand method at the end of the transfer. Everything seems fine afterwards. But this type of transfer still limited up to about 45 MB.

For file transfer larger than that (45MB), we have to use retrieveFile method.

eq:

FileOutputStream fos=new FileOutputStream("c:/blah.out");
ftp.retrieveFile("bigw_test",fos);

Shreya Kumar
Greenhorn

Joined: Dec 16, 2005
Posts: 3
Kurt
Moving the completePendingCommand worked though I'm not sure why.
Thanks a lot - that really helped
Bharat Athreya
Greenhorn

Joined: Mar 09, 2006
Posts: 10
Hello,

Whn i'm trying to Ftp the file from the remote directory, the code hangs when it encounters boolean commandOK=ftp.completePendingCommand(); statement.

The file size is just 2KB...

Here is the Code Snippet

private void doFtpOnly(FTPClient ftp, FTPFile serverFile) throws Exception
{
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
int count;
byte data[] = new byte[1024];
FileOutputStream fos = null;
Exception exception = null;
try
{
System.out.println("FTP Download: " + serverFile.getName() +
" with timestamp " + df.format(serverFile.getTimestamp().getTime()));

fos = new FileOutputStream("C:/ftp" +File.separator +serverFile.getName());

// stream the ftp contents into an inputstream
// this always returns false
if(!ftp.retrieveFile(serverFile.getName(), fos))
{
// throw new Exception("false return from ftp.retrieveFile()");
System.out.println("false return from ftp.retrieveFile()");
}
/* this command freezes every time when running on Windows */
System.out.println("to ftp.completePendingCommand()");

/*if(!ftp.completePendingCommand())
{
throw new Exception("completePendingCommand() returned false");
}*/

if (!FTPReply.isPositiveCompletion( ftp.getReplyCode() ))
{
System.out.println("complete");
}
else
{
System.out.println("In complete");
}
boolean commandOK=ftp.completePendingCommand();
System.out.println("pending cmd complete-status egatedev:" +commandOK);


System.out.println("did ftp.completePendingCommand()");

}catch(Exception e)
{
System.out.println("doFtpOnly : " + e.getMessage());
e.printStackTrace();
}
finally{
// close input stream
if(fos != null)
fos.close();
System.out.println("FTP end");
}
}


Bharat Athreya
esha sharma
Greenhorn

Joined: Jan 02, 2009
Posts: 6
here is the problem
I am trying to retrieve a zip file from FTP server and after retriveing few MB of data (aprx 22MB) it hangs and doesnt move further... the same code works fine for another files except zip file...
i m using ftpclient.retriveFile(filename, fos)
where filename is name of the file that i m trying to retrieve...n fos is FileOutputStream....
any help would be appreciated..
[ January 02, 2009: Message edited by: esha sharma ]
Neha Sumungla
Greenhorn

Joined: Nov 15, 2011
Posts: 3
I found that Apache retrieveFile(...) sometimes did not work with File Sizes exceeding a certain limit. To overcome that I would used retrieveFileStream() instead. Prior to download I have set the Correct FileType and set the Mode to PassiveMode

So the code will look like

....
ftpClientConnection.setFileType(FTP.BINARY_FILE_TYPE);
ftpClientConnection.enterLocalPassiveMode();
ftpClientConnection.setAutodetectUTF8(true);

//Create an InputStream to the File Data and use FileOutputStream to write it
InputStream inputStream = ftpClientConnection.retrieveFileStream(ftpFile.getName());
FileOutputStream fileOutputStream = new FileOutputStream(directoryName + "/" + ftpFile.getName());
//Using org.apache.commons.io.IOUtils
IOUtils.copy(inputStream, fileOutputStream);
fileOutputStream.flush();
IOUtils.closeQuietly(fileOutputStream);
IOUtils.closeQuietly(inputStream);
boolean commandOK = ftpClientConnection.completePendingCommand();
....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with InputStream using org.apache.commons.net