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();
		
		}
	}
发布了2 篇原创文章 · 获赞 0 · 访问量 42

猜你喜欢

转载自blog.csdn.net/fdqzq/article/details/105416146