Spring 中上传文件的关键语句

// 转型为MultipartHttpRequest:   

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;   

        // 获得文件:   

        MultipartFile file = multipartRequest.getFile("filename");    //filename表单参数名

        if(MyStringUtil.isEmptyOrNullAfterTrim(file.getOriginalFilename())){

        throw new RuntimeException("上传文件不能为空");

        }

FileCopyUtils.copy(file.getBytes(), objectFile);

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.