com/scalagent/joram/mom/dest/ftp/TransferImplRef.java

00001 /*
00002  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
00003  * Copyright (C) 2001 - 2007 ScalAgent Distributed Technologies
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or any later version.
00009  * 
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00018  * USA.
00019  *
00020  * Initial developer(s): Nicolas Tachker (ScalAgent)
00021  * Contributor(s): 
00022  */
00023 package com.scalagent.joram.mom.dest.ftp;
00024 
00025 import java.io.*;
00026 import java.net.*;
00027 
00028 public class TransferImplRef 
00029   implements TransferItf {
00030 
00031   public String getFile(String protocol,
00032                         String host,
00033                         int port,
00034                         String user,
00035                         String pass,
00036                         String remotePath, 
00037                         String localPath, 
00038                         String remoteFileName,
00039                         String localFileName,
00040                         String type,
00041                         long crc) throws Exception {
00042     
00043     StringBuffer sb = new StringBuffer();
00044     
00045     sb.append("ftp://");
00046     sb.append(user);
00047     sb.append(":");
00048     sb.append(pass);
00049     sb.append("@");
00050     sb.append(host);
00051     if (port > -1) {
00052       sb.append(":");
00053       sb.append(port);
00054     }
00055     sb.append("/");
00056     if (remotePath != null)
00057       sb.append(remotePath + "/");
00058     sb.append(remoteFileName);
00059     sb.append(";type=");
00060     sb.append(type);
00061 
00062     URL url = new URL(sb.toString());
00063 
00064     URLConnection urlc = url.openConnection();
00065     InputStream is = urlc.getInputStream();
00066     
00067     File file = new File(localPath,localFileName);
00068     
00069     BufferedOutputStream bos = new BufferedOutputStream(
00070       new FileOutputStream(file));
00071     
00072     int c = is.read();
00073     while (c != -1) {
00074       bos.write(c);
00075       c = is.read();
00076     }
00077     bos.flush();
00078     bos.close();
00079     is.close();
00080 
00081     if (crc > 0 && crc != file.length())
00082       throw new Exception("CRC ERROR.");
00083 
00084     return file.getAbsolutePath();
00085   }
00086 }

Generated on Tue Sep 16 16:14:22 2008 for joram by  doxygen 1.5.0