Java 多部分 POST returns Web Api 服务器不支持 MediaType
Java Multipart POST returns MediaTypeNotSupported from WebApi server
我正在使用 'MultipartUtil' class,如 SO question 的第一个答案中所述。
我通过添加额外的行更改了构造函数,如下所示:
public MultiPartUtil(String requestURL, String charset) throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpsURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Cache-Control", "no-cache");
httpConn.setRequestProperty("enctype", "multipart/form-data");
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("User-Agent", "MTApp");
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
}
上传文件的代码是:
MultiPartUtil multipart = new MultiPartUtil(url, "UTF-8");
multipart.addFilePart("archive", new File("archivefile.zip"));
multipart.finish();
在服务器端,我有:
[HttpPost]
public void upload()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, "This request is not properly formatted"));
}
//Process file here...
}
在不允许接受文件的 Java 代码中我可能遗漏了什么?
能否尝试将边界更改为呈现 ---1234567--- 而不是 ===1234567===?
因为在 http header 参数中不允许使用令牌。
我正在使用 'MultipartUtil' class,如 SO question 的第一个答案中所述。
我通过添加额外的行更改了构造函数,如下所示:
public MultiPartUtil(String requestURL, String charset) throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpsURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Cache-Control", "no-cache");
httpConn.setRequestProperty("enctype", "multipart/form-data");
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("User-Agent", "MTApp");
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
}
上传文件的代码是:
MultiPartUtil multipart = new MultiPartUtil(url, "UTF-8");
multipart.addFilePart("archive", new File("archivefile.zip"));
multipart.finish();
在服务器端,我有:
[HttpPost]
public void upload()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, "This request is not properly formatted"));
}
//Process file here...
}
在不允许接受文件的 Java 代码中我可能遗漏了什么?
能否尝试将边界更改为呈现 ---1234567--- 而不是 ===1234567===?
因为在 http header 参数中不允许使用令牌。