Java RestTemplate上传图片文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21102071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:35:49  来源:igfitidea点击:

RestTemplate upload image file

javaspringresttemplate

提问by Ilkar

I need to create RestTemplate request, which will send image to upload by PHP application.

我需要创建 RestTemplate 请求,它将发送图像以通过 PHP 应用程序上传。

My code is:

我的代码是:

Resource resource = new FileSystemResource("/Users/user/Documents/MG_0599.jpg");
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
    parts.add("Content-Type", "image/jpeg");
    parts.add("file", resource);
    parts.add("id_product", productId);

ResponseEntity<String> response = cookieRestTemplate.getForEntity(url, String.class, parts);

After start this app, PHP server send me information, that file is empty.

启动此应用程序后,PHP 服务器向我发送信息,该文件为空。

I was thinking, that problem is by PHP site, but i've installed POSTER plugin for Firefox, and i made GET request onto this same URL, but the file to upload, i've selected normaly like in web form (popup system window to select file). After this PHP program uploaded file without any problem.

我在想,这个问题是由 PHP 站点引起的,但是我已经为 Firefox 安装了 POSTER 插件,并且我在同一个 URL 上发出了 GET 请求,但是要上传的文件,我已经选择了像 web 表单一样的正常(弹出系统窗口)选择文件)。这个PHP程序上传文件后没有任何问题。

I think, that problem is maybe with this, that i'm sending resource as value of param name:

我认为,这个问题可能与此有关,我将资源作为参数名称的值发送:

parts.add("file", resource);

And on the POSTER plugin, i just select file from my file system ?

在 POSTER 插件上,我只是从我的文件系统中选择文件?

Can you help me ?

你能帮助我吗 ?

回答by Sotirios Delimanolis

You aren't using the RestTemplatecorrectly. You are using the following method

你没有RestTemplate正确使用。您正在使用以下方法

public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables)

So you see your MultiValueMapis going to be used as a source for URL variables which you don't actually seem to have. There are no request parameters sent in the request.

因此,您将看到您MultiValueMap将用作 URL 变量的来源,而您实际上似乎没有这些变量。请求中没有发送请求参数。

You won't actually be able to upload a file with any of the getForXmethods.

您实际上无法使用任何getForX方法上传文件。

You will have to use one of the exchangemethods. For example

您将不得不使用其中一种exchange方法。例如

Resource resource = new FileSystemResource(
            "/Users/user/Documents/MG_0599.jpg");
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("Content-Type", "image/jpeg");
parts.add("file", resource);
parts.add("id_product", productId);

restTemplate.exchange(url, HttpMethod.GET,
            new HttpEntity<MultiValueMap<String, Object>>(parts),
            String.class); // make sure to use the generic type argument

Note, that it is very uncommon to do a file upload with a GETrequest.

请注意,使用GET请求进行文件上传是非常罕见的。