Package excelwitxt;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.ArrayList;
public class ftp{
Public String Ftppath = "/home/mw/weblogic/temp";
Public String ftpname = "WebLogic";
Public String FTPPassword = "WebLogic";
Public String Ftpserverip = "128.5.80.4";
String S1 = null;
String filepath = null;
String filename = null;
String s2 = null;
Public DataObject UploadFile (DataObject DataObject) throws exception{
try{
FtpClient ftpclient = new FtpClient (Ftpserverip);
Ftpclient.login (Ftpname,ftppassword);
Ftpclient.binary ();
if (path.length!=0) {
FTPCLIENT.CD (path);
}
Ftpclient.binary ();
Telnetoutputstream OS = null;
FileInputStream is = null;
try{
OS = Ftpclient.put (remotefilename);
File file_in = new file (LocalFilename);
is = new FileInputStream (file_in);
byte[] bytes = new byte[8092];
int C;
while ((C=is.read (bytes))!=-1) {
Os.write (BYTES,0,C);
}
System.out.println ("Upload success");
}catch (Exception e) {
E.printstacktrace ();
}finally{
Is.close ();
Os.close ();
}
}catch (IOException ex) {
Ex.printstacktrace ();
}
}
Public ftpfilebfo[] Queryfile () throws exception{
list<ftpfilebfo> lists = new arraylist<ftpfilebfo> ();
int i = 0;
try {
FtpClient ftpclient = new FtpClient (Ftpserverip);
Telnetinputstream is = null;
Ftpclient.login (Ftpname,ftppassword);
Ftpclient.binary ();
SYSTEM.OUT.PRINTLN ("Login Success");
byte[] bytes = new byte[1024];
if (Path.length ()!=null) {
FTPFILEBFO f = new FTPFILEBFO ();
String atestr = data.substring (42,55). Trim ();
String fileName = data.substring (56,data.length ()). Trim ();
F.setfilename (FileName);
F.setfiledate (DATESTR);
Lists.add (I,F);
i++;
} catch (Exception e) {
E.printstacktrace ();
}
ftpfilebfo[] files = new Ftpfilebfo[i];
for (int j=0; j<i; J + +) {
FTPFILEBFO f = new FTPFILEBFO ();
String Filen = Lists.get (j). GetFileName ();
String FileD = Lists.get (j). Getfiledate ();
F.setfilename (Filen);
F.setfilename (FileD);
FILES[J] = f;
}
return files;
}
}
Public DataObject downloadFile (DataObject DataObject) throws exception{
try {
FtpClient ftpclient = new FtpClient (Ftpserverip);
Telnetinputstream tis = null;
FileOutputStream OS = null;
Ftpclient.login (Ftpname,ftppassword);
Ftpclient.binary ();
String Path = "/home/mw/weblogic/temp";
if (Path.length ()!=null) {
FTPCLIENT.CD (path);
}
tis = Ftpclient.get (selectedfname);
OS = new FileOutputStream (file1);
byte[] bytes = new byte[1024];
int C;
while ((C=tis.read (bytes))!=-1) {
Os.write (BYTES,0,C);
}
Os.close ();
Tis.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Java implementation FTP upload download