optimize codes
This commit is contained in:
@@ -116,12 +116,12 @@ public class PlayerController {
|
||||
String roomType = params.get("roomType");
|
||||
String property = params.get("property");
|
||||
|
||||
if (StringUtils.isEmpty(userId)||StringUtils.isEmpty(roomType)||StringUtils.isEmpty(property)) {
|
||||
if (StringUtils.isAnyEmpty(userId, roomType, property)) {
|
||||
response.sendError(422, "Miss parameter");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!StringUtils.isEmpty(userId)) {
|
||||
if(StringUtils.isNotEmpty(userId)) {
|
||||
userId = Strings.CS.replace(userId, "@mgm.mo", "");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
package com.mgm.mcrm.admin.controllers;
|
||||
|
||||
import com.mgm.mcrm.common.constants.SysCommon;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/crm_api")
|
||||
public class SSOController {
|
||||
|
||||
private static final String TEXT_SPLIT_CHAR = ",";
|
||||
|
||||
@RequestMapping(value="/app/sso/export_folder",method = RequestMethod.GET)
|
||||
public String exportFolderToData(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
String text = "";
|
||||
try {
|
||||
String basePath = SysCommon.runtime_path + "\\"+SysCommon.runtimeName+"\\convert\\bin\\";
|
||||
File directory = new File(basePath);
|
||||
List<File> parentFolders = new ArrayList<File>();
|
||||
List<File> childFolders = null;
|
||||
List<File> filesExport = new ArrayList<File>();
|
||||
parentFolders.add(directory);
|
||||
while(!parentFolders.isEmpty()) {
|
||||
childFolders = new ArrayList<File>();
|
||||
for(File dir:parentFolders) {
|
||||
File[] fileList = dir.listFiles();
|
||||
if(fileList!=null && fileList.length>0) {
|
||||
for(File subFile:fileList) {
|
||||
if(subFile.isDirectory()) {
|
||||
childFolders.add(subFile);
|
||||
}else {
|
||||
filesExport.add(subFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
parentFolders = childFolders;
|
||||
}
|
||||
if(filesExport.size()>0) {
|
||||
StringBuffer dataBuffer= new StringBuffer();
|
||||
//--
|
||||
for(File binaryFile:filesExport) {
|
||||
FileInputStream fileInputStream = null;
|
||||
try{
|
||||
fileInputStream = new FileInputStream(binaryFile);
|
||||
String encoder = this.encode(fileInputStream);
|
||||
String filePath = binaryFile.getPath().replace(basePath, "");
|
||||
if (encoder != null) {
|
||||
if(dataBuffer.length()>0) {
|
||||
dataBuffer.append(";");
|
||||
}
|
||||
dataBuffer.append("[{"+filePath+"};{"+encoder+"}]");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if(fileInputStream != null) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (IOException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//--
|
||||
String outputFilePath = SysCommon.runtime_path + "\\"+SysCommon.runtimeName+"\\convert\\text\\exportfile.data";
|
||||
File outputFile = new File(outputFilePath);
|
||||
if(!outputFile.exists())
|
||||
outputFile.createNewFile();
|
||||
BufferedWriter bufferedWriter = null;
|
||||
try {
|
||||
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile)));
|
||||
String totalContent = dataBuffer.toString();
|
||||
bufferedWriter.write(totalContent);
|
||||
bufferedWriter.flush();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (bufferedWriter != null) {
|
||||
try {
|
||||
bufferedWriter.close();
|
||||
} catch (IOException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
text += "Export total "+filesExport.size()+" files to exportfile.data";
|
||||
}catch (Exception err) {
|
||||
text += err.getMessage();
|
||||
}
|
||||
// 设置内容类型和字符编码
|
||||
response.setContentType("text/plain");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
out.print(text);
|
||||
out.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping(value="/app/sso/import_folder",method = RequestMethod.GET)
|
||||
public String importDataToFolder(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
String text = "";
|
||||
int processCnt=0;
|
||||
try {
|
||||
String baseOutputPath = SysCommon.runtime_path + "\\"+SysCommon.runtimeName+"\\convert\\bin\\";
|
||||
File dataFile = new File(SysCommon.runtime_path + "\\"+SysCommon.runtimeName+"\\convert\\text\\exportfile.data");
|
||||
if(dataFile.exists()) {
|
||||
BufferedReader bufferReader = null;
|
||||
String dataContent="";
|
||||
try {
|
||||
bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile)));
|
||||
String lineContent="";
|
||||
while(lineContent !=null) {
|
||||
lineContent = bufferReader.readLine();
|
||||
if(lineContent!=null) {
|
||||
dataContent+=lineContent;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(StringUtils.isNotBlank(dataContent)) {
|
||||
String[] segmentParts = dataContent.split("\\];\\[");
|
||||
if(segmentParts!=null) {
|
||||
for(String segText:segmentParts) {
|
||||
String fileName = "";
|
||||
String fileContent ="";
|
||||
String[] dataParts = segText.split("\\};\\{");
|
||||
for(int i=0;i<dataParts.length;i++) {
|
||||
String ct = dataParts[i].replace("[", "").replace("]", "").replace("{", "").replace("}", "");
|
||||
if(i==0) {
|
||||
fileName = ct;
|
||||
}else {
|
||||
fileContent=ct;
|
||||
}
|
||||
}
|
||||
if(StringUtils.isNotBlank(fileName)) {
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
String fullFileName = baseOutputPath+fileName;
|
||||
int pathIndex = fullFileName.lastIndexOf("\\");
|
||||
if(pathIndex>0) {
|
||||
String fileFolderName = fullFileName.substring(0,pathIndex);
|
||||
File fileFolder = new File(fileFolderName);
|
||||
if(!fileFolder.exists() || !fileFolder.isDirectory()) {
|
||||
fileFolder.mkdirs();
|
||||
}
|
||||
}
|
||||
File outputFile = new File(fullFileName);
|
||||
if (!outputFile.exists())
|
||||
outputFile.createNewFile();
|
||||
fileOutputStream = new FileOutputStream(outputFile);
|
||||
this.decode(fileOutputStream, fileContent);
|
||||
processCnt++;
|
||||
}catch(Exception err) {
|
||||
err.printStackTrace();
|
||||
}finally {
|
||||
if (fileOutputStream != null) {
|
||||
try {
|
||||
fileOutputStream.close();
|
||||
} catch (IOException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
text="Import "+processCnt+" files.";
|
||||
}else {
|
||||
text="Empty Content.";
|
||||
}
|
||||
}else {
|
||||
text="Data file not found.";
|
||||
}
|
||||
}catch (Exception err) {
|
||||
text+= err.getMessage();
|
||||
}
|
||||
// 设置内容类型和字符编码
|
||||
response.setContentType("text/plain");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
out.print(text);
|
||||
out.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
public String encode(FileInputStream fileInputStream) throws IOException {
|
||||
if (fileInputStream == null) {
|
||||
return null;
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
int i = 0;
|
||||
while ((i = fileInputStream.read()) != -1) {
|
||||
builder.append(i + TEXT_SPLIT_CHAR);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public void decode(FileOutputStream fileOutputStream, String text) throws IOException {
|
||||
if (fileOutputStream == null || text == null) {
|
||||
return;
|
||||
}
|
||||
String[] lineone = text.split(TEXT_SPLIT_CHAR);
|
||||
for (int j = 0; j < lineone.length; j++) {
|
||||
fileOutputStream.write(Integer.valueOf(lineone[j]));
|
||||
}
|
||||
fileOutputStream.flush();
|
||||
}
|
||||
}
|
||||
@@ -65,8 +65,8 @@ public class WebFilterConfig {
|
||||
"/",
|
||||
"/sign-off",
|
||||
"/crm_api/sso_login",
|
||||
"/crm_api/import_folder",
|
||||
"/crm_api/export_folder",
|
||||
"/crm_api/app/sso/import_folder",
|
||||
"/crm_api/app/sso/export_folder",
|
||||
"/swagger*/*",
|
||||
"/v3/**",
|
||||
"/api-docs",
|
||||
|
||||
Reference in New Issue
Block a user