我应该使用哪个grails插件进行大文件上传?

时间:2013-07-01 14:15:53

标签: grails groovy grails-plugin

我应该使用grails插件上传大型视频文件,如20MB,30MB,100MB,我也希望进度条也在那里,我曾尝试使用超级文件上传插件,但我觉得我在做什么错了,它不适合我,如果我使用它,或者其他一些更容易使用的插件,我也看过Jquery插件,但因为我只需要一次添加一个视频,我认为超级文件上传插件对我有好处。

我试过了,但是收到错误,请在下面找到代码。

虽然对于超级文件上传插件的错误:

在我的GSP中我有:

    <sfu:generateConfiguration fileSize="300" form="bookForm"  buttonWidth="104" buttonHeight="30"/>

<form class="form-horizontal" id="bookForm" name="saveVideoFile" action="saveVideoFile" onsubmit="return sfuSubmitForm(this);">
        <div class="control-group">
            <label class="control-label" for="inputEmail">Select Category</label>
            <div class="controls">

            <label class="control-label" for="inputPassword">Upload file</label>
        <div class="control-group">
            <div class="controls">
                 Choose file: 
                 <sfu:fileUploadControl></sfu:fileUploadControl>
                    <br/>
                    Progress bar: <sfu:fileUploadProgressBar/>
                    <br/>
            </div>
        </div>
          <input type="submit" value="Save">
    </form>

然后在控制器saveVideoFile操作中:

def saveVideoFile(){
    String uploadFilename = params.uploadedFileId
    println("params=${params}")

    String fileUploaded

    if ( uploadFilename ) { // get the full path name of the file from the temp directory
        def file = superFileUploadService.getTempUploadFile(uploadFilename)

        fileUploaded = fileUploadService.uploadFile( file, "newFile.jpg", "assets/uploadFile/" );
        println("fileUploaded=${fileUploaded}")

    }else{ // file was not uploaded by flash. User might have javascript off
        def fileStream = request.getFile('sfuFile'); // handle normal file upload as per grails docs
        fileUploaded = fileUploadService.uploadFile( fileStream, "newFile.jpg", "assets/uploadFile/" );
        render "Nothing to upload"
    }


}

我在服务中有一个保存文件对象的函数:

 String uploadFile( File file, String name, String destinationDirectory ) {

        def serveletContext = ServletContextHolder.servletContext
        def storagePath = serveletContext.getRealPath( destinationDirectory )

        def storagePathDirectory = new File( storagePath )

        if( !storagePathDirectory.exists() ){
            println("creating directory ${storagePath}")
            if(storagePathDirectory.mkdirs()){
                println "SUCCESS"   
            }else{
                println "FAILED"
            }
        } 

        // Store file

        println("file In service=${file}")
        //def tempFile = file.getBytes()
        def tempFile = file
        if(!tempFile?.isEmpty()){
            tempFile.transferTo( new File("${storagePath}/${name}") )
            println("Saved File: ${storagePath}/${name}")
            return "${storagePath}/${name}" 
        }else{
            println "File: ${tempFile.inspect()} was empty"
            return null
        }
}

在保存时它会给出:

  

没有方法签名:java.io.File.isEmpty()适用于参数类型:()values:[]可能的解决方案:identity(groovy.lang.Closure),isFile(),list(),dump (),inspect()

在线:

if(!tempFile?.isEmpty()){

关于服务功能。

并且在打印tempFile变量时,我正在获得临时存储位置路径。

我知道我做错了什么,任何帮助都会有用。

1 个答案:

答案 0 :(得分:0)

发现我的脚本问题,我试图调用文件作为Multipartfile对象,超级文件上传器给出了磁盘上文件的临时位置,现在通过复制功能可以将其移动到指定目录:

如果我在服务中更改条件块:

if(tempFile?.isFile()){
                String sourceFilePath = tempFile
                String destinationFilePath = "${storagePath}/${name}"
                (new AntBuilder()).copy(file: sourceFilePath, tofile: destinationFilePath)

                println("Saved File: ${storagePath}/${name}")
                return "${storagePath}/${name}" 
            }else{
                println "File: ${tempFile.inspect()} was empty"
                return null
            }

然后它将被保存,我不得不将文件从临时位置复制到实际目录。 将需要一些更多的增强功能,但对于上面的代码,它将以这种方式工作。