上一篇中的那个FTP上传的代码只是适合Window下面的,而今天这个改进就可以即适合Window下,也适合Linux下了。因为我们做的CMS系统是放到Linux下面的,所以当时欠缺考虑,才导致后面的很多的麻烦。
另外我还想在说一点:(1)String ftpWorkingPath = ftpClient.printWorkingDirectory(); 这里是在FTP上打印出当时的路径,也不知道是什么原因FTP默认的把大写的字母全部转化成小写的了,这点是我以前不知道的,也是我从新遇到的新的问题当时一直在找原因,结果被我发现是这个大小写的原因。(2)所以最后要比较equals的话就必须都转化成小写的字母来比较和来截取都必须统一的用小写或者统一的用大写。例如:
int firstIndex = ftpWorkingPath.trim().toLowerCase().indexOf(webSiteName.trim().toLowerCase());
另外还要去掉空格trim(),以前的代码没有用这些,当时没有报错或者错位(也不知道是什么原因没有报错或者错位),但是这次有出现错位,所以在找原因,最后发现是大小写原因。以前的代码是:
int firstIndex = ftpWorkingPath.indexOf(webSiteName);
因为ftpWorkingPath=“/windowtest/aa/bb/cc/” ,而webSiteName="windowTest",所以firstIndex=-1,这个就是最终的原因所在了。(3)另外还有就是在"(指针)跳到上层目录"的时候:
if(finalTarget.trim().toLowerCase().equals(ftpWorkingPath.trim().toLowerCase())){
System.out.println("jumping again ..."); ftpClient.changeToParentDirectory();
}
一定要大小写一致。
下面是所有的FTP上传整个目录或者项目代码:当然这是最核心的代码,另外还有些相关的代码我没有粘贴出来,如果有需要的朋友可以找到我的QQ。
//window and linux
public void uploadManyFile(FTPClient ftpClient,File localFilePath,String localPath,String webSiteName){
try{
System.out.println(localFilePath.getName()+"*******");
System.out.println("localPath:"+localPath);
System.out.println("localFilePath:"+localFilePath.getName()+"-"+localFilePath.getAbsolutePath());
System.out.println("webSiteName:"+webSiteName);
File fileList[] = localFilePath.listFiles();
//null directory
if(fileList.length <= 0){
ftpClient.changeToParentDirectory();
System.out.println("AbsolutePathList Size 1:"+AbsolutePathList!=null?AbsolutePathList.size():null);
for(int m=0;m<AbsolutePathList.size();m++){
for(int k=0;k<AbsolutePathList.size();k++){
String target = AbsolutePathList.get(k).substring(localPath.length()-2, AbsolutePathList.get(k).length());
int sign = target.lastIndexOf(fileName.get(k));
String finalTarget = target.substring(0, sign-1);
finalTarget = finalTarget.replace("\\", "");
String ftpWorkingPath = ftpClient.printWorkingDirectory();
if(ftpWorkingPath.length() > webSiteName.length()){
int firstIndex = ftpWorkingPath.trim().toLowerCase().indexOf(webSiteName.trim().toLowerCase());
ftpWorkingPath = ftpWorkingPath.substring(firstIndex+webSiteName.length(), ftpWorkingPath.length());
}
ftpWorkingPath = ftpWorkingPath.replace("/", "");
System.out.println("target"+k+"="+target+" sign="+sign+" finalTarget="+finalTarget);
System.out.println("ftpWorkingPath"+k+"="+ftpWorkingPath);
if(finalTarget.trim().toLowerCase().equals(ftpWorkingPath.trim().toLowerCase())){
System.out.println("jumping again");
ftpClient.changeToParentDirectory();
}
}
}
}
for(int i=0;i<fileList.length;i++){
File upfile = (File)fileList[i];
if(upfile.isDirectory()){
System.out.println(upfile.getName()+"---");
System.out.println("jinlai");
if(i+1 == fileList.length){
System.out.println("aa3-absolute="+upfile.getAbsolutePath());
AbsolutePathList.add(upfile.getAbsolutePath());
fileName.add(upfile.getName());
}
ftpClient.makeDirectory(upfile.getName());
ftpClient.changeWorkingDirectory(upfile.getName());
uploadManyFile(ftpClient,upfile,localPath,webSiteName);
}else{
System.out.println(localFilePath.getName()+"****&&***");
System.out.println(upfile.getName()+"--+++");
System.out.println(localFilePath.getPath()+"\\"+upfile.getName());
String localFileRealPath = localFilePath.getPath()+"/"+upfile.getName();
uploadRealFile(ftpClient,new File(localFileRealPath));
System.out.println("i="+ i);
System.out.println("fileList.length="+fileList.length);
if(i+1 == fileList.length){
ftpClient.changeToParentDirectory();
for(int m=0;m<AbsolutePathList.size();m++){
for(int k=0;k<AbsolutePathList.size();k++){
System.out.println("localPath="+localPath+" AbsolutePathList.get(0)="+AbsolutePathList.get(0));
String target = AbsolutePathList.get(k).substring(localPath.length()-2, AbsolutePathList.get(k).length());
System.out.println("target="+target);
int sign = target.lastIndexOf(fileName.get(k));
System.out.println("fileName.get(0)="+fileName.get(0)+" sign="+sign);
String finalTarget = target.substring(0, sign-1);
finalTarget = finalTarget.replace("\\", "");
System.out.println("finalTarget="+finalTarget);
String ftpWorkingPath = ftpClient.printWorkingDirectory();
System.out.println("ftpWorkingPath1="+ftpWorkingPath);
if(ftpWorkingPath.length() > webSiteName.length()){
System.out.println("webSiteName====="+k+webSiteName);
int firstIndex = ftpWorkingPath.trim().toLowerCase().indexOf(webSiteName.trim().toLowerCase());
System.out.println("firstIndex="+firstIndex+" webSiteName="+webSiteName+" webSiteName.length="+webSiteName.length());
ftpWorkingPath = ftpWorkingPath.trim().toLowerCase().substring(firstIndex+webSiteName.length(), ftpWorkingPath.length());
}
ftpWorkingPath = ftpWorkingPath.replace("/", "");
System.out.println("ftpWorkingPath3="+ftpWorkingPath);
if(finalTarget.trim().toLowerCase().equals(ftpWorkingPath.trim().toLowerCase())){
System.out.println("jumping again ...");
ftpClient.changeToParentDirectory();
}
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
我利用上面的代码来上传到FTP服务器上,可以上传成功但是当我运行时就不能运行了,因为我上传的模板是可以放到Tomcat下面跑起来的,结果它总是报告错误。郁闷中.... 如何发现问题的原因的?这才是问题的重点为什么我能想到是传输过程中没有设置传输的格式类型呢? 那是因为我在FTP上打开我传输的JAR包结果发现总是报告文件损坏,所有就想到了问题的解决办法了。 后来我发现了问题所在:是代码的问题,缺少了以下代码:
this.connectServer();
this.setFileType(FTP.BINARY_FILE_TYPE);
/**
* 设置传输文件的类型[文本文件或者二进制文件]
*
* @param fileType
* --BINARY_FILE_TYPE、ASCII_FILE_TYPE
*/
public void setFileType(int fileType) {
try {
ftpClient.setFileType(fileType);
} catch (Exception e) {
e.printStackTrace();
}
}
前面的代码放在Linux下面运行,还是出现了一些小问题:然后开始寻找错误的原因,其中一点是:(1)为了和Windows、Linux下面都兼容,所以我加入了以下代码:
if(i+1 == fileList.length){
System.out.println("aa3-absolute="+upfile.getAbsolutePath());
if(upfile.getAbsolutePath().indexOf("/") == 0){
AbsolutePathList.add(upfile.getAbsolutePath());
}else{
String newStr = "";
StringBuffer sb = new StringBuffer(newStr);
newStr = sb.append("/").append(upfile.getAbsolutePath()).toString();
AbsolutePathList.add(newStr);
}
fileName.add(upfile.getName());
}
(1)添加上述代码的原因是:我在打印的结果中发现
localPath=/usr/local/jboss-4.2.2.GA/server/default/deploy/cms-ear.ear/cms.war/website/LinuxTest/
AbsolutePathList.get(k)=/usr/local/jboss-4.2.2.GA/server/default/deploy/cms-ear.ear/cms.war/website/LinuxTest/WEB-INF/classes/cn/ctit/cms/service/webapp
如果是windows的话:
AbsolutePathList.get(k)=usr/local/jboss-4.2.2.GA/server/default/deploy/cms-ear.ear/cms.war/website/LinuxTest/WEB-INF/classes/cn/ctit/cms/service/webapp
所以才加了上面的代码。
第二点发现的问题是:(2)在Linux下面还出现了错位。原因:我又加入了以下代码:
finalTarget = finalTarget.replace("\\", "");
finalTarget = finalTarget.replace("/", ""); //多加了这么一行,这也是和linux下面兼容
(2)加入上述代码原因:
finalTarget = finalTarget.replace("\\", "");
finalTarget = finalTarget.replace("/", "");
上面那个仅仅是替换掉Windows下面的斜杠,但是Linux下面没有替换,所以才加入了下面那个代码。
最后终于测试通过了,哈哈。所有的代码如下:下面的代码才是真正的适合Windows和Linux的FTP上传的代码。
//window and linux
public void uploadManyFile(FTPClient ftpClient,File localFilePath,String localPath,String webSiteName){
try{
File fileList[] = localFilePath.listFiles();
//exist null directory
if(fileList.length <= 0){
ftpClient.changeToParentDirectory();
for(int m=0;m<AbsolutePathList.size();m++){
for(int k=0;k<AbsolutePathList.size();k++){
String target = AbsolutePathList.get(k).substring(localPath.length()-1, AbsolutePathList.get(k).length());
int sign = target.lastIndexOf(fileName.get(k));
String finalTarget = target.substring(0, sign-1);
finalTarget = finalTarget.replace("\\", "");
String ftpWorkingPath = ftpClient.printWorkingDirectory();
if(ftpWorkingPath.length() > webSiteName.length()){
int firstIndex = ftpWorkingPath.trim().toLowerCase().indexOf(webSiteName.trim().toLowerCase());
ftpWorkingPath = ftpWorkingPath.substring(firstIndex+webSiteName.length(), ftpWorkingPath.length());
}
ftpWorkingPath = ftpWorkingPath.replace("/", "");
System.out.println("target"+k+"="+target+" sign="+sign+" finalTarget="+finalTarget);
System.out.println("ftpWorkingPath"+k+"="+ftpWorkingPath);
if(finalTarget.trim().toLowerCase().equals(ftpWorkingPath.trim().toLowerCase())){
System.out.println("jumping again");
ftpClient.changeToParentDirectory();
}
}
}
}
for(int i=0;i<fileList.length;i++){
File upfile = (File)fileList[i];
if(upfile.isDirectory()){
System.out.println(upfile.getName()+"---");
System.out.println("jinlai");
if(i+1 == fileList.length){
System.out.println("aa3-absolute="+upfile.getAbsolutePath());
if(upfile.getAbsolutePath().indexOf("/") == 0){
AbsolutePathList.add(upfile.getAbsolutePath());
}else{
String newStr = "";
StringBuffer sb = new StringBuffer(newStr);
newStr = sb.append("/").append(upfile.getAbsolutePath()).toString();
AbsolutePathList.add(newStr);
}
fileName.add(upfile.getName());
}
ftpClient.makeDirectory(upfile.getName());
ftpClient.changeWorkingDirectory(upfile.getName());
uploadManyFile(ftpClient,upfile,localPath,webSiteName);
}else{
System.out.println(localFilePath.getName()+"****&&***");
System.out.println(upfile.getName()+"--+++");
System.out.println(localFilePath.getPath()+"/"+upfile.getName());
String localFileRealPath = localFilePath.getPath()+"/"+upfile.getName();
uploadRealFile(ftpClient,new File(localFileRealPath));
System.out.println("i="+ i);
System.out.println("fileList.length="+fileList.length);
if(i+1 == fileList.length){
ftpClient.changeToParentDirectory();
for(int m=0;m<AbsolutePathList.size();m++){
for(int k=0;k<AbsolutePathList.size();k++){
System.out.println("localPath="+localPath+" AbsolutePathList.get(0)="+AbsolutePathList.get(0));
System.out.println("AbsolutePathList.get(k)="+AbsolutePathList.get(k)+" k="+k+" localPath.length()="+localPath.length());
String target = AbsolutePathList.get(k).substring(localPath.length()-1, AbsolutePathList.get(k).length());
System.out.println("target="+target);
int sign = target.lastIndexOf(fileName.get(k));
System.out.println("sign="+sign);
String finalTarget = target.substring(0, sign-1);
finalTarget = finalTarget.replace("\\", "");
finalTarget = finalTarget.replace("/", "");
System.out.println("finalTarget="+finalTarget);
String ftpWorkingPath = ftpClient.printWorkingDirectory();
System.out.println("ftpWorkingPath1="+ftpWorkingPath);
if(ftpWorkingPath.length() > webSiteName.length()){
System.out.println("webSiteName====="+webSiteName);
int firstIndex = ftpWorkingPath.trim().toLowerCase().indexOf(webSiteName.trim().toLowerCase());
System.out.println("firstIndex="+firstIndex+" webSiteName="+webSiteName+" webSiteName.length="+webSiteName.length());
ftpWorkingPath = ftpWorkingPath.trim().toLowerCase().substring(firstIndex+webSiteName.length(), ftpWorkingPath.length());
}
ftpWorkingPath = ftpWorkingPath.replace("/", "");
System.out.println("ftpWorkingPath3="+ftpWorkingPath);
if(finalTarget.trim().toLowerCase().equals(ftpWorkingPath.trim().toLowerCase())){
System.out.println("jumping again ...");
ftpClient.changeToParentDirectory();
}
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}