Java 上传各种形式的数据
希洛 我试图将一些数据上传到我的Web服务器。 whit curl很好用Java 上传各种形式的数据,java,junit,httprequest,multipartform-data,httpconnection,Java,Junit,Httprequest,Multipartform Data,Httpconnection,希洛 我试图将一些数据上传到我的Web服务器。 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 这是痕迹 这里是新方法 URL myurl; HttpURLConnection conn; String port
curl -s -uedoweb-admin:admin -F"data=@/home/raul/test/t1/6376990.pdf;type=application/pdf" -XPUT api.localhost/resource/frl:6376979/data
这是痕迹
这里是新方法
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);
}
我在代码和Web服务器上发现了一些问题。我写了一个新的方法,现在是服务器响应400
有人能帮我吗?我自己发现的 代码如下:
public void form_test() {
try {
url = new URL("http://localhost:9000/resource/frl:6376984/data");
httpCon = (HttpURLConnection) url.openConnection();
String userpass = user + ":" + password;
basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
httpCon.setRequestProperty("Authorization", basicAuth);
String fieldName = "data";
File uploadFile = new File("/home/raul/test/frl%3A6376984/6376990.pdf");
String boundary = "" + System.currentTimeMillis() + "";
httpCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpCon.setRequestProperty("file", "6376986.pdf");
httpCon.setUseCaches(false);
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
httpCon.setRequestMethod("PUT");
OutputStream outputStream = null;
try {
outputStream = (httpCon.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter writer = null;
try {
writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String LINE_FEED = "\r\n";
String fileName = uploadFile.getName();
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"")
.append(LINE_FEED);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
System.out.println("Content-Transfer-Encoding: binary" + (LINE_FEED));
writer.append(LINE_FEED);
writer.flush();
fileToOutputStream(uploadFile, outputStream);
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
writer.append(LINE_FEED);
writer.flush();
writer.close();
httpCon.disconnect();
try {
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
第一个错误是边界字符串开头的字符“=”。似乎我的Web服务器不喜欢边界字段中有这么多的等号,所以我在这里只设置了一个:
httpCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
并从字符串边界中删除这些
String boundary = "" + System.currentTimeMillis() + "";
当我尝试上传文件时,我在Wireshark中也收到一些错误消息“数据包大小超出限制”,在Java中收到一些响应代码400,但现在使用这种方法,效果很好。
谢谢你们的支持和建议。我自己发现的 代码如下:
public void form_test() {
try {
url = new URL("http://localhost:9000/resource/frl:6376984/data");
httpCon = (HttpURLConnection) url.openConnection();
String userpass = user + ":" + password;
basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
httpCon.setRequestProperty("Authorization", basicAuth);
String fieldName = "data";
File uploadFile = new File("/home/raul/test/frl%3A6376984/6376990.pdf");
String boundary = "" + System.currentTimeMillis() + "";
httpCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpCon.setRequestProperty("file", "6376986.pdf");
httpCon.setUseCaches(false);
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
httpCon.setRequestMethod("PUT");
OutputStream outputStream = null;
try {
outputStream = (httpCon.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter writer = null;
try {
writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String LINE_FEED = "\r\n";
String fileName = uploadFile.getName();
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"")
.append(LINE_FEED);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
System.out.println("Content-Transfer-Encoding: binary" + (LINE_FEED));
writer.append(LINE_FEED);
writer.flush();
fileToOutputStream(uploadFile, outputStream);
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
writer.append(LINE_FEED);
writer.flush();
writer.close();
httpCon.disconnect();
try {
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
第一个错误是边界字符串开头的字符“=”。似乎我的Web服务器不喜欢边界字段中有这么多的等号,所以我在这里只设置了一个:
httpCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
并从字符串边界中删除这些
String boundary = "" + System.currentTimeMillis() + "";
当我尝试上传文件时,我在Wireshark中也收到一些错误消息“数据包大小超出限制”,在Java中收到一些响应代码400,但现在使用这种方法,效果很好。
感谢大家的支持和建议。有两件事需要改进。1.用try-catch-for-Exception或THrowable围绕您的测试,看看是否有任何错误。2.你能在服务器的日志上看到你用来测试请求是否真的到达了那里吗?我用try-catch和绿色环绕了多行。日志中没有显示有关我的尝试的任何信息。请使用包含尝试捕获的代码编辑您的问题。您没有使用代码访问服务器这一事实意味着您有一个NPE或其他东西。这个问题及其答案可能会帮助您,看起来该库有一些奇怪的行为。这是提高诊断的两个方面。1.用try-catch-for-Exception或THrowable围绕您的测试,看看是否有任何错误。2.你能在服务器的日志上看到你用来测试请求是否真的到达了那里吗?我用try-catch和绿色环绕了多行。日志中没有显示有关我的尝试的任何信息。请使用包含尝试捕获的代码编辑您的问题。您没有使用代码访问服务器这一事实意味着您有一个NPE或其他东西。这个问题及其答案可能会帮助您,看起来该库有一些奇怪的行为。这个也是