如何使用java将文件从url传输到ftp? [英] How to transfer a file from url to ftp using java?

查看:591
本文介绍了如何使用java将文件从url传输到ftp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个网址,我想将该网址中的文件转移到特定的ftp。
我有两个选择:
1)使用外部库(如apache common-io)我可以从url中获取文件,然后将其上传到ftp。
2)打开两个套接字(将url链接到ftp),然后将文件从url传输到ftp。

我是对的对这个?
最好的方法是什么?



我使用spring。



一些链接,我发现它:





这里是apache common-io库

编辑

我正在处理100-200M文件。

更新:
我选择首先下载文件并上传。
我选择这样做,因为我不期望下载文件时出现很多问题,但我期望将文件上传到FTP服务器时出现问题。所以,我认为错误处理会更好。
这个解决方案的缺点是我将临时文件存储在磁盘中。
如果遇到问题,我会稍后更新。

您需要先阅读URL引用的整个文件,然后才能将其发送到FTP服务器。如果文件非常大,则可能需要在从URL连接读取时将其写入临时文件,然后在发送到FTP服务器时从临时文件读取。使用这种方法,将URL的读取和向FTP服务器的写入保持分开,您可以单独处理错误。例如,如果URL连接引发异常,则可以相应地处理它,并可能重试请求。在尝试与FTP服务器进行任何通信之前,您可以确保拥有完整的文件。 如果您建立了输入和输出流,则可以执行此操作并准备好阅读和写信给他们。这意味着你已经完成了所有的协议,并且在FTP服务器上,这意味着告诉它你想上传一个文件,你想要上传它,以及文件名是什么。然后,您可以简单地从URL输入流中读取并将该块写入FTP的输出流。因为这是流式传输的,所以如果您与Web服务器之间的通信,或者您与FTP服务器之间的通信中断。您可能需要从头开始重新启动所有内容。

无论哪种方式都可以接受。 $ b

Given a url, I want to transfer the file in that url to a specific to ftp. I have two options: 1) Using external library (like apache common-io) I can fetch the file from the url and then upload it to the ftp. 2) Open two sockets (to the url an to the ftp) and simply transfer the file from the url to the ftp.

I'm I right about this? What's the best way to do that?

I'm using spring.

Here are some links that I found about it:

How to download and save a file from Internet using Java?.

Here is the apache common-io library

Edit:

I'm dealing with 100-200M files.

Update: I choose to first download the file and then upload it. I choose to do this since I don't expect many problems downloading the file, but I expect problems uploading the file to the FTP server. So, I think the the error handling will better that way. The lack of this solution is the fact that I'm storing the temporary file in the disk. I'll update later if I'll encounter some problems.

解决方案

  1. Would require you to read the entire file referenced by the URL before you can send it to the ftp server. If the file is really large, you may need to write it to a temporary file when you read from the URL connection, then read from the temporary file when you send to the FTP server. With this method, keeping the reading from the URL and the writing to the FTP server separate, you can handle the errors separately. For example, if the URL connection throws exceptions, you can handle that accordingly, and perhaps retry the request. You can ensure you have the complete file before any communication with the FTP server is attempted.

  2. You can do this if you've established the input and output streams and are ready to read from and write to them. This means you've already done all the protocol stull, and on the FTP server, that means telling it that you want to upload a file, where you want to upload it, and what the file's name is. Then you can simply read from the URL input stream and write that block to the FTP's output stream. Because this is streamed, if the communication between you and the webserver, or you and the FTP server gets interrupted. You'll probably have to restart everything from the beginning.

Either way is acceptable.

这篇关于如何使用java将文件从url传输到ftp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆