Commit d5e31365 authored by tangwei's avatar tangwei

修改扫描错误

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