ImportNumberController.java 3.23 KB
package com.uccc.number.controller;

import com.alibaba.fastjson.JSONObject;
import com.uccc.number.service.RabbitMqService;
import com.uccc.pretty.common.Result;
import com.uccc.pretty.constants.ErrorCode;
import com.uccc.number.exception.ApiException;
import com.uccc.pretty.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

import static com.uccc.pretty.constants.ResultEnum.RESULT_ERR;
import static com.uccc.pretty.constants.ResultEnum.RESULT_OK;

/**
 * Created by bert on 2021-09-15 15:34
 */

@RestController
@RequestMapping("/import_number/")
public class ImportNumberController {
    private Logger logger = LoggerFactory.getLogger(ImportNumberController.class);

    @Autowired
    private RabbitMqService rabbitMqService;

    @RequestMapping(value="/file_import", method = RequestMethod.POST)
    public Result upload(@RequestParam("file") MultipartFile file) {
        Result result = new Result();
        if (file.isEmpty()) throw new ApiException(ErrorCode.FILE_IS_EMPTY);
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//        logger.info("file:{}", fileType);
        if (!fileType.equals(".xlsx")) throw new ApiException(ErrorCode.FILE_PREFIX_WRONG);
        //当前时间的 时分秒
        String fileName= DateUtils.getDate() + "_" + DateUtils.getTimeWithoutSecond().replaceAll(":", "_")+fileType;
        String path = "/Users/bert/Desktop/";

        File checkFile = new File( path + fileName);
        if (checkFile.exists()) throw new ApiException(ErrorCode.FILE_EXIST);

        InputStream is;
        try {
            is = file.getInputStream();
            FileOutputStream fos = new FileOutputStream(path + fileName);
            byte[] b = new byte[1024];
            while ((is.read(b)) != -1) {
                fos.write(b);// 写入数据
            }
            is.close();
            fos.close();// 保存数据
            File saveFile = new File( path + fileName);
            if (saveFile.exists()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", fileName);
                jsonObject.put("cts", System.currentTimeMillis());
                jsonObject.put("type", fileType);
                jsonObject.put("path", path);
                jsonObject.put("oldName",file.getOriginalFilename());
                rabbitMqService.sendMsg(jsonObject.toJSONString());
                //
                result.setCode(RESULT_OK.getCode());
                result.setMessage(RESULT_OK.getMessage());
            }else {
                result.setCode(RESULT_ERR.getCode());
                result.setMessage(RESULT_ERR.getMessage());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



        return result;
    }
}