设为首页 收藏本站
查看: 1206|回复: 0

[经验分享] 利用edtftpj组件实现FTP文件的上传

[复制链接]

尚未签到

发表于 2016-6-10 04:33:28 | 显示全部楼层 |阅读模式
  edtftpj:http://www.enterprisedt.com/products/edtftpj/download.html下载
需求描述:把WEB服务器上一个目录中所有的文件上传到指定ftp服务器的某个目录中。
分析:
在edtftpj中有个FTPClient类,此类可以实现ftp的上传。用到的几个函数说明如下:
1、构造函数 FTPClient(ftp服务器,ftp端口)
2、login(ftp登录账号,ftp密码) //登录ftp服务器
3、chdir("文件夹名") //进入ftp上的某个目录,类似DOS的cd命令
4、setType(FTPTransferType.BINARY) //设置传输类型
5、mkdir("文件夹名") //在ftp当前目录下新建一个目录
问题:
利用此组件在测试过程中,发现当上传含有中文的文件名时会失败(FTPClient认为它不是一个文件,奇怪)!
完整程序:
package yhp.test.util;
  import java.io.File;
  import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPTransferType;
  /**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class FTPUpload {
  private String ftpServer;
  private String ftpPort;
  private String ftpUserName;
  private String ftpPassword;
  private FTPClient ftpClient;
  private boolean isLogin = false;
  public FTPUpload(String pFtpServer, String pFtpPort, String pFtpUserName,
String pFtpPassword) throws Exception {
this.ftpServer = pFtpServer;
if(pFtpPort.trim().equals(""))
this.ftpPort="21";
else
this.ftpPort = pFtpPort;
if(pFtpUserName.trim().equals(""))
this.ftpUserName ="Anonymous";
else
this.ftpUserName = pFtpUserName;
this.ftpPassword = pFtpPassword;
try {
ftpClient = new FTPClient(ftpServer, Integer.parseInt(ftpPort));
ftpClient.login(ftpUserName, ftpPassword);
ftpClient.chdir("//");//在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
isLogin = true;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
  //上传指定文件夹到ftp服务器上
public String uploadFolder(String folderName,String ftpPath)throws Exception{
if (isLogin) {
String strMsg="";
try{
File file=new File(folderName);
if(file.isDirectory()){
ftpClient.chdir("//");
ftpClient.setType(FTPTransferType.BINARY);
if (checkFolderIsExist(ftpPath)) {
ftpClient.chdir(ftpPath);
} else {
createFolder(ftpPath);
}
if(!checkFolderIsExist(file.getName())){
ftpClient.mkdir(file.getName());
}
ftpClient.chdir(file.getName());
ftpPath=ftpPath+"//"+file.getName();
File[] files=file.listFiles();
for(int i=0;i<files.length;i++){
if(files.isDirectory()){
uploadFolder(files.getPath(),ftpPath);
}else{
if(files.isFile()){
try{
ftpClient.put(files.getPath(),files.getName());
}catch(Exception ee){
strMsg+="upload file<<:"+files.getPath()+">> error!Message:"+ee.getMessage()+"/r/n";
}
}
}
}
if(!strMsg.equals("")){
throw new Exception(strMsg);
}
}else{
throw new Exception(folderName+" is not a folder'name!");
}
}catch(Exception e){
strMsg+=e.getMessage()+"/r/n";
}
return strMsg;
} else {
throw new Exception("you didnot login remote ftp server!");
}
}

//把指定目录下所有的文件上传到ftp服务器上
public void uploadAllFilesInFolder(String folderName,String ftpPath)throws Exception{
if (isLogin) {
String strMsg="";
try{
File file=new File(folderName);
if(file.isDirectory()){
ftpClient.chdir("//");
ftpClient.setType(FTPTransferType.BINARY);
if (checkFolderIsExist(ftpPath)) {
ftpClient.chdir(ftpPath);
} else {
createFolder(ftpPath);
}
File[] files=file.listFiles();
for(int i=0;i<files.length;i++){
if(files.isFile()){
try{
ftpClient.put(files.getPath(),files.getName());
}catch(Exception ee){
strMsg+="upload file<<:"+files.getPath()+">> error!Message:"+ee.getMessage()+"/r/n";
}
}
}
}else{
throw new Exception(folderName+" is not a folder'name!");
}
}catch(Exception e){
throw new Exception(e.getMessage());
}
} else {
throw new Exception("you didnot login remote ftp server!");
}
}

//上传指定文件到ftp服务器上
public void uploadFile(String clientFileName, String ftpPath)
throws Exception {
if (isLogin) {
try {
//获取文件名
String filename = "";
int index = clientFileName.lastIndexOf("//");
filename = clientFileName.substring(index + 1);
ftpClient.chdir("//");
ftpClient.setType(FTPTransferType.BINARY);
if (checkFolderIsExist(ftpPath)) {
ftpClient.chdir(ftpPath);
} else {
createFolder(ftpPath);
}
ftpClient.put(clientFileName, filename);
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
  // 检查FTP服务器上文件夹是否存在
public boolean checkFolderIsExist(String pFolder) throws Exception {
if (isLogin) {
String folder=pFolder.trim();
if(folder.startsWith("//"))
folder=folder.substring(1);
if(folder.endsWith("//"))
folder=folder.substring(0,folder.length()-1);
String strLayer="..";
if(folder.indexOf("//")>0){
String[] folders=folder.split("////");
for(int i=1;i<folders.length;i++){
strLayer+=",";
}
}
boolean result=false;
try {
ftpClient.chdir(folder);
ftpClient.chdir(strLayer);
result=true;
} catch (Exception ex) {
result=false;
}
return result;
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
  //创建远程FTP服务器文件夹
public void createFolder(String pFolder) throws Exception {
if (isLogin) {
if (checkFolderIsExist(pFolder) == false) {
try {
String path = "";
ftpClient.chdir("//");
String[] folders = pFolder.split("////");
for (int i = 0; i < folders.length; i++) {
try {
ftpClient.chdir(folders);
} catch (Exception ex) {
ftpClient.mkdir(folders);
ftpClient.chdir(folders);
}
}
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
}
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
  public static void main(String[] args) {
try{
FTPUpload ftp=new FTPUpload("192.168.0.176","21","","");
ftp.uploadAllFilesInFolder("D://my driver//Sony DSC","yhp");
  }catch(Exception e){
System.out.println(e.getMessage());
}
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-228343-1-1.html 上篇帖子: org.apache.commons.net.ftp.FTPSClient 的使用 下篇帖子: Java常用FTP文件操作说明 Apache.FTPClient,ftp4j,jftp
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表