0

我是grails的新手,目前正在开发一个与用户上传图片的网络应用程序项目。用户可以使用提示列表创建“搜索”。每个“提示”都是参与者的目标(例如:上传您最喜欢的糖果的图片。)基本上,网络应用程序是摄影师在社交上分享他们的作品的“寻宝”工具。

现在,我无法尝试在我的用户控制器中编写一个函数来生成一个包含用户上传的所有图片的 zip 文件。这就是我的控制器功能目前的样子。我用这个我发现的例子开始。 生成一个 zip 文件。

def downloadAlbum(){

    ByteArrayOutputStream baos = new ByteArrayOutputStream()
    ZipOutputStream zipFile = new ZipOutputStream(baos)

    //Instance of a user domain class
    def userInstance = User.findByLogin(auth.user())

    //pictures are uploaded to a prompt and stored as a photoInstance
    //this line of code gets the actual file stored as byte[]
        photoInstance.myFile = image.getBytes()

    //select all photos that belong to the user and store them into a list
    def photoInstanceList = userInstance ? Photo.findAllByMyUser(userInstance) : []


    //Mapping
    [userInstance: userInstance, photoInstanceList: photoInstanceList]      

      photoInstanceList.each {photo ->
        if (photoInstance.myFile != "") {
          File file = new File(photoInstance.myFile)
          zipFile.putNextEntry(new ZipEntry(Photo.title+".jpeg"))
          file.withInputStream { i ->

            zipFile << i

          }
          zipFile.closeEntry()
         }
        }
        zipFile.finish()
        response.setHeader("Content-disposition", "filename=\"${login}.zip\"")
        response.contentType = "application/zip"
        response.outputStream << baos.toByteArray()
        response.outputStream.flush()   

}

然后,我在用户视图中使用此代码生成一个链接,该链接调用用户控制器中的函数。我接近了吗?我的代码中是否缺少某些部分?

顺便说一句,这是我在 Stack Overflow 上写的第一个问题。感谢您花时间阅读这篇文章。如果有什么不够清楚,请在哪里可以改进这个问题。我正在使用 grails 2.1.1

感谢您的时间!!

4

1 回答 1

1

如果上面的代码已按原样复制在这里,我们不应该使用photo.title而不是Photo.title在下面的行中

zipFile.putNextEntry(new ZipEntry(Photo.title+".jpeg"))

于 2013-04-15T23:43:05.610 回答