将文件发送到windows打印机-java

将文件发送到windows打印机-java,java,cmd,printing,Java,Cmd,Printing,我试图用JAVA编写一个小程序,将X>=1文件发送到windows打印机,打开windows打印机对话框,等待用户选择默认打印机并单击打印。 比如在JS中,我可以使用“window.print()” 我尝试使用以下代码: public void printFile(String path) throws FileNotFoundException { FileInputStream in = new FileInputStream(path); Doc doc = new Sim

我试图用JAVA编写一个小程序,将X>=1文件发送到windows打印机,打开windows打印机对话框,等待用户选择默认打印机并单击打印。 比如在JS中,我可以使用“window.print()”

我尝试使用以下代码:

public void printFile(String path) throws FileNotFoundException {
    FileInputStream in = new FileInputStream(path);
    Doc doc = new SimpleDoc(in, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
    
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
    attrs.add(Sides.DUPLEX);
    
    PrintService selection = ServiceUI.printDialog(null, 100, 100, services, service, null, attrs); 
}
但是

  • 打开java打印对话框而不是windows打印对话框
  • 它不发送我的文件(因为我不知道如何处理“doc”变量)
  • 而且它不具有多个文件选项
  • 我能做什么

    tnx