package com.lxzn.base.controller; import cn.hutool.crypto.SmUtil; import com.lxzn.base.service.IDncPassLogService; import com.lxzn.base.vo.FileDetail; import com.lxzn.base.vo.PostParams; import com.lxzn.base.vo.RespData; import com.lxzn.base.vo.TokenResp; import com.lxzn.framework.domain.base.SysLogTypeObjectDto; import com.lxzn.framework.utils.JwTUtil; import com.lxzn.framework.utils.SyslogClient; import com.lxzn.framework.utils.date.DateUtil; import com.lxzn.framework.utils.file.FileNumUtil; import com.lxzn.framework.utils.file.FileUtil; import com.lxzn.framework.utils.file.SM3Util; import org.apache.commons.lang3.StringUtils; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.*; import org.springframework.web.method.HandlerMethod; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.URLDecoder; import java.net.UnknownHostException; /** * @author clown * * @date 2023/12/14 */ @RestController @RequestMapping("/outer") @PropertySource("classpath:/scheduled.properties") public class ServiceOuterController { @Autowired private IDncPassLogService dncPassLogService; @RequestMapping("/nc") public void fileClientTxtOrNc() { dncPassLogService.fileClientTxtOrNc(); } @Value("${serviceIntranet.appIdCheck}") private String appIdCheck; @Value("${serviceIntranet.passwordCheck}") private String passwordCheck; @Value("${serviceIntranet.logIp}") private String logIp; @Value("${serviceIntranet.logPort}") private String logPort; @Value("${serviceIntranet.clientIp}") private String clientIp; @Value("${fileNumPath}") private String fileNumPath; @GetMapping("/test") public void analysisAndCopyFile(){ //receiveFileService.analysisAndCopyFile(); } //PostParams属性:string appId,string password @PostMapping(value = "/appAuth") public TokenResp getToken(@RequestBody PostParams postParams){ //获取请求头中传入的appId和password String appId = postParams.getAppId(); String password = postParams.getPassword(); //用于校验的正确的appId和password(根据业务调整) /*String appIdCheck = "admin"; String passwordCheck = "123";*/ //校验appId和paaword,由于测试用的明文密码,SM3Util.verify()先将铭文密码SM3加密后再与传入password对比 if(appIdCheck.equals(appId)&& SM3Util.verify(passwordCheck,password)){ //校验通过,生成token签名 String token = JwTUtil.sign(appId, passwordCheck); return new TokenResp("200","认证成功",token); }else{ //to-do认证失败 return null; } } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){ //如果不是映射到方法直接通过 if (!(handler instanceof HandlerMethod)) { return true; } //从 http 请求头中取出 token String token = request.getHeader("Token"); if (token == null) { throw new RuntimeException("无 token ,请重新登陆"); } return true; } @PostMapping(value = "/fileUpload") public RespData uploadFiles(HttpServletRequest request) { SysLogTypeObjectDto objectName = new SysLogTypeObjectDto(); objectName.setDateTime(DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_SMALL)); FileDetail fileDetail = new FileDetail(); try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //获取FileName if(request.getHeader("FileName")!=null){ try { fileDetail.setFileName(URLDecoder.decode(request.getHeader("FileName"),"utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //获取Content-Length if(request.getHeader("Content-Length")!=null){ try { fileDetail.setContentLength(URLDecoder.decode(request.getHeader("Content-Length"),"utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //获取文件流 ServletInputStream inputStream = null; try { inputStream = request.getInputStream(); } catch (IOException e) { e.printStackTrace(); } //操作文件流,上传文件 //fileService.uploadFile(inputStream,fileDetail.getFileName()); //DataInputStream in = new DataInputStream(inputStream); FileOutputStream fileOut = null; try { fileOut = new FileOutputStream(fileDetail.getFileName());//这里可以改路径 IOUtils.copy(inputStream, fileOut); fileOut.flush(); fileOut.flush(); objectName.setResult("成功"); File file = new File(fileDetail.getFileName()); objectName.setFileName(file.getName()); objectName.setFileSize(String.valueOf(file.length())); objectName.setDateTime(DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_SMALL)); objectName.setFileSize(FileUtil.changeFileFormatKb(String.valueOf(file.length()))); objectName.setDestination("10.118.10.13");//服务端IP objectName.setSourceAddress("10.118.15.11");//客户端 String sm3 = SmUtil.sm3(file); objectName.setAbstract1(sm3); //不记录数据量 String fileNum = FileNumUtil.readFileSum(fileNumPath); if (StringUtils.isBlank(fileNum)) { objectName.setFileNum(Integer.toString(1)); FileNumUtil.fileWriterSql(Integer.toString(1),fileNumPath); } else { Integer num = Integer.valueOf(fileNum) + 1; objectName.setFileNum(Integer.toString(num)); FileNumUtil.fileWriterSql(Integer.toString(num),fileNumPath); } InetAddress address = null; try { address = InetAddress.getLocalHost(); String ip = address.getHostAddress(); objectName.setAddress(ip); } catch (UnknownHostException e) { objectName.setAddress("10.118.10.13"); } try { objectName.setTypes("Info"); SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString()); }catch (Exception e) { } } catch (IOException e) { objectName.setResult("失败"); objectName.setTypes("error"); SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString()); e.printStackTrace(); }finally { try { if (inputStream != null) { inputStream.close(); } if (fileOut != null) { fileOut.close(); } } catch (IOException e) { e.printStackTrace(); } } return new RespData("200", "文件接收成功"); } }