Commit 152c4b8c authored by zhangqingle's avatar zhangqingle

增加页数

parent cbbe86bc
......@@ -23,48 +23,72 @@ public class FileReadableUtil {
fileReadable(filePath,(long)100,100);
}
public static void fileReadable(String path,Long fileMaxSize,Integer fileMaxPages) {
LogUtil.fileChangeLog("-----------FileReadableUtil-路径------------"+path);
File file = new File(path);
private static void closeOpcPackage(OPCPackage opcPackage){
try{
if(opcPackage != null){
opcPackage.flush();
opcPackage.close();
}
}catch(Exception e){
log.error("关闭 OPCPackage 输出流错误!", e);
}
}
private static void closeOtherStream(Closeable stream){
try{
if(stream != null){
stream.close();
}
}catch(Exception e){
log.error("关闭流错误!", e);
}
}
private static Integer docReadable(String path){
Integer pages;
InputStream is = null;
HSLFSlideShow slideShow = null;
// RandomAccessBuffer randomAccessBuffer = null;
PDDocument doc = null;
OPCPackage opcPackage = null;
Long size = file.length();
if (size > (long)(1024 * 1024 * fileMaxSize)){
throw new ServiceException(ResultServiceEnums.FILE_OVER_SIZE);
try {
is = new FileInputStream(new File(path));
WordExtractor ex = new WordExtractor(is);
pages = ex.getSummaryInformation().getPageCount();//总页数
} catch (Exception e) {
log.error("文件不可读---"+e);
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
}finally {
FileReadableUtil.closeOtherStream(is);
}
return pages;
}
int pages = -1;
private static Integer pptReadable(String path){
Integer pages;
InputStream is = null;
HSLFSlideShow slideShow = null;
try {
if (path.endsWith(".doc")) {
is = new FileInputStream(new File(path));
WordExtractor ex = new WordExtractor(is);
pages = ex.getSummaryInformation().getPageCount();//总页数
}else if (path.endsWith("ppt")) {
is =new FileInputStream(file);
slideShow = new HSLFSlideShow(is);
pages = slideShow.getSlides().size();
} else if (path.endsWith("pdf")) {
doc = PDDocument.load(file);
pages = doc.getDocumentCatalog().getPages().getCount();
}else if (path.endsWith("docx")) {
opcPackage = POIXMLDocument.openPackage(path);
XWPFDocument docx = new XWPFDocument(opcPackage);
pages = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
}else if (path.endsWith("pptx")){
opcPackage = POIXMLDocument.openPackage(path);
XMLSlideShow pptxfile = new XMLSlideShow(opcPackage);
pages = pptxfile.getSlides().size();
}
is =new FileInputStream(new File(path));
slideShow = new HSLFSlideShow(is);
pages = slideShow.getSlides().size();
} catch (Exception e) {
log.error("文件不可读---"+e);
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
}finally {
FileReadableUtil.closeOtherStream(slideShow);
FileReadableUtil.closeOtherStream(is);
}
return pages;
}
private static Integer pdfReadable(String path){
Integer pages;
PDDocument doc = null;
try {
doc = PDDocument.load(new File(path));
pages = doc.getDocumentCatalog().getPages().getCount();
} catch (Exception e) {
log.error("文件不可读---"+e);
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
}finally {
try{
if(doc != null){
doc.close();
......@@ -72,37 +96,80 @@ public class FileReadableUtil {
}catch(Exception e){
log.error("关闭 PDDocument 输出流错误!", 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);
}
try{
if(is != null){
is.close();
}
}catch(Exception e){
log.error("关闭 InputStream 输出流错误!", e);
}
FileReadableUtil.closeOtherStream(doc);
}
if (pages != -1 && pages > fileMaxPages) {
throw new ServiceException(ResultServiceEnums.FILE_OVER_PAGES);
return pages;
}
private static Integer docxReadable(String path){
Integer pages ;
OPCPackage opcPackage = null;
try {
opcPackage = POIXMLDocument.openPackage(path);
XWPFDocument docx = new XWPFDocument(opcPackage);
pages = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
} catch (Exception e) {
log.error("文件不可读---"+e);
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
}finally {
FileReadableUtil.closeOpcPackage(opcPackage);
}
return pages;
}
private static Integer pptxReadable(String path){
Integer pages;
XMLSlideShow pptxfile = null;
OPCPackage opcPackage = null;
try {
opcPackage = POIXMLDocument.openPackage(path);
pptxfile = new XMLSlideShow(opcPackage);
pages = pptxfile.getSlides().size();
} catch (Exception e) {
log.error("文件不可读---"+e);
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
} finally {
FileReadableUtil.closeOpcPackage(opcPackage);
}
return pages;
}
if (!CheckFileHeaderUtil.toCheck(path)){
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
public static void fileReadable(String path,Long fileMaxSize,Integer fileMaxPages) {
LogUtil.fileChangeLog("-----------FileReadableUtil-路径------------"+path);
File file = new File(path);
Long size = file.length();
if (size > (long)(1024 * 1024 * fileMaxSize)){
throw new ServiceException("该文件超过最大"+fileMaxSize+"M限制");
}
Integer pages = -1;
switch (path.substring(path.lastIndexOf(".") + 1)) {
case "doc":
pages = FileReadableUtil.docReadable(path);
break;
case "ppt":
pages = FileReadableUtil.pptReadable(path);
break;
case "pdf":
pages = FileReadableUtil.pdfReadable(path);
break;
case "docx":
pages = FileReadableUtil.docxReadable(path);
break;
case "pptx":
pages = FileReadableUtil.pptxReadable(path);
break;
default:
break;
}
if (pages != -1 && pages > fileMaxPages) {
throw new ServiceException("该文件超过最多"+fileMaxPages+"M限制");
}
// if (!CheckFileHeaderUtil.toCheck(path)){
// throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
// }
}
public File TXTHandler(File file) {
......@@ -126,9 +193,7 @@ public class FileReadableUtil {
}
String str = FileUtils.readFileToString(file, code);
FileUtils.writeStringToFile(file, str, "UTF-8");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
return file;
......
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