[英]URL Connection (FTP) in Java - Simple Question
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:password@domain.com/" + 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. 例如,当我使用pdf执行此操作,然后尝试打开使用该程序运行的pdf时,它表示尝试打开pdf时发生错误。 I'm guessing because I am writing a "\\n" to the file? 我猜是因为我正在向文件写入“ \\ n”? How do I copy the file without doing this? 如何不执行此操作就复制文件?
Do not use any of the Reader
or Writer
classes when you're trying to copy the byte-for-byte exact contents of a binary file. 当您尝试复制二进制文件的逐字节精确内容时,请勿使用任何Reader
或Writer
类。 Use these only for plain text! 仅将这些用于纯文本! Instead, use the InputStream
and OutputStream
classes; 而是使用InputStream
和OutputStream
类。 they do not interpret the data at all, while the Reader
and Writer
classes interpret the data as characters. 它们根本不解释数据,而Reader
和Writer
类将数据解释为字符。 For example 例如
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 URLConnection
usage is correct here, I don't know; 我不知道您的URLConnection
用法是否正确; using Apache Commons FTP (as suggested elsewhere) would be an excellent idea. 使用Apache Commons FTP(在其他地方建议)将是一个好主意。 Regardless, this would be the way to read the file. 无论如何,这将是读取文件的方式。
Use a BufferedInputStream
to read and BufferedOutputStream
to write. 使用BufferedInputStream
进行读取,并使用BufferedOutputStream
进行写入。 Take a look at this post: http://www.ajaxapp.com/2009/02/21/a-simple-java-ftp-connection-file-download-and-upload/ 看看这篇文章: 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();
FTP usually opens another connection for data transfer. FTP通常会打开另一个连接以进行数据传输。 So I am not convinced that this approach with URLConnection is going to work. 因此,我不相信URLConnection的这种方法会起作用。 I highly recommend that you use specialized ftp client. 我强烈建议您使用专门的ftp客户端。 Apache commons may have one. Apache公地可能有一个。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.