方法:1、文件转换成pdf(采用openoffice或者jacob)

     2、抓取pdf首页图

第一步:采用jacob:

a、下载jacob  注意区分32位,64位,否则不能用
    将dll文件放在java bin目录下即可
    jar包引入项目

b、转换pdf

package cn.bnsr.edu_yun.util;

import java.io.File;  

import java.io.IOException;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; public class TestUtil {
static final int wdFormatPDF = 17;// PDF 格式
public void wordToPDF(String sfileName,String toFileName){ System.out.println("启动Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
// doc = Dispatch.call(docs, "Open" , sourceFile).toDispatch();
doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {
sfileName, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
System.out.println("打开文档..." + sfileName);
System.out.println("转换文档到PDF..." + toFileName);
File tofile = new File(toFileName);
if (tofile.exists()) {
tofile.delete();
}
// Dispatch.call(doc, "SaveAs", destFile, 17);
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
toFileName, new Variant(17) }, new int[1]);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(doc,"Close",false);
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
//如果没有这句话,winword.exe进程将不会关闭
ComThread.Release();
}
public static void main(String[] args) {
TestUtil d = new TestUtil();
d.wordToPDF("D:\\work\\1111.docx", "d:\\work\\1111.pdf");
} }

第二步:抓取首页图:

  

package cn.bnsr.edu_yun.util;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction; import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage; public class PdfImageUtil {
// 将PDF格式的文件转换为JPG格式的文件
public static void pdfToJPG(String inputFile)throws IOException {
// load a pdf from a byte buffer
File file = new File(inputFile);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
//这句代码通道建立了map映射,如果要删除file那么得接触映射
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
PDFFile pdffile = new PDFFile(buf);
int totalpage =pdffile.getNumPages();
for (int i = 1; i <= totalpage; i++) {
if (i == 1) {
// draw the first page to an image
// 以图片的形式来描绘首页
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
// generate the image
// 生成图片
Image img = page.getImage(rect.width, rect.height, // width &
// height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img.getScaledInstance(rect.width, rect.height, Image.SCALE_SMOOTH), 0, 0, rect.width, rect.height,null); FileOutputStream out = new FileOutputStream( "D:\\work\\s111"+ ".jpg"); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // JPEG编码
// 关闭输出流
out.close();
break;
}
}
buf.clear();
channel.close();
raf.close();
unmap(buf);
file.delete(); }
//解除map映射
public static <T> void unmap(final Object buffer) {
AccessController.doPrivileged(new PrivilegedAction<T>(){
@Override
public T run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(buffer, new Object[0]);
cleaner.clean();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
});
} }

  

第三步、搞定,加入自己的业务逻辑,有问题欢迎提问。

  

java 上传文件-生成文件首页缩略图 生成pdf 抓取图片的更多相关文章

  1. java+上传后的文件展示

    文件夹结构支持 大文件上传控件6支持向服务器上传整个文件夹,并且在服务端保存时与本地目录结构完全保持一致,同时在数据库中也保留文件夹的层级结构.开发人员可以借助于数据库中的层级信息方便的管理文件,管理 ...

  2. java+上传大视频文件断点续传

     上周遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败. 一开始以为是session过期或者文件大小受系统限制,导致的错误. 查看了系统的配置文件没有看到文件大小限制, web.xml中s ...

  3. Java 上传解压zip文件,并且解析文件里面的excel和图片

    需求:上传一个zip文件,zip文件里面包含一个excel和很多图片,需要把excel里面的信息解析出来保存到表中,同时图片也转化成base64保存到数据库表中. PS:为了方便不同水平的开发人员阅读 ...

  4. java上传大文件解决方案

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在10G内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以10G来进行限制. 第一步: 前端修改 由于项目使用的是BJ ...

  5. java上传视频文件

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...

  6. JAVA上传文件到FTP上

    添加maven <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> ...

  7. edtftpj让Java上传FTP文件支持断点续传

    在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传:或者想做类似迅雷下载类似的功能,文件太大,今天传一 ...

  8. java上传文件常见几种方式

    1.ServletFileUpload 表单提交中当提交数据类型是multipare/form-data类型的时候,如果我们用servlet去做处理的话,该http请求就会被servlet容器,包装成 ...

  9. Java上传文件FTP服务器代码

    1. 在实际的应用重,通常是通过程序来进行文件的上传. 2. 实现java上传文件到ftp服务器中 新建maven项目 添加依赖 <dependency> <groupId>c ...

  10. Java上传文件夹(Jersey)

    背景介绍:公司要在CMS系统上为运营人员提供一个功能供运营人员将做好的活动页面上传到阿里云存储上,上传的内容为一个文件夹,文件夹内部有.html网页,JS文件夹下有JS文件,CSS文件夹下有样式表,I ...

随机推荐

  1. mybatis教程:入门>>精通>>实战

    以前曾经用过ibatis,这是mybatis的前身,当时在做项目时,感觉很不错,比hibernate灵活.性能也比hibernate好.而且也比较轻量级,因为当时在项目中,没来的及做很很多笔记.后来项 ...

  2. linux fork两次避免僵尸进程的程序(简单)

    #include<stdio.h> #include<unistd.h> int main() { pid_t pid,pid1; int status; ) { printf ...

  3. 深入浅出数据结构C语言版(17)——希尔排序

    在上一篇博文中我们提到:要令排序算法的时间复杂度低于O(n2),必须令算法执行"远距离的元素交换",使得平均每次交换减少不止1逆序数. 而希尔排序就是"简单地" ...

  4. PHPStorm调试PHP代码~实际操作+mark~~

    因为wamp自己已经下载了xdebug,只要配置开启就行了. 1. 配置php.ini(有就打开注释,没有就加上) XDEBUG Extension[xdebug]zend_extension =&q ...

  5. 01快速入门-04-Map、Set和iterable(ES6)

    1.Map 我们知道,在JS中其实对象的方式就跟Java中的Map极为相似,即键值对的方式.JS中,key必须是字符串,实际上Number等值作为key也是合理的,所以为了解决这个问题,在最新的ES6 ...

  6. JPA关系映射之one-to-one

    一对一关联有两种实现方式:一种是共享的主键关联,另一种是一对一的外键关联 1.共享的主键关联:让两个对象具有共同的主键值,以表明他们之间的一一对应关系. Person.java类 public cla ...

  7. 《Java I/O 从0到1》 - 第Ⅱ滴血 “流”

    前言 <Java I/O 从0到1>系列上一章节,介绍了File 类,这一章节介绍的是IO的核心 输入输出.I/O类库常使用流这个抽象概念.代表任何有能力产出数据的数据源对象或者是有能力接 ...

  8. xgboost安装指南(win10,win7 64位)

    ---恢复内容开始--- Win7 64位系统下安装XGBoost 1. 环境介绍 计算机系统:win7 64位 Xgboost版本:xgboost0.6 2. 依赖软件环境 1) python 64 ...

  9. 翻译:MariaDB字符集和排序规则

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  10. FirstIDL

    pro FIRSTIDL ;控制台输出 print,'first IDL' ;控制台输出 void=dialog_message('Hello,IDL world!',/information) en ...