Model view controller Grails生成一个zip文件供用户下载

Model view controller Grails生成一个zip文件供用户下载,model-view-controller,grails,controller,gorm,Model View Controller,Grails,Controller,Gorm,我是一名新手到grails目前正在从事一个与上传图片的用户一起工作的web应用程序项目。用户可以创建带有提示列表的“搜索”。每个“提示”都是参与者的目标(例如:上传你最喜欢的糖果的图片)。基本上,这个web应用程序是一个“拾荒者搜寻”工具,摄影师可以在社交场合分享他们的工作 现在,我在尝试在我的用户控制器中编写函数以生成包含用户上传的所有图片的zip文件时遇到问题。这就是我的控制器功能目前的样子。 我从这个例子开始。 def下载相册(){ ByteArrayOutputStream bas=新

我是一名新手grails目前正在从事一个与上传图片的用户一起工作的web应用程序项目。用户可以创建带有提示列表的“搜索”。每个“提示”都是参与者的目标(例如:上传你最喜欢的糖果的图片)。基本上,这个web应用程序是一个“拾荒者搜寻”工具,摄影师可以在社交场合分享他们的工作

现在,我在尝试在我的用户控制器中编写函数以生成包含用户上传的所有图片的zip文件时遇到问题。这就是我的控制器功能目前的样子。 我从这个例子开始。

def下载相册(){
ByteArrayOutputStream bas=新的ByteArrayOutputStream()
ZipOutputStream zipFile=新ZipOutputStream(BAS)
//用户域类的实例
def userInstance=User.findByLogin(auth.User())
//图片将上载到提示符并存储为photoInstance
//这行代码将实际文件存储为字节[]
photoInstance.myFile=image.getBytes()
//选择属于用户的所有照片并将其存储到列表中
def photoInstanceList=userInstance?Photo.findAllByMyUser(userInstance):[]
//映射
[userInstance:userInstance,photoInstanceList:photoInstanceList]
photoInstanceList.each{photo->
如果(photoInstance.myFile!=“”){
File File=新文件(photoInstance.myFile)
zipFile.putNextEntry(新Zippentry(Photo.title+“.jpeg”))
file.withInputStream{i->

zipFile如果上面的代码是按照这里的方式复制的,那么我们不应该使用
photo.title
而不是下一行中的
photo.title


zipFile.putNextEntry(新的ZipEntry(Photo.title+“.jpeg”)

stacktrace将对调试有很大帮助。当您使用此代码时会发生什么情况?没有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()   

}