Java中的URL连接(FTP) - 简单问题 [英] URL Connection (FTP) in Java - Simple Question

查看:841
本文介绍了Java中的URL连接(FTP) - 简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在我的电脑上有一个文件,我想复制该文件并上传它。我尝试手动将文件的每个字节写入输出流,但对于复杂文件(如zip文件或pdf文件)无效。

 文件文件=我电脑上的某个文件; 
String name = file.getName();
URL url =新的URL(ftp:// user:password@domain.com/+ name +; type = i);
URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream();

//然后我该怎么办?

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

  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(\\\
.getBytes());
line = br.readLine();
}
os.close();

例如,当我用pdf执行此操作时,然后尝试打开我运行的PDF这个程序,它说,试图打开PDF时发生错误。我猜是因为我正在写一个\\\
到文件中?如何在不执行此操作的情况下复制文件? code>或 Writer 类,当你试图复制一个二进制文件的byte-to-byte精确内容时。只能用于纯文本!相反,使用 InputStream OutputStream 类;它们根本不解释数据,而 Reader Writer 类将数据解释为字符。例如

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


$ / code>

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


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

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.

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. I'm guessing because I am writing a "\n" to the file? 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. Use these only for plain text! Instead, use the InputStream and OutputStream classes; they do not interpret the data at all, while the Reader and Writer classes interpret the data as characters. 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; using Apache Commons FTP (as suggested elsewhere) would be an excellent idea. Regardless, this would be the way to read the file.

这篇关于Java中的URL连接(FTP) - 简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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