def params = new MultipartEntity();
params.addPart("name", new StringBody(fileName))
params.addPart("file", new StringBody(fileContent))
post.setEntity(params)
def response = httpclient.execute(post)
此代码不起作用。
def fileContent = new FileBody(new File(fileName))
def params = new MultipartEntity();
params.addPart("name", new StringBody(fileName))
params.addPart("file", fileContent)
post.setEntity(params)
这段代码可以。
它中断是因为我从我要发布到的 Tomcat 服务器收到 400 Bad Request。我不知道为什么它这么说,它是一个不受我控制的应用程序。
我不想将临时文件写入硬盘。我现在不清楚如何避免这种情况。
最佳答案
我认为您可能希望使用 name 作为 body
来代替 file
:
params.addPart("body", new StringBody(fileContent));
关于java - 如何使用 StringBody 通过 HttpMime 上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13167774/