Java implementation FTP upload download

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.