Windchill学习之WTObject主内容打包下载

功能需求:能选中多个WTObject,然后对这些WTObject的主内容进行打包下载

一、选中多个WTObject,获取这些object的主内容,压缩成.zip文件,并写到服务器,代码片段如下:


二、压缩完文件后,调用windchill的api获取文件的URL并下载文件,代码片段如图:


三、完整代码如下:

import java.beans.PropertyVetoException;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import wt.content.ApplicationData;
import wt.content.ContentHelper;
import wt.content.ContentHolder;
import wt.content.ContentItem;
import wt.content.ContentServerHelper;
import wt.content.FormatContentHolder;
import wt.doc.WTDocument;
import wt.fc.ReferenceFactory;
import wt.fc.WTReference;
import wt.httpgw.HTTPServletResponse;
import wt.util.WTException;
import com.ptc.core.components.beans.ObjectBean;
import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.forms.FormResultAction;
import com.ptc.netmarkets.model.NmObjectHelper;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;

public class DocExportToZip extends DefaultObjectFormProcessor {

	public FormResult doOperation(NmCommandBean commandBean, List<ObjectBean> paramList) throws WTException {
		ReferenceFactory factory = new ReferenceFactory(); 
		FormResult result = new FormResult(FormProcessingStatus.SUCCESS);
		try {
			getZipFile(commandBean, factory);//压缩文件Zip
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String js; 
		URL url = NmObjectHelper.constructOutputURL(new File("c:\\temp\\zipFile.zip"), "zipFile.zip");
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile("c:\\temp\\zipFile.zip");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		@SuppressWarnings("unchecked")
		Enumeration<ZipEntry> enu = (Enumeration<ZipEntry>) zipFile.getEntries();
		if (enu.hasMoreElements()) {
			js = "top.window.location=\"";
	        js = js.concat(url.toExternalForm());
	        js = js.concat("\";");

	        result.setStatus(FormProcessingStatus.SUCCESS);
	        result.setJavascript(js);
	        result.setNextAction(FormResultAction.JAVASCRIPT);
		}else {
			js = "alert(\"请选择内容!\");";

	        result.setStatus(FormProcessingStatus.SUCCESS);
	        result.setJavascript(js);
	        result.setNextAction(FormResultAction.JAVASCRIPT);
		}
        return result;
	}
	
	public static void getZipFile(NmCommandBean commandBean,ReferenceFactory factory) throws WTException, PropertyVetoException, IOException{
		ApplicationData data = null;
		List<NmOid> oids = commandBean.getActionOidsWithoutWizard();
		// File file = new File("C:\\test");
		OutputStream os = new BufferedOutputStream(new FileOutputStream(
				"c:\\temp\\zipFile.zip"));
		ZipOutputStream zos = new ZipOutputStream(os);
		zos.setEncoding("UTF-8");
		byte[] buf = new byte[8192];
		int len;
		for (NmOid oid : oids) {
			WTReference reference = factory.getReference(oid.toString());
			Object object = reference.getObject();
			if (object instanceof WTDocument) {
				WTDocument document = (WTDocument) object;
				ContentHolder contentHolder = ContentHelper.service
						.getContents(document);							
				ContentItem contentItem = ContentHelper
						.getPrimary((FormatContentHolder) contentHolder);		//获取对象主内容
				if (contentItem instanceof ApplicationData) {
					data = (ApplicationData) contentItem;
					// ContentServerHelper.service.writeContentStream(data,
					// file+File.separator+data.getFileName());
					ZipEntry ze = new ZipEntry(data.getFileName());
					zos.putNextEntry(ze);										//主内容打包压缩
					BufferedInputStream bis = new BufferedInputStream(
							ContentServerHelper.service
									.findLocalContentStream(data));
					while ((len = bis.read(buf)) > 0) {
						zos.write(buf, 0, len);								//将.Zip文件写入硬盘
					}
					zos.closeEntry();
				}
			}
		}
		zos.closeEntry();
		zos.close();
		os.close();
	}
}


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值