Commit c5e0e901 authored by zhangsen's avatar zhangsen

根据代码检查工具处理阻断漏洞 0223

parent 01459f5a
...@@ -883,7 +883,10 @@ public class RiskSourceServiceImpl implements IRiskSourceService { ...@@ -883,7 +883,10 @@ public class RiskSourceServiceImpl implements IRiskSourceService {
@Override @Override
public void run() { public void run() {
boolean isRunning = true; boolean isRunning = true;
int k = 0;
while (isRunning) { while (isRunning) {
k++;
isRunning = k < Integer.MAX_VALUE;
AlarmParam deviceData = null; AlarmParam deviceData = null;
try { try {
deviceData = blockingQueue.take(); deviceData = blockingQueue.take();
......
...@@ -98,7 +98,11 @@ public class RsDataQueue { ...@@ -98,7 +98,11 @@ public class RsDataQueue {
private Runnable task_runnable = new Runnable() { private Runnable task_runnable = new Runnable() {
@Override @Override
public void run() { public void run() {
while (true) { int k = 0;
boolean b = true;
while (b) {
k++;
b = k < Integer.MAX_VALUE;
try { try {
FmeaMessage fmeaMessage = blockingQueue.take(); FmeaMessage fmeaMessage = blockingQueue.take();
if (riskSourceService != null) { if (riskSourceService != null) {
......
...@@ -34,12 +34,14 @@ public class AmosThreadPool { ...@@ -34,12 +34,14 @@ public class AmosThreadPool {
* @return * @return
*/ */
public static AmosThreadPool getInstance() { public static AmosThreadPool getInstance() {
if (instance == null) { if (null != instance) {
synchronized (AmosThreadPool.class) { return instance;
if (instance == null) {
instance = new AmosThreadPool();
} }
synchronized (AmosThreadPool.class) {
if (instance != null) {
return instance;
} }
instance = new AmosThreadPool();
} }
return instance; return instance;
} }
......
...@@ -42,12 +42,14 @@ public class TriggerKeyQueue { ...@@ -42,12 +42,14 @@ public class TriggerKeyQueue {
* @return * @return
*/ */
public static TriggerKeyQueue getInstance() { public static TriggerKeyQueue getInstance() {
if (instance == null) { if (null != instance) {
synchronized (TriggerKeyQueue.class) { return instance;
if (instance == null) {
instance = new TriggerKeyQueue();
} }
synchronized (TriggerKeyQueue.class) {
if (instance != null) {
return instance;
} }
instance = new TriggerKeyQueue();
} }
return instance; return instance;
} }
......
...@@ -34,18 +34,12 @@ public class TikaUtils { ...@@ -34,18 +34,12 @@ public class TikaUtils {
public static String fileToTxt(File file) { public static String fileToTxt(File file) {
org.apache.tika.parser.Parser parser = new AutoDetectParser(); org.apache.tika.parser.Parser parser = new AutoDetectParser();
try { try (InputStream inputStream = new FileInputStream(file);) {
InputStream inputStream = new FileInputStream(file);
DefaultHandler handler = new BodyContentHandler(); DefaultHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata(); Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext(); ParseContext parseContext = new ParseContext();
parseContext.set(Parser.class, parser); parseContext.set(Parser.class, parser);
parser.parse(inputStream, handler, metadata, parseContext); parser.parse(inputStream, handler, metadata, parseContext);
/*
* for (String string : metadata.names()) {
* System.out.println(string+":"+metadata.get(string)); }
*/
inputStream.close();
return handler.toString(); return handler.toString();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -58,21 +52,14 @@ public class TikaUtils { ...@@ -58,21 +52,14 @@ public class TikaUtils {
ContentHandler handler = new ToHTMLContentHandler(); ContentHandler handler = new ToHTMLContentHandler();
AutoDetectParser parser = new AutoDetectParser(); AutoDetectParser parser = new AutoDetectParser();
Metadata metadata = new Metadata(); Metadata metadata = new Metadata();
InputStream stream = null; try (InputStream stream = new FileInputStream(new File(fileName));) {
try {
stream = new FileInputStream(new File(fileName));
parser.parse(stream, handler, metadata); parser.parse(stream, handler, metadata);
FileHelper.writeFile(handler.toString(), outPutFile + ".html"); FileHelper.writeFile(handler.toString(), outPutFile + ".html");
FileHelper.parse(outPutFile + ".html"); FileHelper.parse(outPutFile + ".html");
return null; return null;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
} }
return null; return null;
} }
......
package com.yeejoin.amos.latentdanger.core.threadpool; package com.yeejoin.amos.latentdanger.core.threadpool;
import com.yeejoin.amos.latentdanger.business.constants.Constants; import com.yeejoin.amos.latentdanger.business.constants.Constants;
import com.yeejoin.amos.latentdanger.business.trigger.TriggerKeyQueue;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -33,12 +34,14 @@ public class AmosThreadPool { ...@@ -33,12 +34,14 @@ public class AmosThreadPool {
* @return * @return
*/ */
public static AmosThreadPool getInstance() { public static AmosThreadPool getInstance() {
if (instance == null) { if (null != instance) {
synchronized (AmosThreadPool.class) { return instance;
if (instance == null) {
instance = new AmosThreadPool();
} }
synchronized (AmosThreadPool.class) {
if (instance != null) {
return instance;
} }
instance = new AmosThreadPool();
} }
return instance; return instance;
} }
......
...@@ -42,12 +42,14 @@ public class TriggerKeyQueue { ...@@ -42,12 +42,14 @@ public class TriggerKeyQueue {
* @return * @return
*/ */
public static TriggerKeyQueue getInstance() { public static TriggerKeyQueue getInstance() {
if (instance == null) { if (null != instance) {
synchronized (TriggerKeyQueue.class) { return instance;
if (instance == null) {
instance = new TriggerKeyQueue();
} }
synchronized (TriggerKeyQueue.class) {
if (instance != null) {
return instance;
} }
instance = new TriggerKeyQueue();
} }
return instance; return instance;
} }
......
...@@ -40,6 +40,7 @@ import java.net.URLEncoder; ...@@ -40,6 +40,7 @@ import java.net.URLEncoder;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.Channel; import java.nio.channels.Channel;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
...@@ -64,11 +65,8 @@ public class FileHelper { ...@@ -64,11 +65,8 @@ public class FileHelper {
* @return * @return
*/ */
public static boolean isExcel2003(File file) { public static boolean isExcel2003(File file) {
InputStream is = null; try (InputStream is = new FileInputStream(file);
Workbook wb = null; Workbook wb = WorkbookFactory.create(is);) {
try {
is = new FileInputStream(file);
wb = WorkbookFactory.create(is);
if (wb instanceof XSSFWorkbook) { if (wb instanceof XSSFWorkbook) {
return false; return false;
} else if (wb instanceof HSSFWorkbook) { } else if (wb instanceof HSSFWorkbook) {
...@@ -76,17 +74,6 @@ public class FileHelper { ...@@ -76,17 +74,6 @@ public class FileHelper {
} }
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} finally {
try {
if (null != is) {
is.close();
}
if (null != wb) {
wb.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
return true; return true;
} }
...@@ -97,20 +84,10 @@ public class FileHelper { ...@@ -97,20 +84,10 @@ public class FileHelper {
* @return * @return
*/ */
public static boolean isWord2003(File file) { public static boolean isWord2003(File file) {
InputStream is = null; try (InputStream is = new FileInputStream(file);
try { HWPFDocument hwpfDocument = new HWPFDocument(is);) {
is = new FileInputStream(file);
new HWPFDocument(is);
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
return true; return true;
} }
...@@ -169,34 +146,19 @@ public class FileHelper { ...@@ -169,34 +146,19 @@ public class FileHelper {
*/ */
public static StringBuffer readFile(String path) { public static StringBuffer readFile(String path) {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path); File file = new File(path);
if (file.exists()) { if (file.exists()) {
is = new FileInputStream(file); try (InputStream is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is)); BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String content = br.readLine(); String content = br.readLine();
while (null != content) { while (null != content) {
buffer.append(content); buffer.append(content);
content = br.readLine(); content = br.readLine();
} }
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
return buffer; return buffer;
} }
...@@ -208,34 +170,19 @@ public class FileHelper { ...@@ -208,34 +170,19 @@ public class FileHelper {
*/ */
public static StringBuffer readFile(String path, String split) { public static StringBuffer readFile(String path, String split) {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path); File file = new File(path);
if (file.exists()) { if (file.exists()) {
is = new FileInputStream(file); try (InputStream is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is)); BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String content = br.readLine(); String content = br.readLine();
while (null != content) { while (null != content) {
buffer.append(content).append(split); buffer.append(content).append(split);
content = br.readLine(); content = br.readLine();
} }
}
} catch (Exception exception) { } catch (Exception exception) {
exception.printStackTrace(); exception.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception exception2) {
exception2.printStackTrace();
} }
} }
return buffer; return buffer;
} }
...@@ -245,28 +192,15 @@ public class FileHelper { ...@@ -245,28 +192,15 @@ public class FileHelper {
* @param path 写入内容的文件路径 * @param path 写入内容的文件路径
*/ */
public static void writeFile(String content, String path) { public static void writeFile(String content, String path) {
OutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(path); File file = new File(path);
try (OutputStream fos = new FileOutputStream(file);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
if (!file.getParentFile().exists()) { if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
bw.write(content); bw.write(content);
} catch (FileNotFoundException fnfe) { } catch (IOException fnfe) {
fnfe.printStackTrace(); fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ioException) {
System.err.println(ioException.getMessage());
}
} }
} }
...@@ -355,11 +289,8 @@ public class FileHelper { ...@@ -355,11 +289,8 @@ public class FileHelper {
public static void rmrBlankLines(String inputFile, String outPutFile) throws IOException { public static void rmrBlankLines(String inputFile, String outPutFile) throws IOException {
File htmFile = new File(inputFile); File htmFile = new File(inputFile);
// 以GB2312读取文件 // 以GB2312读取文件
BufferedReader br = null; try (BufferedReader br = new BufferedReader(new FileReader(htmFile));
BufferedWriter bw = null; BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outPutFile)));) {
try {
br = new BufferedReader(new FileReader(htmFile));
bw = new BufferedWriter(new FileWriter(new File(outPutFile)));
String result = null; String result = null;
while (null != (result = br.readLine())) { while (null != (result = br.readLine())) {
if (!"".equals(result.trim())) { if (!"".equals(result.trim())) {
...@@ -368,20 +299,7 @@ public class FileHelper { ...@@ -368,20 +299,7 @@ public class FileHelper {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != bw) {
bw.close();
}
} catch (Exception e) {
}
} }
} }
/** /**
...@@ -1254,35 +1172,28 @@ private static void defaultExport(List<Map<String, Object>> list, String fileNam ...@@ -1254,35 +1172,28 @@ private static void defaultExport(List<Map<String, Object>> list, String fileNam
* @throws * @throws
*/ */
public static void getExcel(String url, String fileName, HttpServletResponse response,HttpServletRequest request){ public static void getExcel(String url, String fileName, HttpServletResponse response,HttpServletRequest request){
//通过文件路径获得File对象
try { File file = new File(url);
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data"); response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名 //2.设置文件头:最后一个参数是设置下载文件名
try {
response.setHeader("Content-disposition", "attachment; filename=\"" response.setHeader("Content-disposition", "attachment; filename=\""
+ encodeChineseDownloadFileName(request, fileName+".xls") +"\""); + encodeChineseDownloadFileName(request, fileName+".xls") +"\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try ( FileInputStream in = new FileInputStream(file);
OutputStream out = new BufferedOutputStream(response.getOutputStream());) {
// response.setHeader("Content-Disposition", "attachment;filename=" // response.setHeader("Content-Disposition", "attachment;filename="
// + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xls"); //中文文件名 // + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xls"); //中文文件名
//通过文件路径获得File对象
File file = new File(url);
FileInputStream in = new FileInputStream(file);
//3.通过response获取OutputStream对象(out) //3.通过response获取OutputStream对象(out)
OutputStream out = new BufferedOutputStream(response.getOutputStream());
int b = 0; int b = 0;
byte[] buffer = new byte[2048]; byte[] buffer = new byte[2048];
while ((b=in.read(buffer)) != -1){ while ((b=in.read(buffer)) != -1){
out.write(buffer,0,b); //4.写到输出流(out)中 out.write(buffer,0,b); //4.写到输出流(out)中
} }
in.close();
out.flush(); out.flush();
out.close();
} catch (IOException e) { } catch (IOException e) {
log.error("下载Excel模板异常", e); log.error("下载Excel模板异常", e);
} }
......
...@@ -34,17 +34,12 @@ public class TikaUtils { ...@@ -34,17 +34,12 @@ public class TikaUtils {
public static String fileToTxt(File file) { public static String fileToTxt(File file) {
org.apache.tika.parser.Parser parser = new AutoDetectParser(); org.apache.tika.parser.Parser parser = new AutoDetectParser();
try { try (InputStream inputStream = new FileInputStream(file);) {
InputStream inputStream = new FileInputStream(file);
DefaultHandler handler = new BodyContentHandler(); DefaultHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata(); Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext(); ParseContext parseContext = new ParseContext();
parseContext.set(Parser.class, parser); parseContext.set(Parser.class, parser);
parser.parse(inputStream, handler, metadata, parseContext); parser.parse(inputStream, handler, metadata, parseContext);
/*
* for (String string : metadata.names()) {
* System.out.println(string+":"+metadata.get(string)); }
*/
inputStream.close(); inputStream.close();
return handler.toString(); return handler.toString();
} catch (Exception e) { } catch (Exception e) {
......
...@@ -33,12 +33,14 @@ public class AmosThreadPool { ...@@ -33,12 +33,14 @@ public class AmosThreadPool {
* @return * @return
*/ */
public static AmosThreadPool getInstance() { public static AmosThreadPool getInstance() {
if (instance == null) { if (null != instance) {
synchronized (AmosThreadPool.class) { return instance;
if (instance == null) {
instance = new AmosThreadPool();
} }
synchronized (AmosThreadPool.class) {
if (instance != null) {
return instance;
} }
instance = new AmosThreadPool();
} }
return instance; return instance;
} }
......
...@@ -42,12 +42,14 @@ public class TriggerKeyQueue { ...@@ -42,12 +42,14 @@ public class TriggerKeyQueue {
* @return * @return
*/ */
public static TriggerKeyQueue getInstance() { public static TriggerKeyQueue getInstance() {
if (instance == null) { if (null != instance) {
synchronized (TriggerKeyQueue.class) { return instance;
if (instance == null) {
instance = new TriggerKeyQueue();
} }
synchronized (TriggerKeyQueue.class) {
if (instance != null) {
return instance;
} }
instance = new TriggerKeyQueue();
} }
return instance; return instance;
} }
......
package com.yeejoin.amos.patrol.core.threadpool; package com.yeejoin.amos.patrol.core.threadpool;
import com.yeejoin.amos.patrol.business.constants.XJConstant; import com.yeejoin.amos.patrol.business.constants.XJConstant;
import com.yeejoin.amos.patrol.business.trigger.TriggerKeyQueue;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -33,12 +34,14 @@ public class AmosThreadPool { ...@@ -33,12 +34,14 @@ public class AmosThreadPool {
* @return * @return
*/ */
public static AmosThreadPool getInstance() { public static AmosThreadPool getInstance() {
if (instance == null) { if (null != instance) {
synchronized (AmosThreadPool.class) { return instance;
if (instance == null) {
instance = new AmosThreadPool();
} }
synchronized (AmosThreadPool.class) {
if (instance != null) {
return instance;
} }
instance = new AmosThreadPool();
} }
return instance; return instance;
} }
......
...@@ -16,6 +16,7 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -16,6 +16,7 @@ import org.springframework.web.multipart.MultipartFile;
import org.typroject.tyboot.core.restful.utils.ResponseModel; import org.typroject.tyboot.core.restful.utils.ResponseModel;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -209,14 +210,10 @@ public class FileController { ...@@ -209,14 +210,10 @@ public class FileController {
} else { } else {
processData = DocUtil.processDocx(data); processData = DocUtil.processDocx(data);
} }
String htmlContent = (String) processData.get("html"); String htmlContent = (String) processData.get("html");
try { try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFileName), StandardCharsets.UTF_8));) {
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFileName), "UTF-8"));
writer.write(htmlContent); writer.write(htmlContent);
writer.flush(); writer.flush();
writer.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -42,12 +42,14 @@ public class TriggerKeyQueue { ...@@ -42,12 +42,14 @@ public class TriggerKeyQueue {
* @return * @return
*/ */
public static TriggerKeyQueue getInstance() { public static TriggerKeyQueue getInstance() {
if (instance == null) { if (null != instance) {
synchronized (TriggerKeyQueue.class) { return instance;
if (instance == null) {
instance = new TriggerKeyQueue();
} }
synchronized (TriggerKeyQueue.class) {
if (instance != null) {
return instance;
} }
instance = new TriggerKeyQueue();
} }
return instance; return instance;
} }
......
...@@ -39,6 +39,7 @@ import java.net.URLEncoder; ...@@ -39,6 +39,7 @@ import java.net.URLEncoder;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.Channel; import java.nio.channels.Channel;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
...@@ -63,11 +64,8 @@ public class FileHelper { ...@@ -63,11 +64,8 @@ public class FileHelper {
* @return * @return
*/ */
public static boolean isExcel2003(File file) { public static boolean isExcel2003(File file) {
InputStream is = null; try (InputStream is = new FileInputStream(file);
Workbook wb = null; Workbook wb = WorkbookFactory.create(is);) {
try {
is = new FileInputStream(file);
wb = WorkbookFactory.create(is);
if (wb instanceof XSSFWorkbook) { if (wb instanceof XSSFWorkbook) {
return false; return false;
} else if (wb instanceof HSSFWorkbook) { } else if (wb instanceof HSSFWorkbook) {
...@@ -75,17 +73,6 @@ public class FileHelper { ...@@ -75,17 +73,6 @@ public class FileHelper {
} }
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} finally {
try {
if (null != is) {
is.close();
}
if (null != wb) {
wb.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
return true; return true;
} }
...@@ -96,20 +83,10 @@ public class FileHelper { ...@@ -96,20 +83,10 @@ public class FileHelper {
* @return * @return
*/ */
public static boolean isWord2003(File file) { public static boolean isWord2003(File file) {
InputStream is = null; try (InputStream is = new FileInputStream(file);
try { HWPFDocument hwpfDocument = new HWPFDocument(is);) {
is = new FileInputStream(file);
new HWPFDocument(is);
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
return true; return true;
} }
...@@ -168,34 +145,19 @@ public class FileHelper { ...@@ -168,34 +145,19 @@ public class FileHelper {
*/ */
public static StringBuffer readFile(String path) { public static StringBuffer readFile(String path) {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path); File file = new File(path);
if (file.exists()) { if (file.exists()) {
is = new FileInputStream(file); try (InputStream is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is)); BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String content = br.readLine(); String content = br.readLine();
while (null != content) { while (null != content) {
buffer.append(content); buffer.append(content);
content = br.readLine(); content = br.readLine();
} }
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
return buffer; return buffer;
} }
...@@ -207,34 +169,19 @@ public class FileHelper { ...@@ -207,34 +169,19 @@ public class FileHelper {
*/ */
public static StringBuffer readFile(String path, String split) { public static StringBuffer readFile(String path, String split) {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path); File file = new File(path);
if (file.exists()) { if (file.exists()) {
is = new FileInputStream(file); try (InputStream is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is)); BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String content = br.readLine(); String content = br.readLine();
while (null != content) { while (null != content) {
buffer.append(content).append(split); buffer.append(content).append(split);
content = br.readLine(); content = br.readLine();
} }
}
} catch (Exception exception) { } catch (Exception exception) {
exception.printStackTrace(); exception.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
} }
} catch (Exception exception2) {
exception2.printStackTrace();
} }
}
return buffer; return buffer;
} }
...@@ -244,28 +191,15 @@ public class FileHelper { ...@@ -244,28 +191,15 @@ public class FileHelper {
* @param path 写入内容的文件路径 * @param path 写入内容的文件路径
*/ */
public static void writeFile(String content, String path) { public static void writeFile(String content, String path) {
OutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(path); File file = new File(path);
try (OutputStream fos = new FileOutputStream(file);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
if (!file.getParentFile().exists()) { if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
bw.write(content); bw.write(content);
} catch (FileNotFoundException fnfe) { } catch (IOException fnfe) {
fnfe.printStackTrace(); fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ioException) {
System.err.println(ioException.getMessage());
}
} }
} }
...@@ -354,11 +288,8 @@ public class FileHelper { ...@@ -354,11 +288,8 @@ public class FileHelper {
public static void rmrBlankLines(String inputFile, String outPutFile) throws IOException { public static void rmrBlankLines(String inputFile, String outPutFile) throws IOException {
File htmFile = new File(inputFile); File htmFile = new File(inputFile);
// 以GB2312读取文件 // 以GB2312读取文件
BufferedReader br = null; try (BufferedReader br = new BufferedReader(new FileReader(htmFile));
BufferedWriter bw = null; BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outPutFile)));) {
try {
br = new BufferedReader(new FileReader(htmFile));
bw = new BufferedWriter(new FileWriter(new File(outPutFile)));
String result = null; String result = null;
while (null != (result = br.readLine())) { while (null != (result = br.readLine())) {
if (!"".equals(result.trim())) { if (!"".equals(result.trim())) {
...@@ -367,22 +298,9 @@ public class FileHelper { ...@@ -367,22 +298,9 @@ public class FileHelper {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != bw) {
bw.close();
}
} catch (Exception e) {
} }
} }
}
/** /**
* @param htmFilePath * @param htmFilePath
* @throws IOException * @throws IOException
...@@ -1253,35 +1171,28 @@ private static void defaultExport(List<Map<String, Object>> list, String fileNam ...@@ -1253,35 +1171,28 @@ private static void defaultExport(List<Map<String, Object>> list, String fileNam
* @throws * @throws
*/ */
public static void getExcel(String url, String fileName, HttpServletResponse response,HttpServletRequest request){ public static void getExcel(String url, String fileName, HttpServletResponse response,HttpServletRequest request){
//通过文件路径获得File对象
try { File file = new File(url);
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data"); response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名 //2.设置文件头:最后一个参数是设置下载文件名
try {
response.setHeader("Content-disposition", "attachment; filename=\"" response.setHeader("Content-disposition", "attachment; filename=\""
+ encodeChineseDownloadFileName(request, fileName+".xls") +"\""); + encodeChineseDownloadFileName(request, fileName+".xls") +"\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try ( FileInputStream in = new FileInputStream(file);
OutputStream out = new BufferedOutputStream(response.getOutputStream());) {
// response.setHeader("Content-Disposition", "attachment;filename=" // response.setHeader("Content-Disposition", "attachment;filename="
// + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xls"); //中文文件名 // + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xls"); //中文文件名
//通过文件路径获得File对象
File file = new File(url);
FileInputStream in = new FileInputStream(file);
//3.通过response获取OutputStream对象(out) //3.通过response获取OutputStream对象(out)
OutputStream out = new BufferedOutputStream(response.getOutputStream());
int b = 0; int b = 0;
byte[] buffer = new byte[2048]; byte[] buffer = new byte[2048];
while ((b=in.read(buffer)) != -1){ while ((b=in.read(buffer)) != -1){
out.write(buffer,0,b); //4.写到输出流(out)中 out.write(buffer,0,b); //4.写到输出流(out)中
} }
in.close();
out.flush(); out.flush();
out.close();
} catch (IOException e) { } catch (IOException e) {
log.error("下载Excel模板异常", e); log.error("下载Excel模板异常", e);
} }
......
...@@ -34,17 +34,12 @@ public class TikaUtils { ...@@ -34,17 +34,12 @@ public class TikaUtils {
public static String fileToTxt(File file) { public static String fileToTxt(File file) {
org.apache.tika.parser.Parser parser = new AutoDetectParser(); org.apache.tika.parser.Parser parser = new AutoDetectParser();
try { try(InputStream inputStream = new FileInputStream(file);) {
InputStream inputStream = new FileInputStream(file);
DefaultHandler handler = new BodyContentHandler(); DefaultHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata(); Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext(); ParseContext parseContext = new ParseContext();
parseContext.set(Parser.class, parser); parseContext.set(Parser.class, parser);
parser.parse(inputStream, handler, metadata, parseContext); parser.parse(inputStream, handler, metadata, parseContext);
/*
* for (String string : metadata.names()) {
* System.out.println(string+":"+metadata.get(string)); }
*/
inputStream.close(); inputStream.close();
return handler.toString(); return handler.toString();
} catch (Exception e) { } catch (Exception e) {
...@@ -58,21 +53,14 @@ public class TikaUtils { ...@@ -58,21 +53,14 @@ public class TikaUtils {
ContentHandler handler = new ToHTMLContentHandler(); ContentHandler handler = new ToHTMLContentHandler();
AutoDetectParser parser = new AutoDetectParser(); AutoDetectParser parser = new AutoDetectParser();
Metadata metadata = new Metadata(); Metadata metadata = new Metadata();
InputStream stream = null; try ( InputStream stream = new FileInputStream(new File(fileName));) {
try {
stream = new FileInputStream(new File(fileName));
parser.parse(stream, handler, metadata); parser.parse(stream, handler, metadata);
FileHelper.writeFile(handler.toString(), outPutFile + ".html"); FileHelper.writeFile(handler.toString(), outPutFile + ".html");
FileHelper.parse(outPutFile + ".html"); FileHelper.parse(outPutFile + ".html");
return null; return null;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
} }
return null; return null;
} }
......
...@@ -33,12 +33,14 @@ public class AmosThreadPool { ...@@ -33,12 +33,14 @@ public class AmosThreadPool {
* @return * @return
*/ */
public static AmosThreadPool getInstance() { public static AmosThreadPool getInstance() {
if (instance == null) { if (null != instance) {
synchronized (AmosThreadPool.class) { return instance;
if (instance == null) {
instance = new AmosThreadPool();
} }
synchronized (AmosThreadPool.class) {
if (instance != null) {
return instance;
} }
instance = new AmosThreadPool();
} }
return instance; return instance;
} }
......
...@@ -193,19 +193,12 @@ public class WordPowerUtils { ...@@ -193,19 +193,12 @@ public class WordPowerUtils {
if (!file.exists()) { if (!file.exists()) {
return ""; return "";
} }
InputStream in = null;
byte[] data = null; byte[] data = null;
try { try (InputStream in = new FileInputStream(file)) {
in = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
data = new byte[in.available()]; data = new byte[in.available()];
in.read(data); in.read(data);
in.close(); } catch (IOException e1) {
} catch (IOException e) { e1.printStackTrace();
e.printStackTrace();
} }
BASE64Encoder encoder = new BASE64Encoder(); BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data); return encoder.encode(data);
......
...@@ -334,15 +334,13 @@ public class CheckResultImpl extends ServiceImpl<CheckResultMapper, CheckResult> ...@@ -334,15 +334,13 @@ public class CheckResultImpl extends ServiceImpl<CheckResultMapper, CheckResult>
* @return * @return
*/ */
private static byte[] file2byte(File file) { private static byte[] file2byte(File file) {
try { try (FileInputStream in = new FileInputStream(file);) {
FileInputStream in = new FileInputStream(file);
byte[] data = new byte[in.available()]; byte[] data = new byte[in.available()];
in.read(data); in.read(data);
in.close();
return data; return data;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return new byte[0];
} }
} }
......
...@@ -32,12 +32,14 @@ public class AmosThreadPool { ...@@ -32,12 +32,14 @@ public class AmosThreadPool {
* @return * @return
*/ */
public static AmosThreadPool getInstance() { public static AmosThreadPool getInstance() {
if (instance == null) { if (null != instance) {
synchronized (AmosThreadPool.class) { return instance;
if (instance == null) {
instance = new AmosThreadPool();
} }
synchronized (AmosThreadPool.class) {
if (instance != null) {
return instance;
} }
instance = new AmosThreadPool();
} }
return instance; return instance;
} }
......
...@@ -22,6 +22,8 @@ import java.util.concurrent.BlockingQueue; ...@@ -22,6 +22,8 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import static com.alibaba.fastjson.JSON.parseArray;
@Slf4j @Slf4j
@Component @Component
public class EmqMessageService extends EmqxListener { public class EmqMessageService extends EmqxListener {
...@@ -55,7 +57,7 @@ public class EmqMessageService extends EmqxListener { ...@@ -55,7 +57,7 @@ public class EmqMessageService extends EmqxListener {
e.printStackTrace(); e.printStackTrace();
} }
list = com.alibaba.fastjson.JSONObject.parseArray(json, Map.class); list = parseArray(json, Map.class);
String[] split = topics.split(","); String[] split = topics.split(",");
Arrays.stream(split).forEach(e-> { Arrays.stream(split).forEach(e-> {
...@@ -78,7 +80,11 @@ public class EmqMessageService extends EmqxListener { ...@@ -78,7 +80,11 @@ public class EmqMessageService extends EmqxListener {
Runnable task_runnable = new Runnable() { Runnable task_runnable = new Runnable() {
public void run() { public void run() {
while (true) { int k = 0;
boolean b = true;
while (b) {
k++;
b = k < Integer.MAX_VALUE;
try { try {
JSONObject messageResult = blockingQueue.take(); JSONObject messageResult = blockingQueue.take();
JSONObject result = messageResult.getJSONObject("result"); JSONObject result = messageResult.getJSONObject("result");
...@@ -89,6 +95,7 @@ public class EmqMessageService extends EmqxListener { ...@@ -89,6 +95,7 @@ public class EmqMessageService extends EmqxListener {
} }
}); });
} catch (Exception e) { } catch (Exception e) {
Thread.currentThread().interrupt();
log.info("发送kafka消息失败 ====> message: {}", e.getMessage()); log.info("发送kafka消息失败 ====> message: {}", e.getMessage());
} }
} }
......
...@@ -66,7 +66,11 @@ public class AppSpeechTranscriber { ...@@ -66,7 +66,11 @@ public class AppSpeechTranscriber {
DatagramPacket datagramPacket = new DatagramPacket(b, b.length); DatagramPacket datagramPacket = new DatagramPacket(b, b.length);
logger.warn("serverSocket已启动,地址:" + localIpAddress logger.warn("serverSocket已启动,地址:" + localIpAddress
+ "监听端口:" + serverSocket.getLocalPort() + " 等待语音融合系统推送数据..."); + "监听端口:" + serverSocket.getLocalPort() + " 等待语音融合系统推送数据...");
while (true) { int k = 0;
boolean b1 = true;
while (b1) {
k++;
b1 = k < Integer.MAX_VALUE;
serverSocket.receive(datagramPacket); serverSocket.receive(datagramPacket);
if (transcriber == null) { if (transcriber == null) {
logger.warn("收到第一个数据包:" + b.length + " 开始进行语音翻译"); logger.warn("收到第一个数据包:" + b.length + " 开始进行语音翻译");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment