Commit b405aa43 authored by zhangqingle's avatar zhangqingle

怎加文件验证

parent 187cefd2
......@@ -321,12 +321,12 @@
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
<version>3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
......@@ -454,6 +454,15 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.17</version>
</dependency>
</dependencies>
......
......@@ -36,6 +36,7 @@ import org.rcisoft.core.aop.PageUtil;
import org.rcisoft.core.exception.ServiceException;
import org.rcisoft.core.model.PersistModel;
import org.rcisoft.core.result.ResultServiceEnums;
import org.rcisoft.core.util.FileReadableUtil;
import org.rcisoft.core.util.LogUtil;
import org.rcisoft.core.util.OfficeToPdf;
import org.rcisoft.core.util.UserUtil;
......@@ -634,9 +635,9 @@ public class BChapterServiceImpl implements BChapterService {
//2. pdf 本地是否存在, word ppt 转换后的pdf是否存在
LogUtil.fileChangeLog("------------------获取原路径文件-----------------------------" + filePath);
File fileOld = new File(filePath + File.separator + name);
LogUtil.fileChangeLog("------------------获取文件名-----------------------------" + fileOld);
String newName = name.substring(0, name.indexOf(".")) + "-pdf.pdf";
LogUtil.fileChangeLog("------------------获取新路径文件-----------------------------" + newName);
LogUtil.fileChangeLog("------------------原文件路径-----------------------------" + fileOld);
String newName = name.substring(0, name.lastIndexOf(".")) + "-pdf.pdf";
LogUtil.fileChangeLog("------------------新文件名-----------------------------" + newName);
//转换后的路径
File fileNew = new File(filePath + File.separator + newName);
LogUtil.fileChangeLog("------------------判断文件类型-----------------------------" + fileNew.getPath());
......@@ -647,6 +648,7 @@ public class BChapterServiceImpl implements BChapterService {
return returnBasePath + File.separator + name;
} else {
String downLoadName = downLoadFile(bFile, file, filePath);
FileReadableUtil.fileReadable(filePath + File.separator + downLoadName);
LogUtil.fileChangeLog("------------------返回下载后路径-----------------------------" + downLoadName);
return returnBasePath + File.separator + downLoadName;
}
......@@ -662,6 +664,11 @@ public class BChapterServiceImpl implements BChapterService {
fileOld.delete();
LogUtil.fileChangeLog("------------------设置下载路径----------------------------");
String downLoadFileUrl = downLoadFile(bFile, file, filePath);
//判断文件是否可读
LogUtil.fileChangeLog("------------------判断文件是否可读-----------------------------" + filePath + File.separator + name);
FileReadableUtil.fileReadable(filePath + File.separator + downLoadFileUrl);
LogUtil.fileChangeLog("-----------------------------------------------downLoadFileUrl:" + filePath + File.separator + downLoadFileUrl);
LogUtil.fileChangeLog("-----------------------------------------------filePath:" + filePath + File.separator + newName);
int result = officeToPdf.transformToPdf(filePath + File.separator + downLoadFileUrl, filePath + File.separator + newName);
......
......@@ -165,7 +165,7 @@ public interface BLessonPersonRepository extends BaseMapper<BLessonPerson> {
@ResultMap(value = "BaseResultMap")
List<BLessonPerson> selectPersonIdByLessonId(AppointLessonDTO param);
@Select("<script>select business_id,person_id,lesson_id , learn_progress, is_finish, finish_date from b_lesson_person where 1=1 " +
@Select("<script>select business_id,person_id,lesson_id , learn_progress, is_finish, finish_date, ever_finished from b_lesson_person where 1=1 " +
"and lesson_id = #{lessonId} " +
"<if test= \" chapterId !=null and chapterId !=''\"> and chapter_id ={chapterId} </if> "+
"</script>")
......
......@@ -251,6 +251,7 @@ public enum ResultServiceEnums {
FILE_NOT_NULL(151,"文件内容不能为空"),
VALUE_NOT_NULL(152,"积分不能为空"),
TRAIN_SIGNED(153,"您已签到,请勿重复签到"),
FILE_UNREADABLE(154,"该文件无法识别,可能已加密或损坏,请修正当前文件或上传其他文件"),
;
private Integer code;
......
package org.rcisoft.core.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.rcisoft.core.exception.ServiceException;
import org.rcisoft.core.result.ResultServiceEnums;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
@Slf4j
public class FileReadableUtil {
public static void main(String[] args) {
String filePath = "C:\\Users\\三\\Desktop\\素材\\2000页.docx";
fileReadable(filePath);
}
public static void fileReadable(String path) {
LogUtil.fileChangeLog("-----------FileReadableUtil-路径------------"+path);
File file = new File(path);
InputStream is = null;
HSLFSlideShow slideShow = null;
WordExtractor ex = null;
RandomAccessBuffer randomAccessBuffer = null;
OPCPackage opcPackage = null;
try {
if (path.endsWith(".doc")) {
is = new FileInputStream(new File(path));
ex = new WordExtractor(is);
}else if (path.endsWith("ppt")) {
is =new FileInputStream(file);
slideShow = new HSLFSlideShow(is);
} else if (path.endsWith("pdf")) {
is = new FileInputStream(file);
randomAccessBuffer = new RandomAccessBuffer(is);
PDFParser parser = new PDFParser(randomAccessBuffer);
parser.parse();
}else if (path.endsWith("docx") || path.endsWith("pptx")) {
opcPackage = POIXMLDocument.openPackage(path);
}
} catch (Exception e) {
log.error("文件不可读---"+e);
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
}finally {
try{
if(is != null){
is.close();
}
}catch(Exception e){
log.error("关闭 InputStream 输出流错误!", e);
}
try{
if(ex != null){
ex.close();
}
}catch(Exception e){
log.error("关闭 WordExtractor 输出流错误!", e);
}
try{
if(randomAccessBuffer != null){
randomAccessBuffer.close();
}
}catch(Exception e){
log.error("关闭 RandomAccessBuffer 输出流错误!", e);
}
try{
if(opcPackage != null){
opcPackage.close();
}
}catch(Exception e){
log.error("关闭 OPCPackage 输出流错误!", e);
}
try{
if(slideShow != null){
slideShow.close();
}
}catch(Exception e){
log.error("关闭 HSLFSlideShow 输出流错误!", 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