java FTP客户端的Java代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6370554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java code for FTP client
提问by Lalchand
I have wrote a simple code to download a file from a FTP server using the following connection string:
我编写了一个简单的代码来使用以下连接字符串从 FTP 服务器下载文件:
URL url = new URL("ftp://test:[email protected]/sample.txt;type=i");
URLConnection urlc = url.openConnection();
But while I tried to read the content from the above connection and write on my local drive I got only 61KB. When I opened thesample.txt
it has the following content:
但是,当我尝试从上述连接读取内容并在本地驱动器上写入时,我只有 61KB。当我打开sample.txt
它时,它有以下内容:
06-16-2011 02:47PM 1228317425 sample.txt
But the original size of the sample.txt in FTP is 1.14GB.
但是FTP中sample.txt的原始大小是1.14GB。
urlc.getContentLength()
returned -1
for me.
urlc.getContentLength()
回来 -1
找我。
here is my entire code
这是我的全部代码
public void download( String ftpServer, String user, String password,
String fileName, File destination ) throws MalformedURLException,
IOException
{
if (ftpServer != null && fileName != null && destination != null)
{
StringBuffer sb = new StringBuffer( "ftp://" );
// check for authentication else assume its anonymous access.
if (user != null && password != null)
{
sb.append( user );
sb.append( ':' );
sb.append( password );
sb.append( '@' );
}
sb.append( ftpServer );
sb.append( '/' );
sb.append( fileName );
/*
* type ==> a=ASCII mode, i=image (binary) mode, d= file directory
* listing
*/
sb.append( ";type=i" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
System.out.println(sb);
try
{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();
System.out.println(urlc.getContentType());
System.out.println(urlc.toString());
System.out.println(urlc.getContentLength());
bis = new BufferedInputStream( urlc.getInputStream() );
bos = new BufferedOutputStream( new FileOutputStream("D://FTP/"+
destination.getName() ) );
int i;
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println( "Input not available" );
}
}
回答by planetjones
You'll need to open the connection and start streaming the result e.g.
您需要打开连接并开始流式传输结果,例如
URL url = new URL("ftp://test:[email protected]/sample.txt;type=i");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream(); // To download
etc. etc.
Have you done that? The basic JDK support is limited to streaming the data. You might want to look at a Java FTP client which allows you to interact more richly with the FTP Protocol. I don't have much experience in this area but Apache Commons Netis one such example. With this you would read files as org.apache.commons.net.ftp.FtpFile
Objects, which has a getter for the file size.
你这样做了吗?基本的 JDK 支持仅限于流式传输数据。您可能想查看一个 Java FTP 客户端,它允许您与 FTP 协议进行更丰富的交互。我在这方面没有太多经验,但Apache Commons Net就是一个这样的例子。有了这个,你可以将文件作为org.apache.commons.net.ftp.FtpFile
对象读取,它有一个文件大小的吸气剂。
BTW: -1 for the getContentLength()
call is just the default if it can't find a header named "content-length". So I think in your case, because this is FTP the method is not relevant. This adds further weight to try and use a dedicated FTP Library.
顺便说一句:getContentLength()
如果找不到名为“content-length”的标头,则调用的-1只是默认值。所以我认为在你的情况下,因为这是 FTP 方法是不相关的。这进一步增加了尝试使用专用 FTP 库的权重。
回答by sbrattla
No need to reinvent the wheel. I'd recommend using FTP4J which I have used in several projects for file upload/download. http://www.sauronsoftware.it/projects/ftp4j/. I assume the library should be able to take care of the issues for you.
无需重新发明轮子。我建议使用 FTP4J,我在几个项目中使用过它来上传/下载文件。http://www.sauronsoftware.it/projects/ftp4j/。我认为图书馆应该能够为您处理这些问题。
回答by cjstehno
You might want to use an existing library rather than rolling your own, try Apache Commons Net.
您可能想要使用现有的库而不是使用自己的库,请尝试Apache Commons Net。