我一直在使用HttpURLConnection類被上傳文件,但在執行我得到一個錯誤,如 「請求被拒絕,因爲沒有多邊界發現」無法使用HttpURLConnection的上傳文件
以下是我的代碼片段
File importFile = new File(args[0]);
url = new URL("http://localhost:8888/ajax/import?action=csv&session=" + sessionId + "&folder=36");
URLConnection uc = url.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Cookie", cookieStringBuffer.toString());
connection.setRequestProperty("content-type", "multipart/form-data");
connection.setDoOutput(true);
connection.connect();
FileInputStream is = new FileInputStream(importFile);
OutputStream os = connection.getOutputStream();
PrintWriter pw = new PrintWriter(os);
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = is.read(buffer)) != -1) {
//os.write(buffer, 0, bytes_read);
pw.print(buffer); // here we "send" our body!
}
pw.flush();
pw.close();
請幫我解決問題。我迫切需要弄清楚這一點。
非常感謝!
非常感謝您的回覆。我之前使用過OutputStream,但沒有結果。所以我試了一下。我無法確切地知道我要出錯的地方。 – shre 2011-05-02 12:14:35