久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java 中的 URL 連接 (FTP) - 簡單問題

URL Connection (FTP) in Java - Simple Question(Java 中的 URL 連接 (FTP) - 簡單問題)
本文介紹了Java 中的 URL 連接 (FTP) - 簡單問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個簡單的問題.我正在嘗試用 Java 將文件上傳到我的 ftp 服務器.

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

我的計算機上有一個文件,我想復制該文件并上傳.我嘗試手動將文件的每個字節寫入輸出流,但這不適用于復雜的文件,例如 zip 文件或 pdf 文件.

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

例如,當我使用 pdf 執行此操作,然后嘗試打開使用此程序運行的 pdf 時,它說嘗試打開 pdf 時發生錯誤.我猜是因為我正在向文件寫入 "?如果不這樣做,如何復制文件?

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 " " to the file? How do I copy the file without doing this?

推薦答案

嘗試復制字節時不要使用任何 ReaderWriter 類-for-byte 二進制文件的確切內容.僅將這些用于純文本!相反,使用 InputStreamOutputStream 類;它們根本不解釋數據,而 ReaderWriter 類將數據解釋為字符.例如

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);
}

你的 URLConnection 用法在這里是否正確,我不知道;使用 Apache Commons FTP(如其他地方所建議的)將是一個好主意.無論如何,這將是讀取文件的方式.

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) - 簡單問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 免费日韩av| 久久久久久久一区 | 欧美精品一二三区 | 久久精品一二三影院 | 久久久久久久国产精品 | 国产永久免费 | 91一区二区 | 色婷婷久久久久swag精品 | 国产精品一区二区在线免费观看 | 免费看爱爱视频 | 国产精品一二三区 | 日中文字幕在线 | 亚洲精品在线播放 | 日本久久久影视 | 91在线精品秘密一区二区 | 成人精品一区二区 | chinese中国真实乱对白 | 欧美激情综合五月色丁香小说 | 精品亚洲一区二区三区四区五区 | 日韩av一区在线观看 | 国产精品爱久久久久久久 | 久久国产欧美日韩精品 | 最新黄色在线观看 | 91 在线 | 成人av免费网站 | 日本不卡一区二区三区在线观看 | 97精品国产97久久久久久免费 | 一区二区三区高清不卡 | 91在线观| 色综合久久久久 | 欧美 日韩 国产 成人 | 成人午夜精品 | 精品成人免费一区二区在线播放 | 国产精品一区二区免费看 | 精品久久久久久 | 亚洲国产精品一区二区久久 | 午夜免费福利电影 | 午夜性视频 | 欧美在线看片 | 怡红院成人在线视频 | 国产91精品久久久久久久网曝门 |