任何人都可以帮我如何从grails中的url下载图像.目前我使用以下代码,但它保存在应用程序的当前文件夹中.我想下载浏览器特定的文件夹(比如从web或saveAS下载一些文件的默认文件夹)
def imageDownload() {
//imageURL = "http://www.google.com/images/logo.png"
String fullPath = params.imageURL
String baseName = FilenameUtils.getBaseName(fullPath);
String extension = FilenameUtils.getExtension(fullPath);
def fileName = baseName+"."+extension
def fileDoc = new File(fullPath);
def webUtils = WebUtils.retrieveGrailsWebRequest()
def response = webUtils.getCurrentResponse()
response.setContentType("application/png")
response.setHeader "Content-disposition", "attachment; filename=\"${fileName}\"";
def file = new FileOutputStream(fullPath.tokenize("/")[-1])
def out = new BufferedOutputStream(file)
out << new URL(fullPath).openStream()
out.close()
redirect(action: "imageDetails", params:params)
}
需要帮助,谢谢.
最佳答案
def downloadImage = {
def fileURL = "http://www.google.com/images/logo.gif"
def thisUrl = new URL(fileURL);
def connection = thisUrl.openConnection();
def dataStream = connection.inputStream
response.setContentType("application/octet-stream")
response.setHeader('Content-disposition', 'Attachment; filename=logo.gif')
response.outputStream << dataStream
response.outputStream.flush()
}