文本文件打印Java小程序在windows操作系统中不工作

文本文件打印Java小程序在windows操作系统中不工作,java,applet,Java,Applet,我创建了一个java小程序,用于将文本文件内容从远程位置复制到本地计算机。它工作正常,也可以尝试使用dos命令(WindowsXP)打印。它不工作,但在Ubuntu操作系统中工作良好。你能帮我改进一下代码吗 这是我的密码 try { String server=this.getParameter("SERVER"); String filename=this.getParameter("FILENAME"); String

我创建了一个java小程序,用于将文本文件内容从远程位置复制到本地计算机。它工作正常,也可以尝试使用dos命令(WindowsXP)打印。它不工作,但在Ubuntu操作系统中工作良好。你能帮我改进一下代码吗

这是我的密码

try {
            String server=this.getParameter("SERVER");
            String filename=this.getParameter("FILENAME");

            String osname=System.getProperty("os.name");
            String filePath="";

            URL url = new URL("http://10.162.26.8/openLypsaa/reports/report_oc/127.0.0.1_sys_ANN_milkbill");
            URLConnection connection = url.openConnection();
            InputStream is = connection.getInputStream();

            if("Linux".equals(osname))
            {
                filePath = "/tmp/OLFile";
            }
            else
            {
                filePath = "C:/WINDOWS/Temp/OLFile";

            }

            OutputStream output = new FileOutputStream(filePath);

            byte[] buffer = new byte[256];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1)
            {
                System.out.println(bytesRead);
                output.write(buffer, 0, bytesRead);
            }
            output.close();

    if("Linux".equals(osname))
                Runtime.getRuntime().exec("lp /tmp/OLFile").waitFor();
            else
                Runtime.getRuntime().exec("print C:/WINDOWS/Temp/OLFile").waitFor();
         }

在要使用的windows中:
filePath=“C:\\windows\\Temp\\OLFile”


您还可以在google上搜索关于PathSeparator的信息,您可以使用java打印服务,该服务可以在任何平台上使用。此代码段将打印内容发送到默认打印机

public static void main(String[] args) {                
    FileInputStream textStream;
    try 
    {
        textStream = new FileInputStream("D:\\email_addresses.txt");
        DocFlavor myFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc myDoc = new SimpleDoc(textStream, myFormat, null); 

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 

        aset.add(new Copies(1)); 
        aset.add(Sides.ONE_SIDED); 

        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();

        System.out.println("Printing to default printer: " + printService.getName());

        DocPrintJob job = printService.createPrintJob();
        job.print(myDoc, aset);

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (PrintException e) {
        e.printStackTrace();
    } 
}

谢谢你的重播。。。现在它开始工作了。。。非常感谢你
public static void main(String[] args) {                
    FileInputStream textStream;
    try 
    {
        textStream = new FileInputStream("D:\\email_addresses.txt");
        DocFlavor myFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc myDoc = new SimpleDoc(textStream, myFormat, null); 

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 

        aset.add(new Copies(1)); 
        aset.add(Sides.ONE_SIDED); 

        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();

        System.out.println("Printing to default printer: " + printService.getName());

        DocPrintJob job = printService.createPrintJob();
        job.print(myDoc, aset);

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (PrintException e) {
        e.printStackTrace();
    } 
}