java实现ftp上传下载
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();
}
}
}