/**
* 导入文件
* */
@ResponseBody
@RequestMapping("/uploadFile")
public ApiResponse<Object> uploadFile( HttpServletRequest servletRequest ){
MultipartFile file = null;
Map<String,Object> map = new HashMap<>();
ApiResponse<Object> apiResponse = new ApiResponse<>();
//前端传的文件 file
if (servletRequest instanceof StandardMultipartHttpServletRequest) {
MultiValueMap<String, MultipartFile> fileMap = ((StandardMultipartHttpServletRequest) servletRequest).getMultiFileMap();
file = fileMap.get("file").get(0);
}
//文件名
String filename = file.getOriginalFilename();
//获取参数
map.put("server",servletRequest.getParameter("server"));
map.put("category",servletRequest.getParameter("category"));
map.put("dynamic_refresh",servletRequest.getParameter("dynamic_refresh"));
map.put("dept_schedule",servletRequest.getParameter("dept_schedule"));
map.put("is_use",servletRequest.getParameter("is_use"));
map.put("remarks",servletRequest.getParameter("remarks"));
map.put("create_by",servletRequest.getParameter("create_by"));
map.put("create_time",servletRequest.getParameter("create_time"));
map.put("target_file",filename);
//文件后缀
String ext = filename.substring(filename.lastIndexOf(".")+1);
if ("yaml".equals(ext) || "yml".equals(ext)){
try {
//获取文件输入流
InputStream inputStream = file.getInputStream();
//放到缓冲区中,一次读取一行
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
StringBuffer sbf = new StringBuffer();
while((line = br.readLine()) != null) {
sbf.append(line).append("\n");
}
br.close();
String yamlFileStr = sbf.toString();
map.put("yamlFileStr",yamlFileStr);
apiResponse = this.paramSetService.uploadFile(map);
} catch (IOException e) {
e.printStackTrace();
}
}
return apiResponse;
}
04-14
2823
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-12