package com.dxhy.common.config;

import java.io.File;

import javax.servlet.MultipartConfigElement;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

/**
 * @author jiaohongyang
 */
@Configuration
public class MultipartConfig {
    @Value("${server.basedir:}")
    private String tempath;

    /**
     * 文件上传临时路径
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = tempath + "/data/tmp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        // 文件最大 KB,MB
        factory.setMaxFileSize(DataSize.parse("30960KB"));
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.parse("30960KB"));
        return factory.createMultipartConfig();
    }
}
