文件上传及工具类

package com.data.manger.config.utils;

import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;

/**
 * 工具类
 */
@Component
public class FileUtil {
    public static final List<String> IMAGE_EXTENSIONS = Arrays.asList(".jpg", ".jpeg", ".png");
}

package com.data.manger.config.utils;

import java.util.UUID;

/**
 * 工具类
 */
public class CommonUtil {
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return uuid;
    }
}

package com.data.manger.config.utils;

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;

/**
 * 工具类
 */
public class DateUtil {
    public static final String HH = "HH";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final DateTimeFormatter FORMATTER_HH = DateTimeFormatter.ofPattern(HH);
    public static final DateTimeFormatter FORMATTER_YYYY_MM_DD = DateTimeFormatter.ofPattern(YYYY_MM_DD);
    public static String getHH() {
        return FORMATTER_HH.format(LocalDateTime.now());
    }
    public static String getYYYYMMDD() {
        return FORMATTER_YYYY_MM_DD.format(LocalDateTime.now());
    }

}

//Controller 多个文件
@ResponseBody
    @PostMapping("/updateImage")
    public Map<String, String> updateImage(@RequestParam("image") MultipartFile[] multfiles)  {
        log.info("time:" + DateUtils.getNowDate(new Date())
                + ",desc:进入TModelDdStructureAction类updateImage的方法,type:method");
        Map<String, String> result = new HashMap<>();
        if (multfiles.length == 0) {
            result.put("message", "请选择图片!");
            return result;
        }
        // 源文件名称
        final String originalFileName = multfiles[0].getOriginalFilename();
        if (StringUtils.isBlank(originalFileName)) {
            result.put("message", "请选择图片!");
            return result;
        }
        // 文件后缀[.jpg]
        final String suffix = originalFileName.substring(originalFileName.lastIndexOf(".")).toLowerCase();
        if (!FileUtil.IMAGE_EXTENSIONS.contains(suffix)) {
            result.put("message", "图片格式错误!");
            return result;
        }
        String lastFilePath;
        String newFileName = CommonUtil.getUUID() + suffix;
        String folderName = File.separator + "temp" + File.separator;
        String relativePath = folderName + DateUtil.getYYYYMMDD() + File.separator + DateUtil.getHH();
        String filePath = "F:\\edc\\optionSet\\img\\" + relativePath;
        String fileUrl = null;
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = null;
        try {
            lastFilePath = filePath + File.separator + newFileName;
            out = new FileOutputStream(lastFilePath);
            out.write(multfiles[0].getBytes());
            fileUrl = "http://localhost:9003" + relativePath + File.separator + newFileName;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (fileUrl == null) {
            result.put("message", "图片上传失败!");
            return result;
        }
        result.put("message", "上传成功!");
        result.put("url", fileUrl);
        log.info("time:" + DateUtils.getNowDate(new Date())
                + ",desc:离开TModelDdStructureAction类updateImage的方法,type:method");
        return result;
    }

 

//Controller   单个文件
@ResponseBody
    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    public JSONObject fileUpload(@RequestParam(value = "file") MultipartFile file,HttpServletRequest request ) {
        log.info("time:" + DateUtils.getNowDate(new Date())
                + ",desc:进入TModelDdStructureAction类fileUpload的方法,type:method");
        //项目路径
//        String deposeFilesDir = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
        Map<String, String> result = new HashMap<>();
        if (file.isEmpty()) {
            log.info("选项集图片文件为空!!!!!!!!!!!!!!!!!");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        log.info("文件上传名称为:"+ fileName);
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = Constants.fileUrl; // 上传后的路径 "F:/EDC/ptionSet/img/"  "/opt/EDC/ptionSet/img/"
        fileName = UUID.randomUUID() + "####" + fileName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            result.put("message","上传成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        result.put("url",filePath + fileName);
        log.info("time:" + DateUtils.getNowDate(new Date())
                + ",desc:离开TModelDdStructureAction类fileUpload的方法,type:method");
        return ResultUtils.successResult(result);
    }

 

文件上传及工具类

上一篇:Jenkins自动化部署安卓项目


下一篇:AngularJs 三目运算符