如何使用java将文件从url传输到ftp? [英] How to transfer a file from url to ftp using java?
问题描述
我有两个选择:
1)使用外部库(如apache common-io)我可以从url中获取文件,然后将其上传到ftp。
2)打开两个套接字(将url链接到ftp),然后将文件从url传输到ftp。
我是对的对这个?
最好的方法是什么?
我使用spring。
一些链接,我发现它:
。 这里是apache common-io库 编辑: 我正在处理100-200M文件。 更新: 无论哪种方式都可以接受。 $ 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. 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. 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屋!
我选择首先下载文件并上传。
我选择这样做,因为我不期望下载文件时出现很多问题,但我期望将文件上传到FTP服务器时出现问题。所以,我认为错误处理会更好。
这个解决方案的缺点是我将临时文件存储在磁盘中。
如果遇到问题,我会稍后更新。