• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to Print more than 1 File

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags. You can edit your post to add them.
 
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic