我一直在使用rest-client-builder插件(http://grails.org/plugin/rest-client-builder)并遇到发送文件作为inputStream对象的问题。
从插件文档中:
Multipart requests are possible by setting properties of the request body to File, URL, byte[] or InputStream instances:
def resp = rest.post(url) { contentType "multipart/form-data" zip = new File(pluginPackage) pom = new File(pomFile) xml = new File(pluginXmlFile) }
我的代码:def post(String url, InputStream photo, String contentType, Cookie[] cookies = null) { def rest = new RestBuilder() def cookiesHeaderString = "" if (cookies) { cookiesHeaderString = WebUtils.buildCookiesHeader(cookies) } def resp = rest.post(url) { header "Cookie", cookiesHeaderString file = photo contentType "multipart/form-data" } return resp?.responseEntity?.body }
有人可以建议如何发送InputStream对象或我做错了什么吗?
最佳答案
对于文件类型,我们需要在RequestCustomizer上设置“file”属性。下面的代码为我工作。
File myFile = new File("myFile.txt")
def restResponse = rest.post(url) {
header headerName, headerValue
contentType "multipart/form-data"
setProperty "file", myFile
}
关于rest - 无法使用Grails Rest-Client-Builder发送InputStream对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18770705/