Arjun Pokkattu
Arjun Pokkattu

Reputation: 137

Text FIle printing Java Applet not working in windows OS

I am created a java applet for copy a text file content from remote location to local computer. Its working fine and also try to print using dos command(Windows XP). Its not working but its working fine in Ubuntu OS. Can you please help me to improve my code..

Here is my code

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();
         }

Upvotes: 0

Views: 436

Answers (2)

Noman ali abbasi
Noman ali abbasi

Reputation: 539

you can use java printing service which can be use on every platform. this code snippet send your print to your default printer.

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();
    } 
}

Upvotes: 1

FazoM
FazoM

Reputation: 4956

In windows you would like to use: filePath = "C:\\WINDOWS\\Temp\\OLFile";

and

Runtime.getRuntime().exec("print C:\\WINDOWS\\Temp\\OLFile").waitFor();

You can also google about PathSeparator.

Upvotes: 1

Related Questions