我已经成功地在我的“本地构建”上创建了一个临时文件夹并在其中添加图像文件以供用户压缩和下载。不幸的是,在部署到我的测试服务器后,我无法创建这样的临时文件夹,因此我相信由于权限错误而无法压缩和流式传输它。基本上我是通过了。我无法访问在我的测试服务器上创建文件夹,并且需要将此文件夹和文件存储在我的 S3 存储桶上,然后从这里创建一个 zipOutputStream - 或者 - 如果可能的话,我认为这可能是一个更好的解决方案,是在我完成 zip 创建之前“即时”发送 zip 内容以响应。这可能吗?如果是这样,将如何去做呢?与将文件临时存储在 S3 上以进行压缩和流式传输相比,此方法是否有好处。
用于文件夹创建和压缩和流式传输的当前代码
def downloadZip(){
def fName = params.fName // ZipFile Name passed in 'example.zip'
def fLoc = params.fLoc //Folder Location passed in '/example'
def user = User.get( fLoc as Long ) //Get the Users files to be zipped
def urlList = []
List ownedIds
//Create a temporary directory to place files inside before zipping
new File(fLoc).mkdir()
//Dynamic Resource 'http://example.com/' -or- 'http://localhost:8080/'
def location = "${resource( dir:'/', absolute:true )}"
//Collect and Download QR-Codes image files
ownedIds = user.geolinks.collect {
//Define Url for Download
def urls = (location+"qrCode/download?u=http%3A%2F%2Fqr.ageoa.com%2F" +it.linkAddress+ "&s=150&n=" +it.linkAddress)
//Download each QR-Code
download2(urls,fLoc)
}
//ZIP the directory that was created and filled with the QR codes
String zipFileName = fName
String inputDir = fLoc
ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(zipFileName))
new File(inputDir).eachFile() { file ->
zipFile.putNextEntry(new ZipEntry(file.getName()))
def buffer = new byte[1024]
file.withInputStream { i ->
def l = i.read(buffer)
// check whether the file is empty
if (l > 0) {
zipFile.write(buffer, 0, l)
}
}
zipFile.closeEntry()
}
zipFile.close()
//Download QR-Code Zip-File
try {
def file = new File(fName)
response.setContentType("application/octet-stream")
response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
response.outputStream << file.newInputStream() // Performing a binary stream copy
}
catch(Exception e){
e.printStackTrace()
}
//Delete Temporary Folder
def dir2 = new File(fLoc)
dir2.deleteDir()
}
//Download All QR-Codes images to folder [userID]
def download2(address, dir){
def file = new FileOutputStream(dir+"/"+address.tokenize("&n=")[-1]+".png")
if(file){
def out = new BufferedOutputStream(file)
out << new URL(address).openStream()
out.close()
}
}