Java tutorial
package com.emaxcore.emaxdata.modules.testdrive.pub.web; import com.emaxcore.emaxdata.common.config.Global; import com.emaxcore.emaxdata.modules.testdrive.entity.TestDriveApplyInfo; import com.emaxcore.emaxdata.modules.testdrive.service.TestDriveApplyInfoService; import com.emaxcore.emaxdata.common.mapper.JsonMapper; import com.emaxcore.emaxdata.common.utils.AES256EncryptionUtils; import com.emaxcore.emaxdata.common.utils.FileEmaxDataUtils; import com.emaxcore.emaxdata.modules.testdrive.pub.constant.TestdrivePubConstant; import com.emaxcore.emaxdata.modules.testdrive.pub.utils.MD5Utils; import com.emaxcore.emaxdata.modules.testdrive.pub.utils.TestdrivePubUtils; import com.emaxcore.emaxdata.modules.testdrive.service.TestDriveResourcePersonService; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * ??????? * <p> * Created by yxtwang on 2017/5/19. */ @Controller @RequestMapping(value = "${publicPath}/app") public class TestdriveUploadSignatureController { private static Logger logger = LoggerFactory.getLogger(TestdriveUploadSignatureController.class); private static boolean isDebugLogger = logger.isDebugEnabled(); //? private static String card = "-card-"; // private static String protocol = "-protocol-"; //???? private static String drive = "-drive-"; @Autowired private TestDriveApplyInfoService testDriveApplyInfoService; @Autowired private TestDriveResourcePersonService testDriveResourcePersonService; /** * ??????? * * @param model * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "uploadSignature") @ResponseBody public String uploadSignature(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException { if (isDebugLogger) { logger.debug(" ??------uploadSignature-------start--------"); } response.setContentType(TestdrivePubConstant.ENCODED); Map<?, ?> jsonMap = null; // ? // ??????? StringBuilder idcardDriveProtocol = new StringBuilder(); // ??????? CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // json String jsonDataStr = multiRequest.getParameter(TestdrivePubConstant.VALUE); if (Global.isSecretMode()) { if (isDebugLogger) { logger.debug("app ?---------- Start------{}---------", jsonDataStr); } AES256EncryptionUtils des = AES256EncryptionUtils.getInstance(); String jsonData = des.decrypt(jsonDataStr); jsonMap = JsonMapper.nonDefaultMapper().fromJson(jsonData, HashMap.class); } else { jsonMap = JsonMapper.nonDefaultMapper().fromJson(jsonDataStr, HashMap.class); } if (isDebugLogger) { logger.debug("uploadSignature?----------jsonMap---{}---------", jsonMap); } // ? TestDriveApplyInfo testDriveApplyInfo = new TestDriveApplyInfo(); //??-- String userFristName = TestdrivePubUtils.readJsonMapValue("fristName", jsonMap); if (StringUtils.isBlank(userFristName)) { jsonMap = this.saveJson(TestdrivePubConstant.ERROR_STATE, "fristName", ""); return TestdrivePubUtils.loggerJsonMap(jsonMap); } testDriveApplyInfo.setFirstName(userFristName); //??-- String userLastName = TestdrivePubUtils.readJsonMapValue("lastName", jsonMap); if (StringUtils.isBlank(userLastName)) { jsonMap = this.saveJson(TestdrivePubConstant.ERROR_STATE, "lastName", ""); return TestdrivePubUtils.loggerJsonMap(jsonMap); } testDriveApplyInfo.setLastName(userLastName); //?? String userName = userFristName + userLastName; testDriveApplyInfo.setName(userName); //telephone String telephone = TestdrivePubUtils.readJsonMapValue("telephone", jsonMap); if (StringUtils.isBlank(telephone)) { jsonMap = this.saveJson(TestdrivePubConstant.ERROR_STATE, "telephone", ""); return TestdrivePubUtils.loggerJsonMap(jsonMap); } testDriveApplyInfo.setMobile(telephone); //appellation String appellation = TestdrivePubUtils.readJsonMapValue("appellation", jsonMap); if (StringUtils.isBlank(appellation)) { jsonMap = this.saveJson(TestdrivePubConstant.ERROR_STATE, "appellation", ""); return TestdrivePubUtils.loggerJsonMap(jsonMap); } testDriveApplyInfo.setSex(appellation); //motorcycleType String motorcycleType = TestdrivePubUtils.readJsonMapValue("motorcycleType", jsonMap); if (StringUtils.isBlank(motorcycleType)) { jsonMap = this.saveJson(TestdrivePubConstant.ERROR_STATE, "motorcycleType", ""); return TestdrivePubUtils.loggerJsonMap(jsonMap); } testDriveApplyInfo.setVehiclesType(motorcycleType); // String widthName = TestdrivePubUtils.readJsonMapValue("userName", jsonMap); if (StringUtils.isBlank(widthName)) { jsonMap = this.saveJson(TestdrivePubConstant.ERROR_STATE, "userName", ""); return TestdrivePubUtils.loggerJsonMap(jsonMap); } testDriveApplyInfo.setWidthName(widthName); // ?MD5 String idCarMD5Hash = TestdrivePubUtils.readJsonMapValue("IDCarMD5Hash", jsonMap); // MD5 String driveMD5Hash = TestdrivePubUtils.readJsonMapValue("driveMD5Hash", jsonMap); // ????MD5 String protocolMD5Hash = TestdrivePubUtils.readJsonMapValue("protocolMD5Hash", jsonMap); // ??? String cidNumber = TestdrivePubUtils.readJsonMapValue("IDNumber", jsonMap); if (StringUtils.isBlank(cidNumber)) { jsonMap = this.saveJson(TestdrivePubConstant.ERROR_STATE, "???", ""); return TestdrivePubUtils.loggerJsonMap(jsonMap); } testDriveApplyInfo.setIdNumber(cidNumber); // ?D:\apache-tomcat-7.0.47\webapps\benz String path = TestdrivePubUtils.getRealPath(request); // ????/mnt/sdc1/data/benzsite String savePath = Global.getUserfilesBaseDir(); // ?/userfiles/app/login ??login String signaturePicAddressPre = TestdrivePubConstant.APP_USER_FILES_PATH + userName; String filePath = ""; // /benz String projectPath = request.getContextPath(); if (StringUtils.isBlank(savePath)) { // ?D:\apache-tomcat-7.0.47\webapps\benz/userfiles/app/login filePath = path + signaturePicAddressPre; } else { filePath = savePath + projectPath + signaturePicAddressPre; } Iterator<String> iter = multiRequest.getFileNames(); while (iter.hasNext()) { MultipartFile file = multiRequest.getFile((String) iter.next()); if (file != null && !file.isEmpty()) { String fileName = file.getOriginalFilename(); FileEmaxDataUtils.createDirectory(filePath); String fileNamePath = filePath + File.separator + fileName; String relativePath = projectPath + signaturePicAddressPre + File.separator + fileName; File localFile = new File(fileNamePath); file.transferTo(localFile); String md5Hash = MD5Utils.getFileMD5String(localFile); // ? if (fileName.startsWith(TestdrivePubConstant.IDCARD_PREFIX)) { if (!idCarMD5Hash.equals(md5Hash)) { idcardDriveProtocol.append(card); boolean deletFlag = localFile.delete(); if (deletFlag) { logger.info("??"); } } else { testDriveApplyInfo.setIdentityCardScanningFile(relativePath); } } // if (fileName.startsWith(TestdrivePubConstant.DRIVE_PREFIX)) { if (!driveMD5Hash.equals(md5Hash)) { idcardDriveProtocol.append(drive); boolean deletFlag = localFile.delete(); if (deletFlag) { logger.info("?!"); } } else { testDriveApplyInfo.setDriversLicenseScanningFile(relativePath); } } // ???? if (fileName.startsWith(TestdrivePubConstant.PROTOCOL_PREFIX)) { if (!protocolMD5Hash.equals(md5Hash)) { idcardDriveProtocol.append(protocol); boolean deletFlag = localFile.delete(); if (deletFlag) { logger.info("?????"); } } else { testDriveApplyInfo.setNumberSignature(relativePath); } } } } // ????1?1? String driveStatus = testDriveApplyInfo.getDriveStatus(); if ((StringUtils.isNotBlank(driveStatus) && (Integer.parseInt(driveStatus) < 1)) || StringUtils.isBlank(driveStatus)) { testDriveApplyInfo.setDriveStatus("1");// ?? } testDriveApplyInfoService.save(testDriveApplyInfo); } else { jsonMap = this.saveJson(TestdrivePubConstant.SUCCESS_STATE, "??", ""); return TestdrivePubUtils.loggerJsonMap(jsonMap); } if (StringUtils.isNotBlank(idcardDriveProtocol)) { jsonMap = this.saveJson(TestdrivePubConstant.ERROR_STATE, "", idcardDriveProtocol.toString()); return TestdrivePubUtils.loggerJsonMap(jsonMap); } else { jsonMap = this.saveJson(TestdrivePubConstant.SUCCESS_STATE, TestdrivePubConstant.SUCCESS, idcardDriveProtocol.toString()); } String appJson = TestdrivePubUtils.loggerJsonMap(jsonMap); if (isDebugLogger) { logger.debug("??--uploadSignature----end---"); } return appJson; } /** * json ? * * @param state * @param msg */ private Map<String, Object> saveJson(final String state, final String msg, final String idcardDriveProtocol) { Map<String, Object> mapData = new HashMap(); mapData.put(TestdrivePubConstant.ERRORMSG, msg); mapData.put(TestdrivePubConstant.STATE, state); StringBuilder message = new StringBuilder(""); if (idcardDriveProtocol.contains(card)) { message.append("??"); } if (idcardDriveProtocol.contains(drive)) { message.append("??"); } if (idcardDriveProtocol.contains(protocol)) { message.append("???"); } String messages = message.toString(); if (StringUtils.isNotBlank(messages)) { mapData.put(TestdrivePubConstant.ERRORMSG, messages); return mapData; } return mapData; } }