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

时间:2015-05-14 10:17:42

标签: java spring performance spring-mvc

我在进行性能测试时遇到了麻烦。该项目是一个春季MVC项目。没有上传文件,性能非常好。但是,文件上传会导致服务器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>

这是代码接收文件

@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) {
        ...
    }
}

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

0 个答案:

没有答案