1

我已经成功地在我的“本地构建”上创建了一个临时文件夹并在其中添加图像文件以供用户压缩和下载。不幸的是,在部署到我的测试服务器后,我无法创建这样的临时文件夹,因此我相信由于权限错误而无法压缩和流式传输它。基本上我是通过了。我无法访问在我的测试服务器上创建文件夹,并且需要将此文件夹和文件存储在我的 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()
    }
}
4

1 回答 1

2

是的,应该这样做,如果其中任何一个没有意义,请告诉我......

// A list of things to download and add to the zip
List<URL> testList = [ 'http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6',
                       'https://www.google.co.uk/images/srpr/logo4w.png' ]*.toURL()
response.setHeader( "Content-disposition", "attachment; filename=resources.zip" )
response.contentType = "application/octet-stream"

// Wrap the response stream in a zipoutputstream
new ZipOutputStream( response.outputStream ).withStream { zos ->

    // Always add a root folder to zip files, not to do so is spiteful
    zos.putNextEntry( new ZipEntry( "resources/" ) )

    // For each URL
    testList.each { res ->

        // Get a name for this file in the zip 

        // This bit might be the tricky bit as I guess you don't know the file
        // names. So instead of this you might need to check the response
        // object from opening a connection to the URL.  However, without a
        // working example URL from you, I can't be sure :-(
        String name = res.path.split( '/' )[ -1 ]

        // Create a zip entry for it
        zos.putNextEntry( new ZipEntry( "resources/$name" ) )

        // Write the resource stream into our zip
        res.withInputStream { ins ->
            zos << ins
        }

        // Close this resource
        zos.closeEntry() 
    }
    // Close the root folder
    zos.closeEntry()
}
于 2013-08-29T08:26:25.833 回答