我正在使用Grails Rest插件从服务器下载文件但是如何获取属于http标题字段“Content-disposition”的建议文件名?
我的代码:
withHttp(uri:uri){
def logo = new Logo()
def status = get(path:path, query:[identity: identity]){ resp, body ->
if( resp.status == 200){
logo.contentType = resp.contentType
// logo.fileName = How?
logo.bytes = body.getBytes()
}
return resp.status
}
if(status == 200){
return logo
}
else{
log.warn("Failed to fetch logo ")
return null;
}
}
我可以阅读内容类型,如何获取建议的文件名?
答案 0 :(得分:0)
好吧,我想出了一种做法。也许不是最好的解决方案,但它适用于最常见的场景。
编辑:我已更改此代码以使用现有方法来解析标头值的元素和属性。希望它能更好地处理不同的标题场景。我仍然不确定* filename参数是否被正确处理。
withHttp(uri:uri){
def logo = new Logo()
def status = get(path:path, query:[identity: identity]){ resp, body ->
if( resp.status == 200){
logo.contentType = resp.contentType
def contentDisposition = resp.getFirstHeader('Content-Disposition')
def element = contentDisposition?.elements.find{ it.name.equalsIgnoreCase('filename') || it.getParameterByName('filename')}
if(element)
logo.fileName = element.getParameterByName('filename')?.value ?: element.value
logo.bytes = body.getBytes()
}
return resp.status
}
if(status == 200){
return logo
}
else{
log.warn("Failed to fetch logo ")
return null;
}
}