java – 使用FTPS将文件从android传输到服务器

前端之家收集整理的这篇文章主要介绍了java – 使用FTPS将文件从android传输到服务器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的 Android应用程序中使用 Apache Commons FTP library

我通过FTPS建立连接,虽然它与服务器完美连接,但在传输文件时遇到问题.

出于安全原因,订购应用程序的客户端在使用PROT P时请求在数据连接上恢复TLS会话.

因此,我在服务器上启用了此选项:

正如我所说,我可以连接到服务器,但不能传输文件.
如果我取消激活“使用PROT P时数据连接上所需的TLS会话恢复”框,则传输正常.

我正在寻找一种使用库进行文件传输的方法,但是没有成功,但我知道必须有办法.

我给你相关代码的一部分:

  1. TransferImagenesFTP.ftpClient = new FTPSClient();
  2.  
  3. TransferImagenesFTP.ftpClient.connect(InetAddress.getByName("XXX_XXX_XX_X"),26);
  4. TransferImagenesFTP.ftpClient.enterLocalPassiveMode();
  5. TransferImagenesFTP.ftpClient.setBufferSize(1024000);
  6. TransferImagenesFTP.ftpClient.login("xxxxxx","zzzzzz");
  7. TransferImagenesFTP.ftpClient.execPROT("P");
  8. TransferImagenesFTP.ftpClient.type(FTP.BINARY_FILE_TYPE);

我感谢任何帮助,谢谢.

解决方法

您可以尝试以下代码,我希望它也适用于您的情况.

代码使用Apache Commons vsf2通过安全ftp连接(SFTP)上传文件

  1. try {
  2. String filepath = "<FILE PATH>";
  3. String serverAddress = "<FTP SERVER ADDRESS>";
  4. String userId = "<FTP USER ID>";
  5. String password = "<FTP PASSWORD>";
  6. String remoteDirectory = "<FTP DIRECTORY TO UPLOAD TO>";
  7. String keyPath = "<PATH TO YOUR KEY>";
  8. String passPhrase = "<PASSWORD FOR YOUR KEY>";
  9.  
  10.  
  11. File file = new File(filepath);
  12. if (!file.exists())
  13. throw new RuntimeException("Error. File not found");
  14.  
  15. //Initializes the file manager
  16. StandardFileSystemManager manager = new StandardFileSystemManager();
  17. manager.init();
  18.  
  19. //Setup our SFTP configuration
  20. FileSystemOptions opts = new FileSystemOptions();
  21. SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts,"no");
  22. SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts,true);
  23. SftpFileSystemConfigBuilder.getInstance().setTimeout(opts,10000);
  24.  
  25. // Create local file object
  26. FileObject localFile = manager.resolveFile(file.getAbsolutePath());
  27.  
  28. // Create remote file object
  29. FileObject remoteFile = manager.resolveFile(createConnectionString(serverAddress,userId,password,keyPath,passPhrase,fileToFTP),createDefaultOptions(keyPath,passPhrase));
  30.  
  31.  
  32. // Copy local file to sftp server
  33. remoteFile.copyFrom(localFile,Selectors.SELECT_SELF);
  34. System.out.println("File upload successful");
  35.  
  36. }
  37. catch (Exception ex) {
  38. ex.printStackTrace();
  39. return false;
  40. }
  41. finally {
  42. manager.close();
  43. }

您可以在Apache Commons VFS Documentation查看更多信息

编辑

在理解了FTPS背后的逻辑和@ riyaz-ali的帖子并将您的评论中的链接引用到this article之后

Apache FTP客户端存在问题,它不支持TLS会话恢复.您可以修补Apache Commons Library的现有实现.

您可以尝试以下代码步骤来使其工作:

>在项目中添加以下修补的类. (此类扩展了Apache commons中给出的补丁中现有的FTPS实现)

  1. import java.io.IOException;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import java.net.Socket;
  5. import java.util.Locale;
  6.  
  7. import javax.net.ssl.SSLSession;
  8. import javax.net.ssl.SSLSessionContext;
  9. import javax.net.ssl.SSLSocket;
  10.  
  11. import org.apache.commons.net.ftp.FTPSClient;
  12.  
  13. import com.google.common.base.Throwables;
  14.  
  15. public class PatchedFTPSClient extends FTPSClient {
  16.  
  17. @Override
  18. protected void _prepareDataSocket_(final Socket socket) throws IOException {
  19. if(socket instanceof SSLSocket) {
  20. final SSLSession session = ((SSLSocket) _socket_).getSession();
  21. final SSLSessionContext context = session.getSessionContext();
  22. try {
  23. final Field sessionHostPortCache = context.getClass().getDeclaredField("sessionHostPortCache");
  24. sessionHostPortCache.setAccessible(true);
  25. final Object cache = sessionHostPortCache.get(context);
  26. final Method method = cache.getClass().getDeclaredMethod("put",Object.class,Object.class);
  27. method.setAccessible(true);
  28. final String key = String.format("%s:%s",socket.getInetAddress().getHostName(),String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT);
  29. method.invoke(cache,key,session);
  30. } catch(Exception e) {
  31. throw Throwables.propagate(e);
  32. }
  33. }
  34. }
  35.  
  36. }

>使用此修改后的代码段.

  1. TransferImagenesFTP.ftpClient = new PatchedFTPSClient();
  2.  
  3. TransferImagenesFTP.ftpClient.connect(InetAddress.getByName<SERVER-ADDRESS>"),26);
  4. TransferImagenesFTP.ftpClient.login("<USERNAME>","<PASSWORD>");
  5. TransferImagenesFTP.ftpClient.execPBSZ(0);
  6. TransferImagenesFTP.ftpClient.execPROT("P");
  7. TransferImagenesFTP.ftpClient.enterLocalPassiveMode();
  8.  
  9. //Now use the FTP client to upload the file as usual.

希望这对您有用,并将解决您的问题.

猜你在找的Android相关文章