久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 91免费高清视频 | 99免费视频| 国产91 在线播放 | 免费观看的黄色网址 | 三级成人在线 | 国产成人免费视频 | 国产精品视频一区二区三区 | 一级黄色片在线免费观看 | 成人亚洲一区 | 国内精品视频一区二区三区 | 欧美a√| 久久夜视频 | 久久99精品久久久久久国产越南 | 欧美a级成人淫片免费看 | 日韩成人av在线播放 | 中文字幕一区在线观看视频 | 自拍视频网站 | 亚洲综合区 | 亚洲国产成人在线视频 | 一区二区高清 | 国产精品伦理一区 | 九九热在线免费视频 | 亚洲bt 欧美bt 日本bt | 精品一区二区三区四区外站 | 国产精品久久久久无码av | 一级做a爰片性色毛片16美国 | 亚洲毛片在线 | 中文字幕一区二区三区精彩视频 | av中文在线 | 日日网| 99久久国产综合精品麻豆 | 亚洲在线 | 久热中文字幕 | 国产女人与拘做受视频 | 欧美一区二区三区大片 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | av一区二区三区四区 | 瑟瑟免费视频 | 国产农村妇女精品一区 | www.久久影视 | 国产高清一区二区三区 |