Java 如何提高spring-mvc文件上传的性能

Java 如何提高spring-mvc文件上传的性能,java,spring,performance,spring-mvc,Java,Spring,Performance,Spring Mvc,我在做性能测试时遇到了麻烦。该项目是一个SpringMVC项目。没有上传文件,性能非常好。然而,文件上传会导致服务器CPU非常高,但占用的网络却很少 这是多部分解析器的配置 <bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="2097

我在做性能测试时遇到了麻烦。该项目是一个SpringMVC项目。没有上传文件,性能非常好。然而,文件上传会导致服务器CPU非常高,但占用的网络却很少

这是多部分解析器的配置

<bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    <property name="maxUploadSize" value="20971520"/>  
    <property name="resolveLazily" value="true"/>  
    <property name="maxInMemorySize" value="5242880"/>  
    <property name="defaultEncoding" value="UTF-8"/>  
</bean>

有没有办法优化文件上传的性能?谢谢。

使用探查器查看花费的时间。您的代码中没有明显的缺陷。如果您使用文件上载,内存中会有很多内容,这将导致垃圾收集。您还创建了许多字符串,这些字符串也会触发gc。当内存几乎满时,您将获得越来越多的GC。本文介绍了如何使用Apache Commons文件上载,而不是使用多部分文件抽象来尝试流式文件上载。然后,您可以直接将流式文件传输到磁盘,而不是先传输到内存/磁盘,然后再传输到磁盘。节省空间和性能。
@RequestMapping("/uploadVideo")
@ResponseBody
public String uploadVideo(@RequestParam(value = "video", required = false) CommonsMultipartFile video,
        HttpServletRequest request) {

    try {
        String fileOriginName = video.getOriginalFilename();
        String suffix = fileOriginName.substring(fileOriginName.lastIndexOf(".") + 1).toLowerCase();

        String fileName = UUID.randomUUID().toString().replace("-", "");
        fileName = fileName + "." + suffix;

        String currentDate = DateUtil.getCurrentDate();
        String savePath = SystemConfig.getSystemConfig().getUploadPath() + File.separator + currentDate;
        String uploadPath = request.getServletContext().getRealPath("/")
                + File.separator + savePath;
        File directory = new File(uploadPath);
        if (!directory.exists()) {
            directory.mkdirs();
        }

        String filePath = uploadPath + File.separator + fileName;
        File videoFile = new File(filePath);

        //save file
        video.transferTo(videoFile);

        ...
    } catch (IllegalStateException e) {
        ...
    } catch (IOException e) {
        ...
    } catch (Exception e) {
        ...
    }
}