虽然看过ibm网站上的那篇文章,但自己没写过,时间太紧了,所以来求助一下,有段打印代码改过是:private void printFileAction()
{ File file = new File("d:\\1.text");//获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet 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);//进行文件的打印
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
这段代码在单独有mian方法的类中运行的时候能有效弹出属性窗口并成功打印,但我把这个方法放到一个servlet类中的dopost方法内运行就会提示出错说service()出错,service must not-null and not-empty的,由于编程基础不好见笑了,求哪位朋友不吝指点下,很着急,多谢先
{ File file = new File("d:\\1.text");//获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet 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);//进行文件的打印
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
这段代码在单独有mian方法的类中运行的时候能有效弹出属性窗口并成功打印,但我把这个方法放到一个servlet类中的dopost方法内运行就会提示出错说service()出错,service must not-null and not-empty的,由于编程基础不好见笑了,求哪位朋友不吝指点下,很着急,多谢先
解决方案 »
- Java基础
- 在接受UDP广播包时,如何获得发送方的IP及端口号?????????
- java tracker服务器 (bt服务器)
- 哪位高手指点一下,想要 JInternalFrame 最大化后恢复原来大小时执行一个操作怎么办??
- 一个比较基本的问题
- (实训题目)-有兴趣者请进!
- 急请高手解决的java数学类问题
- 谁能帮我修改一下窗口左上角的图标?
- 为什么System.out.println(new DataInputstream.readShort())不是期望的值?
- 我想学java,但不知怎么架设学习平台,操作系统包括winX和Linux,请各位指点?
- 如何调用抽象方法
- 如何使用jTable来显示数据库内表~ 高手请进
printService
, defaultService, flavor, pras);这句的时候就会出错,以前没涉及过这方面的编程
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//这个是寻找所有可用的打印服务,就是多台打印机连接的时候吧
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//这个是寻找默认的打印服务
这两句都没法找到windows默认设置的打印机,不知道是什么原因,有经验的朋友来指点下呀,有兴趣没写过这个的朋友也可以去帮忙调试下,这个代码我是在servlet上运行出问题的,就是通过jsp页面提交请求去servlet的类后,然后在servlet里面控制打印机打印,服务器用的是tomcat5.5,如果我单独写一个main方法的类来运行这段代码是可以的无出错,打印服务也找的到