Commit d5e31365 authored by tangwei's avatar tangwei

修改扫描错误

parent 69447b9e
...@@ -28,11 +28,14 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -28,11 +28,14 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder; 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.nio.charset.StandardCharsets;
import java.nio.file.*;
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;
...@@ -59,12 +62,9 @@ public class FileHelper { ...@@ -59,12 +62,9 @@ public class FileHelper {
public static boolean isWord2003(File file) { public static boolean isWord2003(File file) {
try ( try (
InputStream is = new FileInputStream(file); InputStream is = new FileInputStream(file);
HWPFDocument hwpfDocument = new HWPFDocument(is)) )
{ {
HWPFDocument hwpfDocument = new HWPFDocument(is);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
...@@ -73,20 +73,11 @@ public class FileHelper { ...@@ -73,20 +73,11 @@ public class FileHelper {
} }
public static boolean isWord2007(File file) { public static boolean isWord2007(File file) {
InputStream is = null; try (InputStream is = new FileInputStream(file);
try { ){
is = new FileInputStream(file);
new XWPFDocument(is).close(); new XWPFDocument(is).close();
} 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;
} }
...@@ -97,7 +88,8 @@ public class FileHelper { ...@@ -97,7 +88,8 @@ public class FileHelper {
*/ */
public static boolean isPPT2003(File file) { public static boolean isPPT2003(File file) {
try(InputStream is = new FileInputStream(file); try(InputStream is = new FileInputStream(file);
HSLFSlideShow ppt = new HSLFSlideShow(is);) { ) {
HSLFSlideShow ppt = new HSLFSlideShow(is);
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }
...@@ -298,12 +290,10 @@ public class FileHelper { ...@@ -298,12 +290,10 @@ public class FileHelper {
br.remove(); br.remove();
} }
if(!content.isEmpty()){
for (Element meta : content) { content.get(0).attr("content", "text/html; charset=utf-8");
// 获取content节点,修改charset属性
meta.attr("content", "text/html; charset=utf-8");
break;
} }
// 转换成utf-8编码的文件写入 // 转换成utf-8编码的文件写入
FileUtils.writeStringToFile(htmFile, doc.html(), "utf-8"); FileUtils.writeStringToFile(htmFile, doc.html(), "utf-8");
} }
...@@ -329,8 +319,8 @@ public class FileHelper { ...@@ -329,8 +319,8 @@ public class FileHelper {
// 获取html节点 // 获取html节点
Element element = doc.body(); Element element = doc.body();
Elements content = head.getElementsByAttributeValueStarting("name", "meta:page-count"); Elements content = head.getElementsByAttributeValueStarting("name", "meta:page-count");
for (Element meta : content) { if(!content.isEmpty()){
String value = meta.attr("content"); String value = content.get(0).attr("content");
try { try {
Integer count = Integer.valueOf(value); Integer count = Integer.valueOf(value);
Elements ps = element.getElementsByTag("p"); Elements ps = element.getElementsByTag("p");
...@@ -357,11 +347,10 @@ public class FileHelper { ...@@ -357,11 +347,10 @@ public class FileHelper {
e.printStackTrace(); e.printStackTrace();
} }
// 获取content节点,修改charset属性
break;
} }
// 转换成utf-8编码的文件写入 // 转换成utf-8编码的文件写入
FileUtils.writeStringToFile(htmFile, "<!DOCTYPE html>" + doc.html(), "utf-8"); FileUtils.writeStringToFile(htmFile, "<!DOCTYPE html>" + doc.html(), "utf-8");
} }
...@@ -437,39 +426,30 @@ public class FileHelper { ...@@ -437,39 +426,30 @@ public class FileHelper {
} }
public static void nioTransferCopy(File source, File target) { public static void nioTransferCopy(File source, File target) {
FileChannel in = null;
FileChannel out = null;
FileInputStream inStream = null;
FileOutputStream outStream = null;
try {
inStream = new FileInputStream(source); try
outStream = new FileOutputStream(target); (
in = inStream.getChannel(); FileInputStream inStream = new FileInputStream(source);
out = outStream.getChannel(); FileOutputStream outStream = new FileOutputStream(target);
FileChannel in = inStream.getChannel();
FileChannel out = outStream.getChannel();
){
in.transferTo(0, in.size(), out); in.transferTo(0, in.size(), out);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(inStream);
close(in);
close(outStream);
close(out);
} }
} }
private static boolean nioBufferCopy(File source, File target) { private static boolean nioBufferCopy(File source, File target) {
FileChannel in = null;
FileChannel out = null; try (
FileInputStream inStream = null; FileInputStream inStream = new FileInputStream(source);
FileOutputStream outStream = null; FileOutputStream outStream = new FileOutputStream(target);
try { FileChannel in = inStream.getChannel();
inStream = new FileInputStream(source); FileChannel out = outStream.getChannel();)
outStream = new FileOutputStream(target); { ByteBuffer buffer = ByteBuffer.allocate(4096);
in = inStream.getChannel();
out = outStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(4096);
while (in.read(buffer) != -1) { while (in.read(buffer) != -1) {
buffer.flip(); buffer.flip();
out.write(buffer); out.write(buffer);
...@@ -478,22 +458,18 @@ public class FileHelper { ...@@ -478,22 +458,18 @@ public class FileHelper {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} finally {
close(inStream);
close(in);
close(outStream);
close(out);
} }
return true; return true;
} }
public static void customBufferStreamCopy(File source, File target) { public static void customBufferStreamCopy(File source, File target) {
InputStream fis = null;
OutputStream fos = null; try (
try { InputStream fis = new FileInputStream(source);
fis = new FileInputStream(source); OutputStream fos = new FileOutputStream(target);)
fos = new FileOutputStream(target);
{
byte[] buf = new byte[4096]; byte[] buf = new byte[4096];
int i; int i;
while ((i = fis.read(buf)) != -1) { while ((i = fis.read(buf)) != -1) {
...@@ -501,9 +477,6 @@ public class FileHelper { ...@@ -501,9 +477,6 @@ public class FileHelper {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(fis);
close(fos);
} }
} }
...@@ -537,12 +510,12 @@ public class FileHelper { ...@@ -537,12 +510,12 @@ public class FileHelper {
} }
} else { } else {
// 如果目标文件所在目录不存在,则创建目录 // 如果目标文件所在目录不存在,则创建目录
if (!destFile.getParentFile().exists()) { if (!destFile.getParentFile().exists()&&!destFile.getParentFile().mkdirs()) {
// 目标文件所在目录不存在 // 目标文件所在目录不存在
if (!destFile.getParentFile().mkdirs()) {
// 复制文件失败:创建目标文件所在目录失败 // 复制文件失败:创建目标文件所在目录失败
return false; return false;
}
} }
} }
...@@ -559,7 +532,7 @@ public class FileHelper { ...@@ -559,7 +532,7 @@ public class FileHelper {
* @param overlay 如果目标目录存在,是否覆盖 * @param overlay 如果目标目录存在,是否覆盖
* @return 如果复制成功返回true,否则返回false * @return 如果复制成功返回true,否则返回false
*/ */
public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay) { public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)throws NoSuchFileException, URISyntaxException, DirectoryNotEmptyException, IOException {
// 判断源目录是否存在 // 判断源目录是否存在
File srcDir = new File(srcDirName); File srcDir = new File(srcDirName);
if (!srcDir.exists()) { if (!srcDir.exists()) {
...@@ -579,7 +552,10 @@ public class FileHelper { ...@@ -579,7 +552,10 @@ public class FileHelper {
if (destDir.exists()) { if (destDir.exists()) {
// 如果允许覆盖则删除已存在的目标目录 // 如果允许覆盖则删除已存在的目标目录
if (overlay) { if (overlay) {
new File(destDirName).delete();
Path p = Paths.get(new URI(destDirName));
Files.delete(p);
} else { } else {
log.info(""); log.info("");
return false; return false;
...@@ -673,9 +649,10 @@ public class FileHelper { ...@@ -673,9 +649,10 @@ public class FileHelper {
Element tdd = doc.createElement("td"); Element tdd = doc.createElement("td");
tr.appendChild(tdd); tr.appendChild(tdd);
} }
if(max!=null){
max.appendChild(tr); max.appendChild(tr);
} }
}
table.parent().remove(); table.parent().remove();
} }
FileUtils.writeStringToFile(htmFile, doc.html(), "utf-8"); FileUtils.writeStringToFile(htmFile, doc.html(), "utf-8");
...@@ -819,32 +796,16 @@ public class FileHelper { ...@@ -819,32 +796,16 @@ public class FileHelper {
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new BaseException("下载文件名编码时出现错误."); throw new BaseException("下载文件名编码时出现错误.");
} }
OutputStream outputStream = null;
ZipOutputStream zos = null; try (
try { OutputStream outputStream = response.getOutputStream();
outputStream = response.getOutputStream(); ZipOutputStream zos = new ZipOutputStream(outputStream);)
zos = new ZipOutputStream(outputStream);
downloadZip(fileName.replace(".zip", ""), zos, list); { downloadZip(fileName.replace(".zip", ""), zos, list);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (zos != null) {
try {
zos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
} }
} }
...@@ -856,29 +817,15 @@ public class FileHelper { ...@@ -856,29 +817,15 @@ public class FileHelper {
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new BaseException("下载文件名编码时出现错误."); throw new BaseException("下载文件名编码时出现错误.");
} }
OutputStream outputStream = null;
ZipOutputStream zos = null; try (
try { OutputStream outputStream = response.getOutputStream();
outputStream = response.getOutputStream(); ZipOutputStream zos = new ZipOutputStream(outputStream);)
zos = new ZipOutputStream(outputStream); {
log.info("结束");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (zos != null) {
try {
zos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
} }
} }
...@@ -888,19 +835,21 @@ public class FileHelper { ...@@ -888,19 +835,21 @@ public class FileHelper {
String checkId = map.get("id").toString(); String checkId = map.get("id").toString();
//文件名称(带后缀) //文件名称(带后缀)
String fileName = map.get("photoData").toString(); String fileName = map.get("photoData").toString();
InputStream is = null;
BufferedInputStream in = null;
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int len; int len;
//创建zip实体(一个文件对应一个ZipEntry) //创建zip实体(一个文件对应一个ZipEntry)
ZipEntry entry = new ZipEntry(baseDir + fileName.substring(fileName.lastIndexOf(File.separator), fileName.length())); ZipEntry entry = new ZipEntry(baseDir + fileName.substring(fileName.lastIndexOf(File.separator), fileName.length()));
try {
//获取需要下载的文件流 //获取需要下载的文件流
File file = new File(fileName); File file = new File(fileName);
try (
InputStream is = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(is);
is = new FileInputStream(file); )
in = new BufferedInputStream(is); {
zos.putNextEntry(entry); zos.putNextEntry(entry);
//文件流循环写入ZipOutputStream //文件流循环写入ZipOutputStream
while ((len = in.read(buffer)) != -1) { while ((len = in.read(buffer)) != -1) {
...@@ -916,21 +865,6 @@ public class FileHelper { ...@@ -916,21 +865,6 @@ public class FileHelper {
e2.printStackTrace(); e2.printStackTrace();
} }
} }
if (in != null) {
try {
in.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} }
} }
...@@ -1075,9 +1009,9 @@ public class FileHelper { ...@@ -1075,9 +1009,9 @@ public class FileHelper {
* @Title: delFile * @Title: delFile
* @Description: 删除文件 * @Description: 删除文件
*/ */
public static void delFile(String filePath) { public static void delFile(String filePath) throws NoSuchFileException, URISyntaxException, DirectoryNotEmptyException, IOException {
File delFile = new File(filePath); Path p = Paths.get(new URI(filePath));
delFile.delete(); Files.delete(p);
} }
} }
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