我试过了,但是收到错误,请在下面找到代码。
虽然对于超级文件上传插件的错误:
在我的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变量时,我正在获得临时存储位置路径。
我知道我做错了什么,任何帮助都会有用。
答案 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
}
然后它将被保存,我不得不将文件从临时位置复制到实际目录。 将需要一些更多的增强功能,但对于上面的代码,它将以这种方式工作。