gpt4 book ai didi

Java Multipart POST 从 Web Api 服务器返回不支持的 MediaType

转载 作者:太空宇宙 更新时间:2023-11-03 15:30:26 25 4
gpt4 key购买 nike

我正在使用 SO question 的第一个答案中描述的“MultipartUtil”类.

我通过添加额外的行更改了构造函数,如下所示:

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 参数中不允许使用 token 。

关于Java Multipart POST 从 Web Api 服务器返回不支持的 MediaType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33936171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com