我对 swing 很陌生。 我需要使用 java swing 将文件上传到 Jboss 服务器。我怎样才能完成任务?
谢谢
最佳答案
正如 Max 已经提到的,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。
关于java - 我必须使用 java swing 将文件上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4516888/