求教Java打印文档

nmhym0114 2013-03-13
客户在页面点击打印按钮   服务器端的word文档 通过客户端所连接的打印机打印出来..
目前有代码 但知为什么行不通
		   JFileChooser fileChooser = new JFileChooser(); //创建打印作业  

	            File file = new File("D:/测试.txt"); //获取选择的文件  
	            //构建打印请求属性集  
	            HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();  
	            //设置打印格式,因为未确定类型,所以选择autosense  
	            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;  
	            //查找所有的可用的打印服务  
	            PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);  
	            //定位默认的打印服务  
	            PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();  
	            //显示打印对话框  
	            PrintService service = ServiceUI.printDialog(null, 200, 200, printService,   
	                    defaultService, flavor, pras);  
	            if(service != null){  
	                try {  
	                    DocPrintJob job = service.createPrintJob(); //创建打印作业  
	                    FileInputStream fis = new FileInputStream(file); //构造待打印的文件流  
	                    DocAttributeSet das = new HashDocAttributeSet();  
	                    Doc doc = new SimpleDoc(fis, flavor, das);  
	                    job.print(doc, pras);  
	                    fis.close();
	                } catch (Exception e) {  
	                    e.printStackTrace();  
	                }  
	            }  


求大神指教
Global site tag (gtag.js) - Google Analytics