【发布时间】:2015-12-07 14:52:00
【问题描述】:
直升机
我尝试将一些数据上传到我的网络服务器。 whit curl 效果很好
curl -s -uedoweb-admin:admin -F"data=@/home/raul/test/t1/6376990.pdf;type=application/pdf" -XPUT api.localhost/resource/frl:6376979/data
这里是痕迹 http://pastebin.com/jJmungAy
这里是新方法。
URL myurl;
HttpURLConnection conn;
String port = "9000";
String user = "edoweb-admin";
String password = "admin";
String encoding = Base64.encodeBase64String((user + ":" + password).getBytes());
String boundary = "==" + System.currentTimeMillis() + "===";
String crlf = "\r\n";
String twoHyphens = "--";
String attachmentName = "data";
String attachmentFileName = "6376986.pdf";
DataOutputStream request;
try {
myurl = new URL("http://localhost:9000/resource/frl:6376984/data");
conn = (HttpURLConnection) myurl.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("Accept", "*/*");
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
request = new DataOutputStream(conn.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + attachmentName + "\";filename=\""
+ attachmentFileName + "\"" + crlf);
request.writeBytes("Content-Type: application/pdf");
request.writeBytes(crlf);
System.out.println(conn.getResponseCode());
} catch (Exception e) {
throw new RuntimeException(e);
}
我在我的代码和网络服务器上发现了一些问题。我写了一个新方法,现在服务器响应 400。
谁能帮帮我?
【问题讨论】:
-
改善诊断的两件事。 1. 用 try catch for Exception 或 THrowable 包围你的测试,看看是否有任何问题。 2. 你能在服务器日志上看到你用来测试请求是否真的在那里吗?
-
我用 try-catch 包围了多行,它都是绿色的。日志没有显示我的尝试。
-
请使用包含 try-catch 的代码编辑您的问题。你没有通过代码到达服务器的事实意味着你有一个 NPE 或其他东西
-
看,这个问题及其答案可能会对您有所帮助,看起来该库有一些奇怪的行为。 stackoverflow.com/questions/15678208/…
标签: java junit httprequest multipartform-data httpconnection