Commit a6c9b89f authored by zhangsen's avatar zhangsen

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

parent b1af2f42
......@@ -54,14 +54,11 @@ public class WordConverterUtils {
FileItem item = factory.createItem("textField", "text/plain", true, file.getName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
try {
FileInputStream fis = new FileInputStream(file);
OutputStream os = item.getOutputStream();
try (FileInputStream fis = new FileInputStream(file);
OutputStream os = item.getOutputStream();) {
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -10,33 +10,14 @@ public class ImageText implements AbstractText {
public void createTxt(String inputFile, String outputFile) throws Exception {
File file = new File(inputFile);
String baseName = FilenameUtils.getBaseName(inputFile);
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(file);
os = new FileOutputStream(
outputFile.substring(0, outputFile.length() - baseName.length() - 1) + ".txt");
try (InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(outputFile.substring(0, outputFile.length() - baseName.length() - 1) + ".txt")) {
byte[] buffer = new byte[1024];
int result = -1;
while (-1 != (result = is.read(buffer))) {
os.write(buffer, 0, result);
}
} finally {
try {
if (null != is) {
is.close();
}
if (null != os) {
os.close();
}
} catch (Exception exce) {
System.err.println(exce.getMessage());
}
}
}
}
......@@ -18,6 +18,7 @@ import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.TreeSet;
......@@ -39,11 +40,8 @@ public class FileHelper {
* @return
*/
public static boolean isExcel2003(File file) {
InputStream is = null;
Workbook wb = null;
try {
is = new FileInputStream(file);
wb = WorkbookFactory.create(is);
try (InputStream is = new FileInputStream(file);
Workbook wb = WorkbookFactory.create(is);) {
if (wb instanceof XSSFWorkbook) {
return false;
} else if (wb instanceof HSSFWorkbook) {
......@@ -51,17 +49,6 @@ public class FileHelper {
}
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
if (null != wb) {
wb.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -71,39 +58,19 @@ public class FileHelper {
* @return
*/
public static boolean isWord2003(File file) {
InputStream is = null;
try {
is = new FileInputStream(file);
new HWPFDocument(is);
try (InputStream is = new FileInputStream(file);
HWPFDocument hwpfDocument = new HWPFDocument(is);) {
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
public static boolean isWord2007(File file) {
InputStream is = null;
try {
is = new FileInputStream(file);
new XWPFDocument(is).close();
try (InputStream is = new FileInputStream(file);
XWPFDocument xwpfDocument = new XWPFDocument(is)) {
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -113,24 +80,10 @@ public class FileHelper {
* @return
*/
public static boolean isPpt2003(File file) {
InputStream is = null;
HSLFSlideShow ppt = null;
try {
is = new FileInputStream(file);
ppt = new HSLFSlideShow(is);
try (InputStream is = new FileInputStream(file);
HSLFSlideShow ppt = new HSLFSlideShow(is)) {
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
if (null != ppt) {
ppt.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -141,13 +94,10 @@ public class FileHelper {
*/
public static StringBuffer readFile(String path) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content);
......@@ -156,19 +106,7 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return buffer;
}
......@@ -181,13 +119,10 @@ public class FileHelper {
*/
public static StringBuffer readFile(String path, String split) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content).append(split);
......@@ -196,19 +131,7 @@ public class FileHelper {
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception exception2) {
exception2.printStackTrace();
}
}
return buffer;
}
......@@ -219,28 +142,15 @@ public class FileHelper {
* @param 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()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
} catch (IOException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ioException) {
System.err.println(ioException.getMessage());
}
}
}
......@@ -330,11 +240,8 @@ public class FileHelper {
public static void rmrBlankLines(String inputFile, String outPutFile) throws IOException {
File htmFile = new File(inputFile);
// 以GB2312读取文件
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(htmFile));
bw = new BufferedWriter(new FileWriter(new File(outPutFile)));
try (BufferedReader br = new BufferedReader(new FileReader(htmFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outPutFile)));) {
String result = null;
while (null != (result = br.readLine())) {
if (!"".equals(result.trim())) {
......@@ -343,20 +250,7 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != bw) {
bw.close();
}
} catch (Exception e) {
}
}
}
/**
......
......@@ -34,18 +34,12 @@ public class TikaUtils {
public static String fileToTxt(File file) {
Parser parser = new AutoDetectParser();
try {
InputStream inputStream = new FileInputStream(file);
try (InputStream inputStream = new FileInputStream(file);) {
DefaultHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext();
parseContext.set(Parser.class, parser);
parser.parse(inputStream, handler, metadata, parseContext);
/*
* for (String string : metadata.names()) {
* System.out.println(string+":"+metadata.get(string)); }
*/
inputStream.close();
return handler.toString();
} catch (Exception e) {
e.printStackTrace();
......@@ -58,21 +52,13 @@ public class TikaUtils {
ContentHandler handler = new ToHTMLContentHandler();
AutoDetectParser parser = new AutoDetectParser();
Metadata metadata = new Metadata();
InputStream stream = null;
try {
stream = new FileInputStream(new File(fileName));
try (InputStream stream = new FileInputStream(new File(fileName));) {
parser.parse(stream, handler, metadata);
FileHelper.writeFile(handler.toString(), outPutFile + ".html");
FileHelper.parse(outPutFile + ".html");
return null;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return null;
}
......
package com.yeejoin.precontrol.common.service.impl;
import com.yeejoin.precontrol.common.service.PdfService;
import com.yeejoin.precontrol.common.utils.ExtendedIOUtils;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
......@@ -17,22 +16,16 @@ public class PdfServiceImpl implements PdfService {
@Override
public void base64StringToPdf(String base64Content, String filePath) {
BASE64Decoder decoder = new BASE64Decoder();
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
byte[] bytes = decoder.decodeBuffer(base64Content);
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
bis = new BufferedInputStream(byteInputStream);
File file = new File(filePath);
File file = new File(filePath);
try (ByteArrayInputStream byteInputStream = new ByteArrayInputStream(decoder.decodeBuffer(base64Content));
BufferedInputStream bis = new BufferedInputStream(byteInputStream);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
) {
File path = file.getParentFile();
if (!path.exists()) {
path.mkdirs();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int length = bis.read(buffer);
while (length != -1) {
......@@ -42,26 +35,16 @@ public class PdfServiceImpl implements PdfService {
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
ExtendedIOUtils.closeQuietly(bis);
ExtendedIOUtils.closeQuietly(fos);
ExtendedIOUtils.closeQuietly(bos);
}
}
@Override
public String pdfToBase64(File file) {
BASE64Encoder encoder = new BASE64Encoder();
FileInputStream fin = null;
BufferedInputStream bin = null;
ByteArrayOutputStream baos = null;
BufferedOutputStream bout = null;
try {
fin = new FileInputStream(file);
bin = new BufferedInputStream(fin);
baos = new ByteArrayOutputStream();
bout = new BufferedOutputStream(baos);
try (FileInputStream fin = new FileInputStream(file);
BufferedInputStream bin = new BufferedInputStream(fin);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bout = new BufferedOutputStream(baos);) {
byte[] buffer = new byte[1024];
int len = bin.read(buffer);
while (len != -1) {
......@@ -72,18 +55,8 @@ public class PdfServiceImpl implements PdfService {
bout.flush();
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fin.close();
bin.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
......
......@@ -20,6 +20,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -837,9 +838,8 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> impl
public static List<String> getContent(String filePath) {
// 读取文件
List<String> lineLists = null;
try {
lineLists = Files.lines(Paths.get(filePath), Charset.defaultCharset())
.flatMap(line -> Arrays.stream(line.split("\n"))).collect(Collectors.toList());
try (Stream<String> lines = Files.lines(Paths.get(filePath), Charset.defaultCharset());) {
lineLists = lines.flatMap(line -> Arrays.stream(line.split("\n"))).collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -32,6 +32,7 @@ import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
......@@ -52,11 +53,8 @@ public class FileHelper {
* @return
*/
public static boolean isExcel2003(File file) {
InputStream is = null;
Workbook wb = null;
try {
is = new FileInputStream(file);
wb = WorkbookFactory.create(is);
try (InputStream is = new FileInputStream(file);
Workbook wb = WorkbookFactory.create(is);) {
if (wb instanceof XSSFWorkbook) {
return false;
} else if (wb instanceof HSSFWorkbook) {
......@@ -64,17 +62,6 @@ public class FileHelper {
}
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
if (null != wb) {
wb.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -84,20 +71,10 @@ public class FileHelper {
* @return
*/
public static boolean isWord2003(File file) {
InputStream is = null;
try {
is = new FileInputStream(file);
new HWPFDocument(is);
try (InputStream is = new FileInputStream(file);
HWPFDocument hwpfDocument = new HWPFDocument(is);) {
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -126,24 +103,10 @@ public class FileHelper {
* @return
*/
public static boolean isPPT2003(File file) {
InputStream is = null;
HSLFSlideShow ppt = null;
try {
is = new FileInputStream(file);
ppt = new HSLFSlideShow(is);
try (InputStream is = new FileInputStream(file);
HSLFSlideShow ppt = new HSLFSlideShow(is);) {
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
if (null != ppt) {
ppt.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -154,13 +117,10 @@ public class FileHelper {
*/
public static StringBuffer readLocalFile(String path) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is)); ) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content);
......@@ -169,17 +129,6 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return buffer;
}
......@@ -193,13 +142,10 @@ public class FileHelper {
*/
public static StringBuffer readFile(String path, String split) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content).append(split);
......@@ -208,17 +154,6 @@ public class FileHelper {
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception exception2) {
exception2.printStackTrace();
}
}
return buffer;
}
......@@ -230,28 +165,17 @@ public class FileHelper {
* @param 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()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ioException) {
System.err.println(ioException.getMessage());
}
}
}
......@@ -335,11 +259,8 @@ public class FileHelper {
public static void rmrBlankLines(String inputFile, String outPutFile) throws IOException {
File htmFile = new File(inputFile);
// 以GB2312读取文件
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(htmFile));
bw = new BufferedWriter(new FileWriter(new File(outPutFile)));
try (BufferedReader br = new BufferedReader(new FileReader(htmFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outPutFile)));) {
String result = null;
while (null != (result = br.readLine())) {
if (!"".equals(result.trim())) {
......@@ -348,16 +269,6 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != bw) {
bw.close();
}
} catch (Exception e) {
}
}
}
......@@ -1095,13 +1006,10 @@ public class FileHelper {
*/
public static StringBuffer readFile(String path) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content);
......@@ -1110,19 +1018,7 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return buffer;
}
}
......@@ -26,14 +26,16 @@ public class FileUtil {
* @return
*/
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
try (FileOutputStream out = new FileOutputStream(filePath + fileName);) {
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
out.write(file);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream out = new FileOutputStream(filePath + fileName);
out.write(file);
out.flush();
out.close();
}
/**
......
......@@ -25,29 +25,21 @@ public class HttpRequest {
conn.setConnectTimeout(30 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
try (FileOutputStream fos = new FileOutputStream(file);
//得到输入流
InputStream inputStream = conn.getInputStream();) {
//获取自己数组
byte[] getData = readInputStream(inputStream);
fos.write(getData);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("info:" + url + " download success");
return file;
}
......
......@@ -164,43 +164,42 @@ public class HttpUtils {
NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
Assert.hasText(url, "url invalid");
String result;
CloseableHttpClient httpClient;
if (url.startsWith(HTTPS)) {
// 创建一个SSL信任所有证书的httpClient对象
httpClient = HttpUtils.createSslInsecureClient();
} else {
httpClient = HttpClients.createDefault();
}
CloseableHttpResponse response = null;
HttpPost httpPost = getHttpPost(url);
if (GlobalCache.header != null) {
for (String key : GlobalCache.header.keySet()) {
String value = GlobalCache.header.get(key);
httpPost.setHeader(key, value);
// 创建一个SSL信任所有证书的httpClient对象
try ( CloseableHttpClient httpClient = url.startsWith(HTTPS) ? HttpUtils.createSslInsecureClient() : HttpClients.createDefault();) {
CloseableHttpResponse response = null;
HttpPost httpPost = getHttpPost(url);
if (GlobalCache.header != null) {
for (String key : GlobalCache.header.keySet()) {
String value = GlobalCache.header.get(key);
httpPost.setHeader(key, value);
}
}
}
//加入全局请求令牌权限
httpPost.setHeader("Http-Authorization", GlobalCache.paramMap.get("token"));
if (GlobalCache.header.get("Content-Type") != null) {
String contentType = GlobalCache.header.get("Content-Type");
if ("application/x-www-form-urlencoded".equals(contentType)) {
JSONObject jsonObject = JSONObject.parseObject(jsonParams);
List<NameValuePair> params = new ArrayList<>();
//循环json key value 仅能解决正常对象 若Json对象中嵌套数组 则可能需要单独处理
if (jsonObject != null) {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
//加入全局请求令牌权限
httpPost.setHeader("Http-Authorization", GlobalCache.paramMap.get("token"));
if (GlobalCache.header.get("Content-Type") != null) {
String contentType = GlobalCache.header.get("Content-Type");
if ("application/x-www-form-urlencoded".equals(contentType)) {
JSONObject jsonObject = JSONObject.parseObject(jsonParams);
List<NameValuePair> params = new ArrayList<>();
//循环json key value 仅能解决正常对象 若Json对象中嵌套数组 则可能需要单独处理
if (jsonObject != null) {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
httpPost.setEntity(new UrlEncodedFormEntity(params, DEFAULT_ENCODING));
}
httpPost.setEntity(new UrlEncodedFormEntity(params, DEFAULT_ENCODING));
}
}
if ("application/json;charset=UTF-8".equals(contentType)) {
if ("application/json;charset=UTF-8".equals(contentType)) {
httpPost.setEntity(new StringEntity(jsonParams, ContentType.create("application/json", DEFAULT_ENCODING)));
}
} else {
httpPost.setEntity(new StringEntity(jsonParams, ContentType.create("application/json", DEFAULT_ENCODING)));
}
} else {
httpPost.setEntity(new StringEntity(jsonParams, ContentType.create("application/json", DEFAULT_ENCODING)));
return baseRequest(httpClient, httpPost);
} catch (IOException e) {
e.printStackTrace();
}
return baseRequest(httpClient, httpPost);
return new ResponeVo();
}
/**
......@@ -239,44 +238,43 @@ public class HttpUtils {
public static ResponeVo put(String url, String jsonParams) throws IOException, NoSuchAlgorithmException,
KeyStoreException, KeyManagementException {
log.info("----->调用请求 url:" + url + " ---->json参数:" + jsonParams);
CloseableHttpClient httpClient = null;
String content;
if (url.startsWith(HTTPS)) {
// 创建一个SSL信任所有证书的httpClient对象
httpClient = HttpUtils.createSslInsecureClient();
} else {
httpClient = HttpClients.createDefault();
}
CloseableHttpResponse response = null;
HttpPut httpPut = new HttpPut(url);
if (GlobalCache.header != null) {
for (String key : GlobalCache.header.keySet()) {
String value = GlobalCache.header.get(key);
httpPut.setHeader(key, value);
// 创建一个SSL信任所有证书的httpClient对象
try ( CloseableHttpClient httpClient = url.startsWith(HTTPS) ? HttpUtils.createSslInsecureClient() : HttpClients.createDefault();) {
CloseableHttpResponse response = null;
HttpPut httpPut = new HttpPut(url);
if (GlobalCache.header != null) {
for (String key : GlobalCache.header.keySet()) {
String value = GlobalCache.header.get(key);
httpPut.setHeader(key, value);
}
}
}
//加入全局请求令牌权限
httpPut.setHeader("Http-Authorization", GlobalCache.paramMap.get("token"));
if (GlobalCache.header.get("Content-Type") != null) {
String contentType = GlobalCache.header.get("Content-Type");
if ("application/x-www-form-urlencoded".equals(contentType)) {
JSONObject jsonObject = JSONObject.parseObject(jsonParams);
List<NameValuePair> params = new ArrayList<>();
//循环json key value 仅能解决正常对象 若Json对象中嵌套数组 则可能需要单独处理
if (jsonObject != null) {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
//加入全局请求令牌权限
httpPut.setHeader("Http-Authorization", GlobalCache.paramMap.get("token"));
if (GlobalCache.header.get("Content-Type") != null) {
String contentType = GlobalCache.header.get("Content-Type");
if ("application/x-www-form-urlencoded".equals(contentType)) {
JSONObject jsonObject = JSONObject.parseObject(jsonParams);
List<NameValuePair> params = new ArrayList<>();
//循环json key value 仅能解决正常对象 若Json对象中嵌套数组 则可能需要单独处理
if (jsonObject != null) {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
httpPut.setEntity(new UrlEncodedFormEntity(params, DEFAULT_ENCODING));
}
httpPut.setEntity(new UrlEncodedFormEntity(params, DEFAULT_ENCODING));
}
if ("application/json;charset=UTF-8".equals(contentType)) {
httpPut.setEntity(new StringEntity(jsonParams, ContentType.create("application/json", DEFAULT_ENCODING)));
}
} else {
log.error("请求头为空");
}
if ("application/json;charset=UTF-8".equals(contentType)) {
httpPut.setEntity(new StringEntity(jsonParams, ContentType.create("application/json", DEFAULT_ENCODING)));
}
} else {
log.error("请求头为空");
return baseRequest(httpClient, httpPut);
} catch (IOException e) {
e.printStackTrace();
}
return baseRequest(httpClient, httpPut);
return new ResponeVo();
}
/**
......@@ -434,16 +432,12 @@ public class HttpUtils {
}
public static void inputStreamToFile(InputStream ins, File file) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
try (OutputStream os = new FileOutputStream(file);) {
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -33,18 +33,12 @@ public class TikaUtils {
public static String fileToTxt(File file) {
Parser parser = new AutoDetectParser();
try {
InputStream inputStream = new FileInputStream(file);
try (InputStream inputStream = new FileInputStream(file);) {
DefaultHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext();
parseContext.set(Parser.class, parser);
parser.parse(inputStream, handler, metadata, parseContext);
/*
* for (String string : metadata.names()) {
* System.out.println(string+":"+metadata.get(string)); }
*/
inputStream.close();
return handler.toString();
} catch (Exception e) {
e.printStackTrace();
......@@ -57,16 +51,12 @@ public class TikaUtils {
ContentHandler handler = new ToHTMLContentHandler();
AutoDetectParser parser = new AutoDetectParser();
Metadata metadata = new Metadata();
InputStream stream = null;
try {
stream = new FileInputStream(new File(fileName));
try (InputStream stream = new FileInputStream(new File(fileName))) {
parser.parse(stream, handler, metadata);
FileHelper.writeFile(handler.toString(), outPutFile + ".html");
FileHelper.parse(outPutFile + ".html");
return null;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return null;
}
......
......@@ -71,6 +71,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
......@@ -1132,21 +1133,18 @@ public class CommandController extends BaseController {
}
File htmlFile = new File(htmlFileName);
WordConverterUtils.wordToHtml(fileName, htmlFileName, imagePathStr, readUrl, remoteSecurityService, product, appKey, token);
FileInputStream fis = new FileInputStream(htmlFile);
// response.setContentType("multipart/form-data");
// response.setCharacterEncoding("UTF-8");
// response.setContentType("text/html");
ServletOutputStream out;
out = response.getOutputStream();
int b = 0;
byte[] buffer = new byte[1024];
while ((b = fis.read(buffer)) != -1) {
// 4.写到输出流(out)中
out.write(buffer, 0, b);
try (FileInputStream fis = new FileInputStream(htmlFile);
ServletOutputStream out = response.getOutputStream();) {
int b = 0;
byte[] buffer = new byte[1024];
while ((b = fis.read(buffer)) != -1) {
// 4.写到输出流(out)中
out.write(buffer, 0, b);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
fis.close();
out.flush();
out.close();
return ResponseHelper.buildResponse("");
} else {
return null;
......
......@@ -7,10 +7,12 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -150,9 +152,9 @@ public class FileController extends AbstractBaseController {
@TycloudOperation(ApiLevel = UserType.AGENCY)
@RequestMapping(value = "/download/**", method = RequestMethod.GET)
public void download(HttpServletResponse response, HttpServletRequest request) throws Exception {
try {
String path = request.getServletPath().substring(15);
IOUtils.copy(new FileInputStream(fileUploadDir + path), response.getOutputStream());
try (FileInputStream inputStream = new FileInputStream(fileUploadDir + request.getServletPath().substring(15));
ServletOutputStream outputStream = response.getOutputStream();) {
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
e.printStackTrace();
ResponseUtils.renderText(response, "File not exists!");
......@@ -253,12 +255,9 @@ public class FileController extends AbstractBaseController {
}
String htmlContent = (String) processData.get("html");
try {
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFileName), "UTF-8"));
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFileName), StandardCharsets.UTF_8));) {
writer.write(htmlContent);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -51,8 +51,6 @@ public class PlanVisual3dController extends AbstractBaseController {
@Autowired
HttpServletResponse response;
@Autowired
HttpServletRequest request;
@TycloudOperation(ApiLevel = UserType.AGENCY)
@ApiOperation(value = "预案应用树", notes = "预案应用树")
......@@ -76,7 +74,7 @@ public class PlanVisual3dController extends AbstractBaseController {
if (testPlan != null) {
String path = testPlan.getFilePath();
if (path != null && !"".equals(path)) {
try {
try (InputStream fis = new BufferedInputStream(new FileInputStream(fileUploadDir + path));) {
// path是指欲下载的文件的路径。
File file = new File(fileUploadDir + path);
if (file.exists()) {
......@@ -84,12 +82,9 @@ public class PlanVisual3dController extends AbstractBaseController {
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(fileUploadDir + path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
// response.reset();
// 设置response的Header
......
......@@ -83,11 +83,8 @@ public class FileHelper {
* @return
*/
public static boolean isExcel2003(File file) {
InputStream is = null;
Workbook wb = null;
try {
is = new FileInputStream(file);
wb = WorkbookFactory.create(is);
try (InputStream is = new FileInputStream(file);
Workbook wb = WorkbookFactory.create(is);) {
if (wb instanceof XSSFWorkbook) {
return false;
} else if (wb instanceof HSSFWorkbook) {
......@@ -95,17 +92,6 @@ public class FileHelper {
}
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
if (null != wb) {
wb.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -187,13 +173,10 @@ public class FileHelper {
*/
public static StringBuffer readFile(String path) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content);
......@@ -202,19 +185,7 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return buffer;
}
......@@ -227,13 +198,10 @@ public class FileHelper {
*/
public static StringBuffer readFile(String path, String split) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content).append(split);
......@@ -242,19 +210,7 @@ public class FileHelper {
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception exception2) {
exception2.printStackTrace();
}
}
return buffer;
}
......@@ -265,28 +221,15 @@ public class FileHelper {
* @param 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()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
} catch (IOException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ioException) {
System.err.println(ioException.getMessage());
}
}
}
......@@ -375,11 +318,8 @@ public class FileHelper {
public static void rmrBlankLines(String inputFile, String outPutFile) throws IOException {
File htmFile = new File(inputFile);
// 以GB2312读取文件
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(htmFile));
bw = new BufferedWriter(new FileWriter(new File(outPutFile)));
try (BufferedReader br = new BufferedReader(new FileReader(htmFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outPutFile)));) {
String result = null;
while (null != (result = br.readLine())) {
if (!"".equals(result.trim())) {
......@@ -388,20 +328,7 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != bw) {
bw.close();
}
} catch (Exception e) {
}
}
}
/**
......@@ -1192,34 +1119,25 @@ public class FileHelper {
*/
public static void getExcel(String url, String fileName, HttpServletResponse response, HttpServletRequest request) {
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名
try {
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-disposition", "attachment; filename=\""
+ encodeChineseDownloadFileName(request, fileName + ".xls") + "\"");
// response.setHeader("Content-Disposition", "attachment;filename="
// + 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)
OutputStream out = new BufferedOutputStream(response.getOutputStream());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try (//通过文件路径获得File对象
FileInputStream in = new FileInputStream(new File(url));
//3.通过response获取OutputStream对象(out)
OutputStream out = new BufferedOutputStream(response.getOutputStream());) {
int b = 0;
byte[] buffer = new byte[2048];
while ((b = in.read(buffer)) != -1) {
out.write(buffer, 0, b); //4.写到输出流(out)中
}
in.close();
out.flush();
out.close();
} catch (IOException e) {
log.error("下载Excel模板异常", e);
}
......
......@@ -26,10 +26,12 @@ public class FileUtil {
if (!targetFile.exists()) {
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath + fileName);
out.write(file);
out.flush();
out.close();
try (FileOutputStream out = new FileOutputStream(filePath + fileName);) {
out.write(file);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
......
......@@ -60,6 +60,7 @@ import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
......@@ -121,20 +122,10 @@ public class FileHelper {
* @return
*/
public static boolean isWord2003(File file) {
InputStream is = null;
try {
is = new FileInputStream(file);
new HWPFDocument(is);
try (InputStream is = new FileInputStream(file);
HWPFDocument hwpfDocument = new HWPFDocument(is);) {
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -164,24 +155,10 @@ public class FileHelper {
* @return
*/
public static boolean isPPT2003(File file) {
InputStream is = null;
HSLFSlideShow ppt = null;
try {
is = new FileInputStream(file);
ppt = new HSLFSlideShow(is);
try (InputStream is = new FileInputStream(file);
HSLFSlideShow ppt = new HSLFSlideShow(is);) {
} catch (Exception e) {
return false;
} finally {
try {
if (null != is) {
is.close();
}
if (null != ppt) {
ppt.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
......@@ -193,13 +170,10 @@ public class FileHelper {
*/
public static StringBuffer readFile(String path) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content);
......@@ -208,19 +182,7 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return buffer;
}
......@@ -232,13 +194,10 @@ public class FileHelper {
*/
public static StringBuffer readFile(String path, String split) {
StringBuffer buffer = new StringBuffer();
InputStream is = null;
BufferedReader br = null;
try {
File file = new File(path);
File file = new File(path);
try (InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
if (file.exists()) {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while (null != content) {
buffer.append(content).append(split);
......@@ -247,19 +206,7 @@ public class FileHelper {
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != br) {
br.close();
}
} catch (Exception exception2) {
exception2.printStackTrace();
}
}
return buffer;
}
......@@ -269,28 +216,17 @@ public class FileHelper {
* @param 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()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ioException) {
System.err.println(ioException.getMessage());
}
}
}
......@@ -379,11 +315,8 @@ public class FileHelper {
public static void rmrBlankLines(String inputFile, String outPutFile) throws IOException {
File htmFile = new File(inputFile);
// 以GB2312读取文件
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(htmFile));
bw = new BufferedWriter(new FileWriter(new File(outPutFile)));
try (BufferedReader br = new BufferedReader(new FileReader(htmFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outPutFile)));) {
String result = null;
while (null != (result = br.readLine())) {
if (!"".equals(result.trim())) {
......@@ -392,17 +325,6 @@ public class FileHelper {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != bw) {
bw.close();
}
} catch (Exception e) {
}
}
......@@ -1198,35 +1120,24 @@ private static void defaultExport(List<Map<String, Object>> list, String fileNam
* @throws
*/
public static void getExcel(String url, String fileName, HttpServletResponse response,HttpServletRequest request){
try {
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-disposition", "attachment; filename=\""
+ encodeChineseDownloadFileName(request, fileName+".xls") +"\"");
// response.setHeader("Content-Disposition", "attachment;filename="
// + 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)
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名
try {
response.setHeader("Content-disposition", "attachment; filename=\""
+ encodeChineseDownloadFileName(request, fileName+".xls") +"\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try (FileInputStream in = new FileInputStream(new File(url));
//3.通过response获取OutputStream对象(out)
OutputStream out = new BufferedOutputStream(response.getOutputStream());) {
int b = 0;
byte[] buffer = new byte[2048];
while ((b=in.read(buffer)) != -1){
out.write(buffer,0,b); //4.写到输出流(out)中
}
in.close();
out.flush();
out.close();
} catch (IOException e) {
log.error("下载Excel模板异常", e);
}
......
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