Please help me out.... I want to upload a file to ftp using my java program. I got this bit of code but its not working.... The file is uploaded to the ftp server but its size is always 0 bytes... What might be the problem??? Code- String server="www.test.com"; String userName="test"; String pw="test"; String fileName = "test.dat"; FtpClient ftpClient = new FtpClient(); try { ftpClient.openServer(server); ftpClient.login(userName, pw); ftpClient.cd("myfolder/test/"); ftpClient.binary(); TelnetOutputStream netOut = ftpClient.put(fileName); File file = new File(fileName); // Now transfer the file contents ObjectInputStream fileIn = new ObjectInputStream(new FileInputStream(file)); byte c[]=new byte[1000]; int read = 0; while ((read = fileIn.read(c)) != -1) { netOut.write(c, 0, read); } fileIn.close(); netOut.close(); ftpClient.closeServer();
public class FtpToServer extends FtpClient { public static int BUFFER_SIZE = 10240; private static FtpClient m_client; private static String host = ""; private static String user = ""; private static String password = ""; private static String targetDataDir = ""; private static String targetResultsDir = ""; private static String sourceDir = ""; private static String sourceFile = ""; public static char SEPARATOR = '/'; public String targetFile = ""; public String ftpStartTime=""; public String ftpEndTime=""; public String servletStartTime=""; public String servletEndTime="";
private String getUniqueId() { String strUniqueId = ""; Date date = new Date(); SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddhhmmssSSSSSS"); strUniqueId = simpledateformat.format(date)+(int)(Math.ceil(Math.random()*1000)); return strUniqueId; }
public String getCurrentDateTime() { Date date = new Date(); SimpleDateFormat simpledateformat = new SimpleDateFormat("hhmmssSSSSSS"); String datetime = simpledateformat.format(date); datetime = datetime.substring(0,2)+":"+datetime.substring(2,4)+":"+datetime.substring(4,6)+"."+datetime.substring(6); return datetime; }