Java 中的 URL 连接 (FTP) - 简单问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5925438/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:28:33  来源:igfitidea点击:

URL Connection (FTP) in Java - Simple Question

javaurlftp

提问by CodeGuy

I have a simple question. I'm trying to upload a file to my ftp server in Java.

我有一个简单的问题。我正在尝试使用 Java 将文件上传到我的 ftp 服务器。

I have a file on my computer, and I want to make a copy of that file and upload it. I tried manually writing each byte of the file to the output stream, but that doesn't work for complicated files, like zip files or pdf files.

我的计算机上有一个文件,我想制作该文件的副本并上传。我尝试将文件的每个字节手动写入输出流,但这不适用于复杂的文件,例如 zip 文件或 pdf 文件。

File file = some file on my computer;
String name = file.getName();
URL url = new URL("ftp://user:[email protected]/" + name +";type=i");
URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream();

//then what do I do?

Just for kicks, here is what I tried to do:

只是为了踢,这是我试图做的:

OutputStream os = urlc.getOutputStream();
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
while(line != null && (!line.equals(""))) {
    os.write(line.getBytes());
    os.write("\n".getBytes());
    line = br.readLine();
}
os.close();

For example, when I do this with a pdf and then try and open the pdf that I run with this program, it says an error occurred when trying to open the pdf. I'm guessing because I am writing a "\n" to the file? How do I copy the file without doing this?

例如,当我使用 pdf 执行此操作然后尝试打开我使用此程序运行的 pdf 时,它说尝试打开 pdf 时发生错误。我猜是因为我正在向文件中写入“\n”?如何在不执行此操作的情况下复制文件?

回答by Ernest Friedman-Hill

Do not use any of the Readeror Writerclasses when you're trying to copy the byte-for-byte exact contents of a binary file. Use these only for plain text! Instead, use the InputStreamand OutputStreamclasses; they do not interpret the data at all, while the Readerand Writerclasses interpret the data as characters. For example

当您尝试复制二进制文件的逐字节精确内容时,请勿使用任何ReaderWriter类。仅将这些用于纯文本!相反,使用InputStreamOutputStream类;它们根本不解释数据,而ReaderWriter类将数据解释为字符。例如

OutputStream os = urlc.getOutputStream();
FileInputStreamReader fis = new FileInputStream(file);
byte[] buffer = new byte[1000];
int count = 0;
while((count = fis.read(buffer)) > 0) {
    os.write(buffer, 0, count);
}

Whether your URLConnectionusage is correct here, I don't know; using Apache Commons FTP (as suggested elsewhere) would be an excellent idea. Regardless, this would be the way to read the file.

URLConnection这里的用法是否正确,我不知道;使用 Apache Commons FTP(如其他地方所建议的)将是一个好主意。无论如何,这将是读取文件的方式。

回答by Aleadam

Use a BufferedInputStreamto read and BufferedOutputStreamto write. Take a look at this post: http://www.ajaxapp.com/2009/02/21/a-simple-java-ftp-connection-file-download-and-upload/

使用 aBufferedInputStream来读取和BufferedOutputStream写入。看看这篇文章:http: //www.ajaxapp.com/2009/02/21/a-simple-java-ftp-connection-file-download-and-upload/

InputStream is = new FileInputStream(localfilename);
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os =m_client.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] buffer = new byte[1024];
int readCount;
while( (readCount = bis.read(buffer)) > 0) {
    bos.write(buffer, 0, readCount);
}
bos.close();

回答by Alex Gitelman

FTP usually opens another connection for data transfer. So I am not convinced that this approach with URLConnection is going to work. I highly recommend that you use specialized ftp client. Apache commons may have one.

FTP 通常会打开另一个连接进行数据传输。所以我不相信这种使用 URLConnection 的方法会奏效。我强烈建议您使用专门的 ftp 客户端。Apache Commons 可能有一个。

Check this out http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html

看看这个 http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html