gpt4 book ai didi

grails - 如何从 grails 中的 url 下载图像

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

任何人都可以帮助我如何从 grails 中的 url 下载图像。目前我正在使用以下代码,但它保存在应用程序的当前文件夹中。我想下载浏览器特定的文件夹(例如我们从网络或 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()
}

关于grails - 如何从 grails 中的 url 下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756532/

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