上传附件获取页数方便下次使用
测试test代码:
package com.sinolife.ccs.pdf;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.lowagie.text.pdf.PdfReader;
public class WordPageTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
wordToPdf("E:\\java\\22.docx", "E:\\java\\233.pdf");
}
/**
* word转成pdf
*
* @方法名:wordToPdf
* @参数 @param source doc路径
* @参数 @param target 目标pdf路径
* @返回类型 void
*/
public static void wordToPdf(String source, String target) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//String licensePath = FilePathCache.FilePathMap.get("licensePath");
//InputStream license = new FileInputStream(licensePath);
// 凭证文件,请正确设置读取凭证文件的路径
// InputStream license = new FileInputStream("E:\\ChromeCoreDownloads\\word_util\\wordorpdf.xml");// 凭证文件
//获取文件路径,非测试环境wordorpdf.xml文件放在resourcr文件夹下面读取方法
//File file = new File(this.getClass().getResource("/wordorpdf.xml").getPath());
InputStream license = new FileInputStream("E:\\ChromeCoreDownloads\\word_util\\wordorpdf.xml");
License aposeLic = new License();
aposeLic.setLicense(license);
fis = new FileInputStream(source);
fos = new FileOutputStream(target);
// 转换
Document doc = new Document(fis);
doc.save(fos, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.flush();
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//获取pdf页数
File file = new File(target);
PdfReader pdfReader = null;
try {
pdfReader = new PdfReader(new FileInputStream(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int pages = pdfReader.getNumberOfPages();
System.out.println("pdf文件的总页数为:" + pages);
}
}
上正式环境代码:
/**
* 获取word/pdf页数
* tempFile附件
* fileExt 附件格式
* target word转的pdf文件
* Integer 页数
* huangkai.wb DMP-59519 附件页数
*/
@Override
public Integer getWordPage(SFFile tempFile,String fileExt) throws IOException{
Integer filePage=1;
try {
String source = tempFile.getPath();
if(StringUtils.isNotEmpty(fileExt)){
if(fileExt.equals(".doc") || fileExt.equals(".docx")){
String target=null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//获取文件路径,非测试环境wordorpdf.xml文件放在resourcr文件夹下面读取方法
File fileWord = new File(this.getClass().getResource("/wordorpdf.xml").getPath());
InputStream license = new FileInputStream(fileWord);
License aposeLic = new License();
aposeLic.setLicense(license);
fis = new FileInputStream(source);
//获取临时创建的pdf文件路径与名称
target = source+".pdf";
fos = new FileOutputStream(target);
// 转换
Document doc = new Document(fis);
//创建pdf文件
doc.save(fos, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fos.flush();
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
File filePdf = new File(target);
PdfReader pdfReader = null;
try {
pdfReader = new PdfReader(new FileInputStream(filePdf));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
filePage = pdfReader.getNumberOfPages();
//操作完上面的文件 需要删除在根目录下生成的临时word文件
File word = new File(filePdf.toURI());
word.delete();
}else if(fileExt.equals(".pdf")){
PdfReader pdfReader = new PdfReader(new FileInputStream(tempFile));
filePage = pdfReader.getNumberOfPages();
}
}
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
return filePage;
} catch (Exception e) {
e.printStackTrace();
}
}
return filePage;
}
由于该页数读取word不精确可以加下面这段代码做修改页数:限制只能输入正整数
<input type='text' style="width:40px;border:none;" id="${item.fileId}" value="${item.filePage}" onkeyup="upperCase(this,this.id)" >
function upperCase(filePage,fileId){
filePage.value=filePage.value.replace(/^(0+)|[^\d]+/g,'');
var page = filePage.value;
if(page!=null&&page!=''){
$.ajax({
url : '路径',
data : "fileId=" + fileId+"&filePage="+page,
dataType : "json",
async: false,
success : function(data){
data = eval(data);
if(data[0].flag!='Y'){
}
}
});
}
}
jar包:
<!--word文件页数读取依赖包 -->
<dependency org="fakepath" name="aspose-words" rev="18.5.0718" />
<!-- PDF文件页数读取依赖包可以不需要 -->
<dependency org="org.apache.pdfbox" name="pdfbox" rev="1.8.11"/>
生产环境模拟获取路径代码:
/**
* 上传文件到IM服务器
* @throws IOException
* @throws IllegalStateException
*/
public FileInfo uploadAttachment(String caseNo, MultipartFile file)
throws IllegalStateException, IOException {
FileInfo fileInfo = new FileInfo();
//创建临时文件
SFFile tempFile = tempFileFactory.createTempFile();
//把上传文件内容写入临时文件中
file.transferTo(tempFile);
SFFilePath destPath = new SFFilePath();
destPath.setModule("CCS");
destPath.setModuleSubPath(new String[]{"attachment"});
destPath.setStogeType(StogeType.WEEK);
String originalFileName = file.getOriginalFilename();
destPath.setFileName(originalFileName);
String fileExt = originalFileName.substring(originalFileName.lastIndexOf(".")).toLowerCase();
if(fileExt.equals(".doc") || fileExt.equals(".docx")){
destPath.setMimeType(MimeType.application_msword_doc);
}else if(fileExt.equals(".xls") || fileExt.equals(".xlsx")){
destPath.setMimeType(MimeType.application_msexcel_xls);
}else if(fileExt.equals(".ppt") || fileExt.equals(".pptx")){
destPath.setMimeType(MimeType.application_mspowerpoint_ppt);
}else if(fileExt.equals(".pdf")){
destPath.setMimeType(MimeType.application_pdf);
}else if(fileExt.equals(".chm")){
destPath.setMimeType(MimeType.application_mshelp_chm);
}else if(fileExt.equals(".gif")){
destPath.setMimeType(MimeType.image_gif_gif);
}else if(fileExt.equals(".jpeg")){
destPath.setMimeType(MimeType.image_jpeg_jpeg);
}else if(fileExt.equals(".jpg")){
destPath.setMimeType(MimeType.image_jpeg_jpg);
}
String fileId = PlatformContext.getIMFileService().putFile(tempFile, destPath);
Integer filePage = getWordPage(tempFile,fileExt);
fileInfo.setFileId(fileId);
fileInfo.setFilePage(filePage);
return fileInfo;
}
原作者博客路径:
https://www.cnblogs.com/hunmeng/p/11983882.html
下载包原路径:
链接: https://pan.baidu.com/s/1xudkKqR1-TLLO0RPskyVjQ 提取码: adft