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

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

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

問題描述

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

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

我的計(jì)算機(jī)上有一個(gè)文件,我想復(fù)制該文件并上傳.我嘗試手動將文件的每個(gè)字節(jié)寫入輸出流,但這不適用于復(fù)雜的文件,例如 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();

例如,當(dāng)我使用 pdf 執(zhí)行此操作,然后嘗試打開使用此程序運(yùn)行的 pdf 時(shí),它說嘗試打開 pdf 時(shí)發(fā)生錯(cuò)誤.我猜是因?yàn)槲艺谙蛭募懭?"?如果不這樣做,如何復(fù)制文件?

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?

推薦答案

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

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(如其他地方所建議的)將是一個(gè)好主意.無論如何,這將是讀取文件的方式.

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.

這篇關(guān)于Java 中的 URL 連接 (FTP) - 簡單問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
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 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美一级视频 | 亚洲欧美国产精品 | 超碰人人人人 | 精品国产三级 | 91精品看片 | av一区二区三区四区 | 欧美在线一区二区 | 韩日av| 午夜国产在线观看 | 久久手机视频 | 黄频在线观看 | 午夜视频在线免费观看 | 国产视频一区二区在线观看 | 国产一级黄色大片 | 日韩精品一区二区在线 | 日韩在线观看中文字幕 | 视频一区二区在线 | 免费在线观看黄色片 | 三级网站在线 | 日本不卡在线 | 日本三级中文字幕 | 欧美日皮视频 | 在线观看91| 一区二区三区在线看 | 四虎免费在线观看 | 欧美mv日韩mv国产 | 色接久久 | 久久艹av| 午夜影院黄 | 欧美爱爱网 | 天天干天天谢 | 久草福利资源 | 成人在线a| 国产一区亚洲 | 欧美色图在线观看 | 一级黄色小视频 | 国产资源在线播放 | 成人免费毛片嘿嘿连载视频 | 三级av片| 久久超| 国产欧美视频在线观看 |