ImportNumberController.java
3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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;
}
}