Python API请求的Java翻译未能正常工作。

huangapple go评论77阅读模式
英文:

Translation of python API request to Java not working

问题

以下是翻译好的内容:

我正在尝试使用Java复制一个非常简单的Python函数,该函数调用远程服务。我没有服务文档。以下是Python代码:

def sendImage(addr, image_path):
    image = open(image_path, 'rb').read()
    files = { 'image': ('Image.jpg', image, 'image/jpg') }
    response = requests.post(addr, files = files)
    print(response.text)

但我似乎无法使其工作。以下是我目前的Java代码:

try {

    URL url = new URL(request);
    HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "image/jpeg");
    connection.setConnectTimeout(Constants.TIMEOUT);
    connection.setReadTimeout(Constants.TIMEOUT);
    try (OutputStream out = connection.getOutputStream()) {
        IOUtils.copy(image, out);
    }
    try (InputStream in = connection.getInputStream()) {
        Map<String, String> originalResponse = mapper.readValue(in, TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, String.class));
    }

}
catch (Exception e) {
    e.printStackTrace();
}

我使用相同的addr和相同的图像调用了这两个函数,但当我运行Java代码时,应用程序会在以下几行代码中停顿几秒钟:

try (InputStream in = connection.getInputStream()) {

然后我得到一个"400: Bad Request"的响应。

为什么会出现问题,我该如何使其工作?提前谢谢。

英文:

I'm trying to replicate, using Java, a very simple Python function that calls a remote service. I don't have the service documentation. Here's the Python code:

def sendImage(addr, image_path):
    image = open(image_path, &#39;rb&#39;).read()
    files = { &#39;image&#39;: (&#39;Image.jpg&#39;, image, &#39;image/jpg&#39;) }
    response = requests.post(addr, files = files)
    print(response.text)

But I can't seem to get this to work. Here's my Java code so far:

try {

    URL url = new URL(request);
    HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod(&quot;POST&quot;);
    connection.setRequestProperty(&quot;Content-Type&quot;, &quot;image/jpeg&quot;);
    connection.setConnectTimeout(Constants.TIMEOUT);
    connection.setReadTimeout(Constants.TIMEOUT);
    try (OutputStream out = connection.getOutputStream()) {
        IOUtils.copy(image, out);
    }
    try (InputStream in = connection.getInputStream()) {
        Map&lt;String, String&gt; originalResponse = mapper.readValue(in, TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, String.class));
    }

}
catch (Exception e) {
    e.printStackTrace();
}

I'm calling both functions with the same addr and using the same image, but when I run my Java code, the app hangs in this line for a few seconds

try (InputStream in = connection.getInputStream()) {

and then I get a "400: Bad Request" response.

Why is this not working, and how can I make it work? Thanks in advance

答案1

得分: 1

response = requests.post(addr, files = files) 是使用多部分编码进行文件上传(请参阅api参考)。在你的Java代码中,你只需将文件内容写入请求的主体。

如果你想编写代码来正确处理多部分请求,请查看这个RFC,了解请求主体中必须包含的内容。

我建议你使用一些HTTP客户端。这里是使用okhttp上传的示例

英文:

response = requests.post(addr, files = files) is file upload with multipart encoding (see api reference) and in your java code you just write file content as body of request.

If you want to write code to properly handle multipart request, then check out this RFC, what you must include in request body.

My suggestion would be to use some http client. Here is example of upload with okhttp.

huangapple
  • 本文由 发表于 2020年9月30日 18:31:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64135688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定