• Post Reply Bookmark Topic Watch Topic
  • New Topic

PipedStreams fail for FTP upload a XML file  RSS feed

 
Dennis Meyer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I use JDOM to create an XML Document and want to upload it to FTP as XML file. For upload I use the jakarta ORO Netapi.
As I get it as Outputstream from XML and need an Inputstream to Upload the file I tried to use PipedStreams but I couldn't get it running.
It always "hangs" before os.flush()
THX for all comments!!!
Dennis
code:
ftp.setFileType(FTP.BINARY_FILE_TYPE);
XMLOutputter xmlBuilder = new XMLOutputter(indent, newlines, encoding);
PipedInputStream pin = new PipedInputStream();
PipedOutputStream pout = new PipedOutputStream(pin);
BufferedOutputStream os = new BufferedOutputStream(pout);
ftp.storeFile("remote", pin);
xmlBuilder.output(doc, os);
os.flush();
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you say hang do you mean the program locks up? Also what does your ftp statement look like (ie remote host, dir, etc) these need to be defined in order to complete the ftp. It looks to me like it could be hanging because the stream has nowhere to go.
 
Dennis Meyer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
maybe you're right. I do mean a lockup like you already mentioned. The program just stops at that point. Here is tho whole code:
private boolean ftpUpload(Document doc){
System.out.println("start FTP Upload");
boolean ok = true;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ftpServer);
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
}
else{
System.out.println("Connection established");
//dfUpload... is a SimpleDateFormat
String fileName = ftpFileName + dfUploadFile.format(new java.util.Date()) + ".xml";
ftp.setFileType(FTP.BINARY_FILE_TYPE);
XMLOutputter xmlBuilder = new XMLOutputter(indent, newlines, encoding);
PipedInputStream pin = new PipedInputStream();
PipedOutputStream pout = new PipedOutputStream(pin);
BufferedOutputStream os = new BufferedOutputStream(pout);
ftp.storeFile("remote", pin);
xmlBuilder.output(doc, os);
os.flush();
if(!ftp.completePendingCommand()) {
System.err.println("File transfer failed.");
}
ftp.logout();
ftp.disconnect();
}
} catch(IOException e) {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException ex) {
// do nothing
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
}
System.out.println("end FTP upload");
return ok;
}
 
Dennis Meyer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to say that the variables for the FTP commands (e.g. ftpServer) are Strings and should not be the problem as I can establish a connection without problem. It looks like the Streams cause this problem.
THX, Dennis
 
Dennis Meyer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javascript: x()The PipedStreams are used to syncronize two threads. As I do not couple two independent threads the PipedInputStreams waits for input, but will never get any!!!
Solution:
1) Generate Producer / Consumer Treads and use PipedStreams
2) Generate the whole XML File in Memory and generate onla an Outputstream
As my files are pretty small and the service has to be VERY reliable, I now use solution 2.
Hope this will help anybody in the future
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!