gpt4 book ai didi

java - 所需的 MultipartFile 参数 'files' 不存在 - MultipartyEntityBuilder

转载 作者:行者123 更新时间:2023-12-02 00:04:46 26 4
gpt4 key购买 nike

我正在尝试使用 httpclient(Post) 将多方实体发送到在服务器中运行的 spring 模块。所以它是一个安静的网络服务。但我的代码抛出错误。你能帮我一下吗?

我已经尝试过这个: Spring : File Upload RESTFUL Web Service

HTTP 客户端文件:(发布请求)

public class MultiFile {
public static void main(String args[]) throws ClientProtocolException, IOException
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:8080/RESTapi/student/addimage");
File file = new File("/Users/prabhu-pt3030/Desktop/eclipse-workspace-new/testing/target/classes/tes/javaFile123.txt");
MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();
FileBody filebody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
entitybuilder.addPart("files", filebody);
HttpEntity mutiPartHttpEntity = entitybuilder.build();
httppost.setEntity(mutiPartHttpEntity);
HttpResponse httpresponse = httpclient.execute(httppost);
}
}

Spring Controller :

@RequestMapping(value="/student/addimage",method=RequestMethod.POST,headers = "content-type=multipart/form-data")
public void Addingimage(@RequestParam(value="files") MultipartFile files)
{
//System.out.println(files.isEmpty());
}

输出:

所需的 MultipartFile 参数“files”不存在

最佳答案

我认为 ContentType 有问题。

您的 Controller 找不到任何名为“files”的参数

试试这个

public class MultiFile {
public static void main(String args[]) throws ClientProtocolException, IOException
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:8080/RESTapi/student/addimage");
File file = new File("/Users/prabhu-pt3030/Desktop/eclipse-workspace-new/testing/target/classes/tes/javaFile123.txt");
MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();
FileBody filebody = new FileBody(file, ContentType.DEFAULT_BINARY);
entitybuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entitybuilder.addPart("files", filebody);
HttpEntity mutiPartHttpEntity = entitybuilder.build();
httppost.setEntity(mutiPartHttpEntity);
HttpResponse httpresponse = httpclient.execute(httppost);
}
}

请参阅此处 more detail

关于java - 所需的 MultipartFile 参数 'files' 不存在 - MultipartyEntityBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58162393/

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