我需要一些幫助,這裏有我的PDF轉換器程序。Java:PDF轉換器可以在Mac中使用,但在Windows中可以生成空的PDF文件
所以,我正在使用JADE框架來做這個移動代理PDF轉換器。但是,我面臨的問題更多地與我將文本文件轉換爲PDF,將其作爲二進制文件在網絡上傳輸並將其恢復爲PDF文件的方式相關。
我寫的程序在我的MacBook上正常工作。 但是,在Windows上,它將我的PDF文件恢復爲空白PDF。
這是我用來發送PDF文件的代碼。
private void sendPDF(File f, String recipient) {
String content = "";
if(f != null) {
try {
FileInputStream fis = new FileInputStream(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int noBytesRead = 0;
byte[] buffer = new byte[1024];
while((noBytesRead = fis.read(buffer)) != -1) {
baos.write(buffer, 0, noBytesRead);
}
content = baos.toString();
fis.close();
baos.close();
System.out.println("Successful PDF-to-byte conversion.");
} catch (Exception e) {
System.out.println("Exception while converting PDF-to-byte.");
content = "failed";
e.printStackTrace();
}
} else {
System.out.println("PDF-to-file conversion failed.");
content = "failed";
}
ACLMessage message = new ACLMessage(ACLMessage.INFORM);
message.addReceiver(new AID(recipient, AID.ISLOCALNAME));
message.setContent(content);
myAgent.send(message);
System.out.println("PDF document has been sent to requesting client.");
}
而且,這裏是我用來恢復PDF的代碼。
private File restorePDF(String content) {
String dirPDF = dirBuffer + "/" + new Date().getTime() + ".pdf";
File f = new File(dirPDF);
try {
if(!f.exists()) f.createNewFile();
byte[] buffer = new byte[1024];
ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
FileOutputStream fos = new FileOutputStream(f);
int noBytesRead = 0;
while((noBytesRead = bais.read(buffer)) != -1) {
fos.write(buffer, 0, noBytesRead);
}
fos.flush();
fos.close();
bais.close();
} catch (Exception e) {
e.printStackTrace();
f = null;
}
return f;
}
對此的任何幫助將不勝感激! :)
任何日誌?異常?跟蹤? – Snicolas