Results 1 to 6 of 6
  1. #1
    satyam000 is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default Unable to Print more than 1 File

    Hi,
    I have written the Java Code for Printing files from my apllication.
    If i run this program stand alone, it is printing files as much as are there.
    But if i am including this program to my application, its printing onlY 1 file after that it got stopped.

    Actually while executing the program, initially socket gets open and it takes the file to print meanwhile it gets the 2nd file to print but at that time that socket is already opened. So coz of that this problem is happening as i think.

    Can anybody help me out for the same ?
    i have pasted the code for the same which i am using.

    I would be very thankful.


    Code:-

    //Method used to print files using printer IP and Port
    private boolean printWithIP(String printerIP, String printerPort, String fileName){
    boolean printStatus = false;
    Socket clientSocket = null;
    DataOutputStream outToServer = null;
    FileInputStream fin = null;
    try{
    File file = new File(fileName);
    if(fileName.contains("PCL") || fileName.contains("pcl") || fileName.contains("txt") || fileName.contains("TXT")){
    Thread.sleep(7000);
    clientSocket = new Socket(printerIP.trim(), Integer.parseInt(printerPort.trim()));
    outToServer = new DataOutputStream(clientSocket.getOutputStream());
    fin = new FileInputStream(file);
    byte fileContent[] = new byte[(int)file.length()];
    fin.read(fileContent);
    outToServer.write(fileContent);
    clientSocket.close();
    outToServer.close();
    fin.close();
    printStatus = true;
    }else if(fileName.contains("acc") || fileName.contains("ACC")){
    Thread.sleep(7000);
    clientSocket = new Socket(printerIP.trim(), Integer.parseInt(printerPort.trim()));
    outToServer = new DataOutputStream(clientSocket.getOutputStream());
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;
    try{
    reader = new BufferedReader(new FileReader(file));
    String text = null;
    // repeat until all lines is read
    while ((text = reader.readLine()) != null){
    contents.append(text).append("\n");
    }
    }catch(Exception e){e.printStackTrace();}
    System.out.println(contents.toString());
    outToServer.writeBytes(contents.toString());
    outToServer.close();
    printStatus = true;
    contents = null;
    reader = null;
    clientSocket.close();
    }else if(fileName.contains("pdf") || fileName.contains("PDF")){
    Document document = new Document();
    document.open();
    PdfReader reader = new PdfReader(fileName);
    Map info = reader.getInfo();
    String key;
    String value;
    for (Iterator i = info.keySet().iterator(); i.hasNext(); ) {
    key = (String) i.next();
    value = (String) info.get(key);
    System.out.println(key + ": " + value);
    }
    if (reader.getMetadata() == null) {
    } else {
    System.out.println("XML Metadata: "
    + new String(reader.getMetadata()));
    }
    // escapade 2
    StringBuffer strbufe = new StringBuffer();
    byte[] streamBytes = new byte[(int)reader.getFileLength()];
    for(int z=1; z<=reader.getNumberOfPages(); z++){
    PdfDictionary page = reader.getPageN(z);
    PRIndirectReference objectReference = (PRIndirectReference) page.get(PdfName.CONTENTS);
    // System.out.println("=== inspecting the stream of page" +z+" in object " +
    // objectReference.getNumber() + " ===");
    PRStream stream = (PRStream) PdfReader.getPdfObject(objectReference);
    streamBytes = PdfReader.getStreamBytes(stream);
    PRTokeniser tokenizer = new PRTokeniser(streamBytes);
    while (tokenizer.nextToken()) {
    if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
    // System.out.println(tokenizer.getStringValue());
    strbufe.append(tokenizer.getStringValue());
    // System.out.println(strbufe);
    }
    }
    }
    Thread.sleep(7000);
    clientSocket = new Socket(printerIP.trim(), Integer.parseInt(printerPort.trim()));
    outToServer = new DataOutputStream(clientSocket.getOutputStream());
    System.out.println(strbufe.toString());
    outToServer.writeBytes(strbufe.toString());
    outToServer.close();
    clientSocket.close();
    printStatus = true;
    document.close();
    }
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{

    try {
    clientSocket.close();
    } catch (IOException e) {
    try {
    outToServer.close();
    fin.close();
    clientSocket.close();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return printStatus;
    }//printWithIP()

  2. #2
    satyam000 is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default anY iDea..?

    Why no bodY is having solution or any idea about this..??

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Maybe because you've posted a bunch of unformatted, unindented code that nobody bothers to read?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    satyam000 is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default Please have a look

    OOooops m reallY reallY sorrY......actually m new to this forum.

    From the next time i'll keep dis in my minD.
    Please help.

    Here is the coDe::-

    {
    boolean printStatus = false;
    Socket clientSocket = null;
    DataOutputStream outToServer = null;
    FileInputStream fin = null;
    try{
    File file = new File(fileName);

    Thread.sleep(7000);
    clientSocket = new Socket(printerIP.trim(), Integer.parseInt(printerPort.trim()));


    outToServer = new DataOutputStream(clientSocket.getOutputStream());


    fin = new FileInputStream(file);
    byte fileContent[] = new byte[(int)file.length()];
    fin.read(fileContent);
    outToServer.write(fileContent);

    clientSocket.close();
    outToServer.close();
    fin.close();
    printStatus = true;
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Reasons for no answer:
    Question does not concern AWT/Swing
    Code too complex and ill formatted

    Suggestion. Put each of the file-writing pieces in a separate method.
    So the main method just tests file type and calls a file-writing method.
    Each of the file-writing methods should handle its own exceptions
    and close its files in a finally{} clause.

    This suggestion may not help. Quite possibly opening and closing sockets is a problem.
    A forum dedicated to I/O might have people who would know.

  6. #6
    satyam000 is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Unable to Print more than 1 File
    By satyam000 in forum Networking
    Replies: 1
    Last Post: 11-24-2009, 07:25 AM
  2. Unable to access velocity.properties file from jar file
    By mjwoodford in forum New To Java
    Replies: 0
    Last Post: 10-09-2009, 01:46 PM
  3. Print .prn file using java
    By nishant in forum Advanced Java
    Replies: 0
    Last Post: 07-14-2009, 09:15 AM
  4. Replies: 1
    Last Post: 06-19-2009, 10:47 AM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •