Java 为什么';这张照片不能打印到我的打印机上吗?

Java 为什么';这张照片不能打印到我的打印机上吗?,java,printing,Java,Printing,我需要将编程生成的字符串打印到打印机上。但是,每当我运行此代码时,打印机都不会打印任何内容 import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import javax.print.Doc; import javax.print.DocFlavor; import javax.p

我需要将编程生成的
字符串
打印到打印机上。但是,每当我运行此代码时,打印机都不会打印任何内容

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class PrintMain {
    public static void main(String[] args) {
        PrintService[] ps = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE,null);
        String[] psNames = new String[ps.length];
        for(int i = 0;i < ps.length;i++) {
            psNames[i] = ps[i].getName();
        }
        String choiceString = (String)JOptionPane.showInputDialog(new JFrame(),"What printer do you want to use?","Printer",JOptionPane.PLAIN_MESSAGE,null,psNames,null);
        PrintService choice = null;
        for(int i = 0;i < psNames.length;i++) {
            if (choiceString.equals(psNames[i])) {
                choice = ps[i];
            }
        }
        if(choice!=null) {
            DocPrintJob job = choice.createPrintJob();
            String text = "This is a test string. A method call would usually generate text for this, but TL;DR.";
            InputStream stream = new  = ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
            Doc doc = new SimpleDoc(stream,DocFlavor.INPUT_STREAM.AUTOSENSE,null);
            try {
                PrintJobWatcher watch = new PrintJobWatcher();
                job.addPrintJobListener(watch);
                PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                pras.add(new Copies(1));
                job.print(doc,pras);
                watch.waitForDone();
            } catch(PrintException e1) {
                JOptionPane.showMessageDialog(new JFrame(),"A PrintException has occurred. Message: "+e1.getMessage());
            } finally {
                try {
                    stream.close();
                } catch(IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

我不明白为什么
字符串不打印出来。我做错了什么?

我找到了解决办法。使用此选项打印内容:

JTextArea toPrint = new JTextArea("Text here.");
try {
    boolean done = toPrint.print();
    JOptionPane.showMessageDialog(new JFrame(),"Printing "+(done?"completed":"canceled"));
} catch(PrinterException ex) {
    JOptionPane.showMessageDialog(new JFrame(),"The printer messed up. " +ex.getMessage());
}
它甚至可以打开一个小对话框来设置打印机

JTextArea toPrint = new JTextArea("Text here.");
try {
    boolean done = toPrint.print();
    JOptionPane.showMessageDialog(new JFrame(),"Printing "+(done?"completed":"canceled"));
} catch(PrinterException ex) {
    JOptionPane.showMessageDialog(new JFrame(),"The printer messed up. " +ex.getMessage());
}