I am writing a program in Java using JT400. The program downloads certain spool files from AS400 to the client machine.
The process is roughly thus:
- Connect
- Access specific user's spool files
- Loop through files until specific spool file found
- Download that spool file to a local folder
- Disconnect
My current problem is that I don't know how to download the file to local directory.
I have a PrintObjectTransformedInputStream pdf file, but I don't know how to work with that.
Code below:
static void hrzn(List<String> strUser, // Users to search for
List<String> lisReports, // List of reports to search and download for the user
int ipYear, // Year parameter
int ipMonth, // Month parameter
int ipDay, // Day parameter
String strPath // Path where spool file will be downloaded to
) throws AS400SecurityException, RequestNotSupportedException, IOException, InterruptedException, ErrorCompletingRequestException, ObjectDoesNotExistException, OpenListException {
// Sign in to AS400
String strUserLogin = "*******************************";
String strPass = "***************************";
AS400 hrznAS400 = new AS400("*************", strUserLogin, strPass);
//User hrznUser = new User(hrznAS400, strUser);
System.out.println(strUser.size());
// Iterate through each user in the user list
for (int i = 0; i < strUser.size(); i++) {
System.out.println("User: " + strUser.get(i));
// Create SpooledFileOpenList of the output queue of the user
SpooledFileOpenList list = new SpooledFileOpenList(hrznAS400);
// Filter spool file list to specific user
list.setFilterUsers(new String[]{strUser.get(i)});
// Sort by date create. False means descending order;
list.addSortField(SpooledFileOpenList.DATE_OPENED, false);
// Get the filtered list of spool files
list.open();
Enumeration enumFiles = list.getItems();
// Loop through each of the spool files in the users output queue
while (enumFiles.hasMoreElements()) {
SpooledFileListItem item = (SpooledFileListItem) enumFiles.nextElement();
System.out.println("User ID: " + String.valueOf(strUser) + " File: " + item.getUserData() + " File Creation Date: " + item.getCreationDate());
// If the current spool file type is desired, execute code
if (lisReports.contains(item.getUserData().trim())) {
System.out.println("File: " + item.getUserData() + " Date Created: " + item.getCreationDate());
// Create a spooled file
SpooledFile splF = new SpooledFile(
hrznAS400, // AS400
item.getName(), // SPLF NAME
item.getNumber(), // SPFL NUMBER
item.getJobName(), // JOB NAME
item.getJobUser(), // JOB USER
item.getJobNumber() // JOB NUMBER
); // https://javadoc.io/static/net.sf.jt400/jt400/11.0/com/ibm/as400/access/SpooledFile.html
// Get printer device type
String strPrType = splF.getStringAttribute(PrintObject.ATTR_PRTDEVTYPE);
// Extract date/time info from item fields
LocalDateTime localDate = LocalDateTime.from(item.getCreationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
int year = localDate.getYear();
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
int hour = localDate.getHour();
int minute = localDate.getMinute();
int second = localDate.getSecond();
// If the printer device type equals *SCS, proceed with PDF transform
if (strPrType.equals("*SCS")) {
System.out.println("Created instance of spool file");
// Set up print parameter list
PrintParameterList printParms = new PrintParameterList();
printParms.setParameter(PrintObject.ATTR_WORKSTATION_CUST_OBJECT, "/QSYS.LIB/QCTXPDF.WSCST"); // https://javadoc.io/static/net.sf.jt400/jt400/11.0/com/ibm/as400/access/doc-files/PrintAttributes.html#HDRKEYIFS_8
printParms.setParameter(PrintObject.ATTR_MFGTYPE, "*WSCST");
printParms.setParameter(PrintObject.ATTR_DEVTYPE, "*AFPDS");
// Create a transformed input stream from the spooled file
PrintObjectTransformedInputStream is = splF.getTransformedInputStream(printParms); //https://www.ibm.com/docs/en/i/7.4?topic=considerations-workstation-customizing-object-wscst-parameter
System.out.println("Transformed spool file to PDF");
}
}
}
list.close();
}
}