无法在Windows上使用Java Print Service API打印UTF8文档

时间:2013-04-24 07:09:30

标签: java java-api

我一直试图在Windows操作系统上使用Java Print Service API打印包含俄文字母的文本文档,但到目前为止还没有运气。打印机打印文件,但unicode字符出现乱码。

在运行程序时,指定了VM参数 -Dfile.encoding = utf8

以下是代码

    import java.io.DataInputStream;  
    import java.io.FileInputStream;  
    import java.io.UnsupportedEncodingException;  
    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.Sides;  


    public class PrintUtf8Doc {


        public static void main(String[] args) throws UnsupportedEncodingException {  
            FileInputStream textstream=null;   
            DataInputStream da=null;  
            byte buff[]=new byte[1024*20];  

            try {  
                textstream = new FileInputStream("D:\\test\\russian.txt");   
                da=new DataInputStream(textstream);  
                da.read(buff);  

            } catch (Exception e) {   
                e.printStackTrace();  
            }   
            if (textstream == null) {   
                return; 
            } 
    //       Set the document type

            DocFlavor myFormat = DocFlavor.BYTE_ARRAY.AUTOSENSE;


    //       Create a Doc
            Doc myDoc = new SimpleDoc(buff, myFormat, null);   
    //       Build a set of attributes  
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();   
            aset.add(Sides.DUPLEX);   

            System.out.println("Host Encoding: "+myFormat.hostEncoding);  


            PrintService service = PrintServiceLookup.lookupDefaultPrintService();  
            DocFlavor flavs[]=service.getSupportedDocFlavors();
            for(DocFlavor flavor: flavs){
              System.out.println(flavor.toString());                
            }

            // Create a print job from one of the print services  
            if (service!=null) {   

                DocPrintJob printJob=service.createPrintJob();  
                try {  
                    printJob.print(myDoc, aset);  
                } catch (PrintException e) {  
                    e.printStackTrace();  
                }               

            }

        }
    }

-------------------------------------------------------------------
The supported flavors are printed in the console

Host Encoding: UTF8  
image/gif; class="[B"  
image/gif; class="java.io.InputStream"  
image/gif; class="java.net.URL"  
image/jpeg; class="[B"  
image/jpeg; class="java.io.InputStream"  
image/jpeg; class="java.net.URL"  
image/png; class="[B"  
image/png; class="java.io.InputStream"  
image/png; class="java.net.URL"  
application/x-java-jvm-local-objectref; class="java.awt.print.Pageable"  
application/x-java-jvm-local-objectref; class="java.awt.print.Printable"  
application/octet-stream; class="[B"  
application/octet-stream; class="java.net.URL"  
application/octet-stream; class="java.io.InputStream"

我甚至尝试将docFlavor更改为 “application / octet-stream; class =”java.io.InputStream“ 并将inputStream传递给SimpleDoc()构造函数,但仍然是同样的问题

看起来即使平台编码设置为UTF8,Windows上的Java打印服务也无法正确识别字节。

是否有任何需要完成或更正的设置? 任何建议都会有很大的帮助

1 个答案:

答案 0 :(得分:1)

我认为您需要阅读UTF-8中的russian.txt,我认为因为该文件包含UTF-8字符,但正在windows-1252中读取(如果未在Java中指定,则为默认编码)转换可能会导致字符乱码。

您可以使用InputStreamReader将charset视为已读(请参阅下面的最后一个参数):

BufferedReader rd = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));

如果需要有byte [],那么你需要推断出从上面的阅读器转换它的方法,因为它们暴露了基于char的读取API(以及读取线路以及缓冲读取器的情况)