查询使用Spring Multipart上传文件的当前大小

时间:2013-11-18 13:20:55

标签: ajax spring grails file-upload

我使用org.springframework.web.multipart API上传grails应用程序中的文件:

负责获取上传文件的当前大小的对象是什么:

  1. -request:
  2. -MultipartHttpServletRequest: ......或者什么
  3. 已知我使用以下方法上传文件:

       public static final Logger logger = Logger.getLogger(this.class);
        public File uploadFile(MultipartFile file, String baseDir){
            logger.info("Uploading a file ");
            boolean successUpload = false;
            File localDestination = null;
            if(!file.isEmpty()){
                String extension = parseExtension(file.getOriginalFilename()); 
                try{
                    successUpload = true ;
                    String fileName = baseDir+System.nanoTime()+extension ;
                    localDestination = new File(fileName);
                    file.transferTo(localDestination);
                    logger.info("><<SIZE MULTIPART1<>> = "+file.getSize());
                }catch(IllegalStateException ex){
                    logger.info("File already replaced");
                    successUpload = false;
                }catch (IOException ex) {
                    successUpload = false;
                }
            }
            if(successUpload){
                return localDestination;
            }else{
                return null;
            }
        }
    

    如何查询上传的当前大小,这意味着:控制器中的操作如何在文件上传时返回当前大小。

    更新:

    我在文件上传时测试此操作:

    def size(){
        MultipartFile file =  ((MultipartHttpServletRequest)request).getFile('ml_files')
        render(contentType:'text/json'){
            [size:file.getSize()]
        }
    }
    

    已知ml_files是用作请求标识符的最终值。

    当我用ajax查询此操作时,我收到以下错误消息:

    Cannot cast object 'org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper@6a4b6cd6' with class 
    
    'org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper' to class 'org.springframework.web.multipart.MultipartHttpServletRequest'.
    

0 个答案:

没有答案