java ftp 下载 0k_Java上传文件到ftp文件为0k,求帮忙!

/**

日志

*/

private static final Log log = LogFactory.getLog(ftcClients.class);

//FTC连接地址

public static final String URL=ApplicationPropertiesHolder.getProperty("docInterface.ftp.url");

//FTC连接端口

public static final int PORT=Integer.parseInt(ApplicationPropertiesHolder.getProperty("docInterface.ftp.post"));

//FTC账号

public static final String USERNAME=ApplicationPropertiesHolder.getProperty("docInterface.ftp.userName");

//FTC密码

public static final String PASSWORD=ApplicationPropertiesHolder.getProperty("docInterface.ftp.passWord");

//图片存放文件夹

public static final String IMAGESRC=ApplicationPropertiesHolder.getProperty("docInterface.ftp.directoryString");

public static List SubmitPost(Map imges){

List patch=new ArrayList();

FTPClient ftp = new FTPClient();

try {

ftp.connect(URL, PORT); //连接

boolean isLogin = ftp.login(USERNAME, PASSWORD); //登陆

System.out.println("fcp登录:"+isLogin);

ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //设置传输文件类型

// boolean chang= ftp.changeWorkingDirectory(IMAGESRC);//设置传输文件存放文件夹

String pacthString=getpacth();

if(!mkdirs(ftp,pacthString)){//创建目录

return patch;

}

ftp.setControlEncoding("utf-8");

ftp.enterLocalPassiveMode();//设置为主动模式

File file_in=null;

InputStream is=null;

BufferedInputStream inStream = null;

for (Map.Entry imge : imges.entrySet()) {

file_in = new File(imge.getValue());

log.info(imge.getKey()+"文件存在于否:"+file_in.exists());

is = new FileInputStream(file_in);

inStream = new BufferedInputStream(is);

boolean result = ftp.storeFile(new String(imge.getKey().getBytes("utf-8"),"iso-8859-1"), inStream);

log.info(imge.getKey()+"文件传输:"+result+"传输地址:"+pacthString+"/"+imge.getKey());

if(!result){

is.close();

patch.add(null);

return patch;

}else{

patch.add(pacthString+"/"+imge.getKey());

}

}

is.close();

ftp.logout();

return patch;

} catch (Exception e) {

log.info(e.getMessage());

e.printStackTrace();

return patch;

}

} 这是我写的测试通过的 不知道是不是编码导致你上次不上去了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Apache Commons Net 库中的 FTPClient 类来实现 Java FTP 递归下载文件的功能。下面是一个示例代码,可以下载 FTP 服务器上指定目录下的所有文件及其子目录下的所有文件文件夹: ```java import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FtpDownloader { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "username"; String password = "password"; String remoteDirPath = "/path/to/remote/directory"; String localDirPath = "/path/to/local/directory"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); downloadDirectory(ftpClient, remoteDirPath, localDirPath); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } private static void downloadDirectory(FTPClient ftpClient, String remoteDirPath, String localDirPath) throws IOException { FTPFile[] subFiles = ftpClient.listFiles(remoteDirPath); if (subFiles != null && subFiles.length > 0) { for (FTPFile ftpFile : subFiles) { String remoteFilePath = remoteDirPath + "/" + ftpFile.getName(); String localFilePath = localDirPath + "/" + ftpFile.getName(); if (ftpFile.isDirectory()) { // create the directory in local file system File localDir = new File(localFilePath); if (!localDir.exists()) { localDir.mkdir(); } // download the sub directory recursively downloadDirectory(ftpClient, remoteFilePath, localFilePath); } else { // download the file OutputStream outputStream = new FileOutputStream(localFilePath); ftpClient.retrieveFile(remoteFilePath, outputStream); outputStream.close(); } } } } } ``` 在上面的代码中,`downloadDirectory()` 方法递归地下载指定目录下的所有文件文件夹。如果遇到子目录,它会在本地文件系统中创建一个对应的目录,并递归下载子目录下的所有文件文件夹。如果遇到普通文件,它会将其下载到本地文件系统中。 你需要将代码中的 `server`,`port`,`username`,`password`,`remoteDirPath` 和 `localDirPath` 替换为你自己的 FTP 服务器地址、端口、用户名、密码、远程目录和本地目录。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值