0
A
回答
1
正如馬克斯已經提到Swing是一個UI庫。 你必須創建HTTP POST和寫入文件到輸出流,即做這樣的事情:
URL url = new URL("http://host/filehandler");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
InputStream in = new FileInputStream(filePath);
OutputStream out = con.getOutputStream();
byte[] buffer = new byte[4096];
while (-1 != (n = in.read(in))) {
out.write(buffer, 0, n);
}
顯然http://host/filehandler
應該被映射到的東西是準備接收這篇文章,對付它。例如實現doPost()
的servlet並將該流保存爲文件。
0
使用過JFileChooser並選擇了要上傳的文件後,您必須連接到服務器。您的服務器必須運行一個ftp服務器。你必須有一個帳戶和密碼。從apache獲取commons-net-2.2.jar
,以便能夠創建FTPClient。
在這裏,你會發現更多的有關FTPClient:
http://commons.apache.org/net/apidocs/org/apache/commons/net/ftp/FTPClient.html
你的代碼必須是這樣的:
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("192.168.1.123");
client.login("myaccount", "myPasswd");
int reply = client.getReplyCode();
if (!client.isConnected()) {
System.out.println("FTP server refused connection." + reply);
client.disconnect();
System.exit(1);
} else {
System.out.println("FTP server connected." + reply);
}
// Create an InputStream for the file to be uploaded
String filename = "sp2t.c";
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(filename, fis);
client.logout();
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
相關問題
- 1. 使用java將文件上傳到服務器
- 2. 使用java將文件上傳到服務器
- 3. 將文件上傳到SFTP服務器
- 4. 使用Java Swing在服務器端使用php加上文件
- 5. 使用Android服務將xml文件上傳到服務器
- 6. 服務器上的文件的java路徑必須加倍backslah
- 7. 使用WinSCP將我的database.sql文件上傳到Linux服務器
- 8. 將文件上傳到SSRS服務器
- 9. 將文件上傳到Liferay服務器
- 10. 將文件上傳到服務器
- 11. 將文件上傳到服務器
- 12. 將大文件上傳到服務器
- 13. 將文件上傳到Web服務器
- 14. ASP.Net文件上傳到FTP服務器
- 15. 我無法使用uploadify將文件上傳到我的服務器上
- 16. 從applet上傳文件到服務器
- 17. 使用Java類上傳文件到服務器
- 18. 如何將文件和文件夾上傳到jenkins服務器
- 19. 將文件上傳到託管服務器
- 20. 使用PHP將文件從遠程服務器上傳到FTP服務器
- 21. 自動將文本文件上傳到MS SQL服務器
- 22. 上傳文件:用戶到服務器1到服務器2
- 23. 如何將Java JNLP文件上傳到服務器?
- 24. 的Java:將文件上傳到FTP服務器
- 25. 將文件從Java程序上傳到服務器
- 26. Flex - 您可以使用FileReference.upload()函數將文件上傳到Sharepoint,還是必須使用SP Web服務?
- 27. Android使用服務進度條上傳文件到服務器
- 28. Xcode - 如何將文本文件(.txt)上傳到FTP服務器
- 29. 上傳文件(excel)到服務器
- 30. 如何使用servlet將文件上傳到服務器?
搖擺無關文件上傳,你需要一些Java HTTP客戶端,例如:http://hc.apache.org/httpclient-3.x/features.html – bezmax 2010-12-23 08:33:35