File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Unable to Print more than 1 File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Unable to Print more than 1 File" Watch "Unable to Print more than 1 File" New topic
Author

Unable to Print more than 1 File

Parikshit Kaushal
Greenhorn

Joined: Nov 23, 2009
Posts: 2
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()
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Please Use Code Tags. You can edit your post to add them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to Print more than 1 File
 
Similar Threads
Push/Grab archive file from server
Unable to Print more than 1 File
cleint-server interaction
display http header
How do I run a java file inside JSP?