ppt转换图片及有密码情况处理

ppt转换图片方法 ,采用Jacob调用本地COM组件转换,其中作为服务器供用户上传时,如果用户传ppt是有密码的则会使ppt一直停留在输入密码状态,导致其它用户无法使用,这时可以简单做个超时判断,关闭当前转换,具体代码如下:

public static void changeAsImage(String filePath, String exportPath, String imageFormat, int imageWidth,
			int imageHeight) {
		logger.info("filePath:{},exportPath:{}", filePath, exportPath);
		Map<String, Object> map = new HashMap<>();
		//作为超时判断用
		map.put("start", System.currentTimeMillis());
		new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(timeOut);

					//超时处理
					if (map != null && map.containsKey("start")) {
						logger.warn("有人采用密码ppt导入或者转换超时");
						map.clear();
						// 超时强制关闭
						String command = "taskkill /f /im office.exe";
						String command1 = "taskkill /f /im wpp.exe";
						String command2 = "taskkill /f /im POWERPNT.EXE";
						try {
					      Runtime.getRuntime().exec(command2);
						  Runtime.getRuntime().exec(command);
						  Runtime.getRuntime().exec(command1);
						} catch (Exception e) {
							logger.warn("强制关闭出错{}",e.getMessage(),e);
						}

					}
				} catch (Exception e) {
					logger.warn("强制关闭出错{}",e.getMessage(),e);
				} finally {
					// 释放内存
					ComThread.Release();
				}
			}
		}).start();
		ActiveXComponent component = null;
		Dispatch presentation = null;
		try {
			ComThread.InitSTA();
			// 打开ppt
			component = new ActiveXComponent("PowerPoint.Application");
			map.put("component", component);
			// 获得演讲稿对象
			Dispatch presentations = component.getProperty("Presentations").toDispatch();
			// 打开一个ppt 并返回一个当前文档个对象
			presentation = Dispatch
					.call(presentations, "Open", new Variant(filePath), new Variant(0), new Variant(-1), new Variant(0))
					.toDispatch();
			Dispatch.call(presentation, "Export", new Variant(exportPath), new Variant(imageFormat),
					new Variant(imageWidth), new Variant(imageHeight));
			//成功清除超时状态
			map.clear();
		} catch (Throwable e) {
			logger.error("ppt转换图片出错:{}" , e.getMessage(), e);
			throw new RespException(e.getMessage(), e);
		} finally {
			if (component != null) {
				component.invoke("Quit", new Variant[0]);
			}
			// 释放内存
			ComThread.Release();
		
		}
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值