ftp文件上传

package com.iflytek.util;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintStream;

import java.io.UnsupportedEncodingException;

import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

import org.apache.commons.net.ftp.FTPReply;

 

public class FtpUtils

{

public String hostname = "166.0.1.78";

public Integer port = Integer.valueOf(21);

public String username = "hz";

public String password = "hzftp";

public FTPClient ftpClient = null;

 

public void initFtpClient() { this.ftpClient = new FTPClient();

this.ftpClient.setControlEncoding("utf-8");

try {

this.ftpClient.connect(this.hostname, this.port.intValue());

this.ftpClient.login(this.username, this.password);

int replyCode = this.ftpClient.getReplyCode();

System.out.println(replyCode);

if (!FTPReply.isPositiveCompletion(replyCode))

System.out.println("连接ftp成功");

}

catch (SocketException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} }

 

public boolean uploadFile(String pathname, String fileName, String originfilename) throws FileNotFoundException {

boolean flag = false;

InputStream inputStream = null;

try {

inputStream = new FileInputStream(new File(originfilename));

initFtpClient();

this.ftpClient.setFileType(2);

CreateDirecroty(pathname);

this.ftpClient.makeDirectory(pathname);

this.ftpClient.changeWorkingDirectory(pathname);

this.ftpClient.storeFile(fileName, inputStream);

inputStream.close();

this.ftpClient.logout();

flag = true;

} catch (IOException e) {

e.printStackTrace();

}

return flag;

}

 

private void CreateDirecroty(String remote) throws IOException {

boolean success = true;

String directory = remote + "/";

if ((directory.equalsIgnoreCase("/")) && (!changeWorkingDirectory(new String(directory)))) {

int start = 0;

int end = 0;

if (directory.startsWith("/"))

start = 1;

else {

start = 0;

}

end = directory.indexOf("/", start);

String path = "";

String paths = "";

while (true)

try {

String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");

path = path + "/" + subDirectory;

try {

if (!existFile(path)) {

if (makeDirectory(subDirectory))

changeWorkingDirectory(subDirectory);

else

changeWorkingDirectory(subDirectory);

}

else

changeWorkingDirectory(subDirectory);

}

catch (IOException e)

{

e.printStackTrace();

}

paths = paths + "/" + subDirectory;

start = end + 1;

end = directory.indexOf("/", start);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

}

 

private boolean makeDirectory(String dir) throws IOException {

boolean flag = true;

flag = this.ftpClient.makeDirectory(dir);

if (flag)

System.out.println("创建文件成功");

else {

System.out.println("创建文件失败");

}

return flag;

}

private boolean existFile(String path) throws IOException {

boolean flag = false;

FTPFile[] ftpFileArr = this.ftpClient.listFiles(path);

if (ftpFileArr.length > 0) {

flag = true;

}

return flag;

}

private boolean changeWorkingDirectory(String directory) throws IOException {

boolean flag = true;

flag = this.ftpClient.changeWorkingDirectory(directory);

if (flag)

System.out.println("进入文件夹成功");

else {

System.out.println("进入文件夹失败,开始创建文件夹");

}

return flag;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值