将CommonsMultipartFile转换为文件

时间:2011-12-30 06:37:19

标签: grails grails-controller

我使用的插件将文件上传为CommonsMultipartFile。上传工作正常,但我试图使用另一个插件来读取文件标题(mp3标题),但它不会采用CommonsMultipartFile,只需要常规文件。有没有办法将CommonsMultipartFile转换为文件或进行其他一些解决方法。我已经尝试从上传的位置复制文件,但它似乎不起作用。这是我到目前为止所做的:

if (request instanceof MultipartHttpServletRequest) {

    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
        CommonsMultipartFile file =  (CommonsMultipartFile)multiRequest.getFile("files");   

            moveFile(file)
   }

   private moveFile(CommonsMultipartFile file){
    def userId = getUserId()
    def userGuid = SecUser.get(userId.id).uid
    def webRootDir = servletContext.getRealPath("/")


    def userDir = new File(webRootDir, "/myUsers/${userGuid}/music")
    userDir.mkdirs()
    file.transferTo( new File( userDir,file.originalFilename))      
    def myFile = new File( "/myUsers/${userGuid}/music/" +     file.originalFilename)


    AudioFile audioFile = AudioFileIO.read(file);

             //AudioFile is expecting a file, not a CommonsMultipartFile 

}

但是当我这样做时,我收到了这个错误:

groovy.lang.MissingMethodException:没有方法签名:static org.jaudiotagger.audio.AudioFileIO.read()适用于参数类型:(org.springframework.web.multipart.commons.CommonsMultipartFile)值:[org。 springframework.web.multipart.commons.CommonsMultipartFile@10a531]

由于 杰森

1 个答案:

答案 0 :(得分:3)

您的代码将MultiPart文件复制到文件,但仍然使用AudioFileIO的Multipart文件。

必须如此:

private moveFile(CommonsMultipartFile file){
    def userId = getUserId()
    def userGuid = SecUser.get(userId.id).uid
    def webRootDir = servletContext.getRealPath("/")

    def userDir = new File(webRootDir, "/myUsers/${userGuid}/music")
    userDir.mkdirs()
    File myFile = new File( userDir,file.originalFilename)
    file.transferTo(myFile)      

    //
    // !!!!!! you have to pass myFile there
    //
    AudioFile audioFile = AudioFileIO.read(myFile) 
}