java jftp_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 lists = new ArrayList();

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

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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值