|
Hi friends, a few days ago I asked the community how to write from our AS/400 to a shared folder inside our network (eg. \\FILESERVER\MYFILES\MYFILE.TXT. I found the answer by myself and maybe it is interesting for you as well. Problem: I had to copy some spoolfiles from the IFS of our AS/400 to our faxserver which has to send them away. Then I found at http://jcifs.samba.org a pice of software which enable me to use those shared folders. And the best of all ist free :-) To give you a sample to start here is my code. If you have some comments feel free to answer me (am on vacation from 20.06.02-07.07.02). /* * copy2david.java * Stefan Lorei stefan.lorei@byk.com * 16.05.2002 * * Beschreibung: * Dieses Programm liest eine PCL-Datei, modifiziert den Inhalt, damit diese * durch den Faxserver Tobit bearbeitet werden kann. * Dies sind u.a. Angaben wie Absender und Empfänger. * */ import jcifs.smb.SmbFile; import jcifs.util.*; import jcifs.smb.*; //import jcifs.smb.SmbFileInputStream; //import jcifs.smb.SmbFileOutputStream; //import jcifs.netbios.NbtAddress; //import jcifs.util.Log; import java.util.Date; import java.io.*; public class copy2david implements AuthHandler { public static String readLine() throws Exception { int c; StringBuffer sb = new StringBuffer(); while(( c = System.in.read() ) != '\n' ) { if( c == -1 ) return ""; sb.append( (char)c ); } return sb.toString().trim(); } public boolean authenticate(AuthInfo authInfo ) { System.out.println( "Authentication for " + authInfo.domain + "\\" + authInfo.username + " failed" ); System.out.println( authInfo.exception.getMessage() + " for " + authInfo.target ); authInfo.domain = ""; authInfo.username = "faxgw"; authInfo.password = "gwfax"; // System.out.println( authInfo.exception.getMessage() + ", trying new credentials" ); return true; } public copy2david(String[] argv) throws Exception { SmbFile.setAuthHandler( this ); // System.out.println( "listed " + (new SmbFile( argv[0] ).listFiles()).length + " files" ); //System.out.println( "listed " + (new SmbFile( "smb://10.10.11.200/bfsfax" ).listFiles()).length + " files" ); } // copy non SMB-source public static void copyFile(File inputFile, File outputFile, String einfügen) throws IOException { FileReader in = null; FileWriter out = null; try { in = new FileReader(inputFile); out = new FileWriter(outputFile); int c; boolean einmal = false; while ((c = in.read()) != -1) { if (!einmal) { if (einfügen != null) // Einfügen angefordert // angeforderten Text am Anfang in Zieldokument einfügen out.write(einfügen); einmal = true; } out.write(c); } } finally { try { if (out != null) out.close(); } catch (IOException e) {} try { if (in != null) in.close(); } catch (IOException e) {} } } // copy from non SMB-source to SMB-source public static void copyFile(File fromFile, SmbFile toFile, String einfügen) throws IOException { /* Copy fromFile to toFile, overwriting toFile if it already exists. Throw IOException if there's any problem reading or writing the file. */ FileInputStream fis = null; SmbFileOutputStream fos = null; try { fos = new SmbFileOutputStream(toFile); byte[] buffer = new byte[1024 * 64]; if (einfügen != null) // Einfügen angefordert // angeforderten Text am Anfang in Zieldokument einfügen fos.write( (new String( einfügen)).getBytes() ); fis = new FileInputStream(fromFile); int bytesRead = fis.read(buffer); int i = 0; while(bytesRead > 0) { i++; //System.out.println("Buffer " + i + ":" + new String(buffer,0,bytesRead)); //System.out.println("Buffer " + i + ":" + buffer); //System.out.println("bytesRead " + i + ":" + bytesRead); fos.write(buffer, 0, bytesRead); bytesRead = fis.read(buffer); } fos.close(); fis.close(); } finally { try { if (fos != null) fos.close(); } catch (IOException e) {} try { if (fis != null) fis.close(); } catch (IOException e) {} } } // copy SMB-source public static void copyFile(SmbFile fromFile, SmbFile toFile, String einfügen) throws IOException { /* Copy fromFile to toFile, overwriting toFile if it already exists. Throw IOException if there's any problem reading or writing the file. */ SmbFileInputStream fis = null; SmbFileOutputStream fos = null; try { fis = new SmbFileInputStream(fromFile); fos = new SmbFileOutputStream(toFile); byte[] buffer = new byte[1024]; int bytesRead = fis.read(buffer); int i = 0; if (einfügen != null) // Einfügen angefordert // angeforderten Text am Anfang in Zieldokument einfügen fos.write( (new String( einfügen)).getBytes() ); while(bytesRead != -1) { i++; if (bytesRead > 0) { //System.out.println("Buffer " + i + ":" + new String(buffer,0,bytesRead)); //System.out.println("Buffer " + i + ":" + buffer); //System.out.println("bytesRead " + i + ":" + bytesRead); fos.write(buffer, 0, bytesRead); } else { } bytesRead = fis.read(buffer); } fos.close(); fis.close(); } finally { try { if (fos != null) fos.close(); } catch (IOException e) {} try { if (fis != null) fis.close(); } catch (IOException e) {} } } public static void createOUT( String[] argv ) throws Exception { String einfügen = null; File in = null; File out = null; if (argv.length == 0) { System.out.println("Keine Parameter übergeben. Kopiere alle *.PCL Dokumente nach David."); //inf = new SmbFile("smb://FAXGW:GWFAX@10.10.11.200/bfsfax/F000517.PCL"); einfügen = "" + "@@NUMMER 0281 670 9451@@" + "@@BENUTZER Herr Lorei@@" + "@@BETREFF Mein Betreff@@" + ""; } else { in = new File(argv[0]); System.out.println("argv[0]:" + argv[0]); if (argv.length >= 2) { einfügen = argv[1]; System.out.println("argv[1]:" + argv[1]); } } if(in != null) { // Datei angegeben // zuerst Zieldokument mit Empfängerdaten für Tobit erzeugen String outName = in.getName(); String outPath = in.getParent(); // Zieldateinamen ändern outName = outName.substring(0, outName.length() -3); outName = outName + "OUT"; out = new File(outPath + "/" + outName); copyFile(in, out, einfügen); //System.out.println("Zieldatei " + out + " erstellt."); in.delete(); //System.out.println("Quelldatei " + inf + " gelöscht."); } /* Log.setMask(( Log.ALL | Log.EXCEPTIONS ) & ~Log.HEX_DUMPS ); Log.println( Log.WARNINGS, "LogTest warning", "you've been warned" ); Log.println( Log.DEBUGGING, "This is a debugging message", "MADE IT!" ); Log.printHexDump( "This will not be printed", "hello, world".getBytes() ); */ /* // Show all files System.out.println("listFiles - Start"); //ok SmbFile file = new SmbFile("smb://FAXGW:GWFAX@10.10.11.200/BFSFAX/"); // ok SmbFile file = new SmbFile("smb://faxgw:gwfax@10.10.5.16/x/"); SmbFile file = new SmbFile("smb://faxgw:gwfax@10.10.10.252/ImportPCL/"); long t1 = System.currentTimeMillis(); SmbFile[] files = file.listFiles(); long t2 = System.currentTimeMillis() - t1; for( int i = 0; i < files.length; i++ ) { System.out.print( " " + files[i].getName() ); } System.out.println(); System.out.println( files.length + " files in " + t2 + "ms" ); System.out.println("listFiles - Ende"); */ } /* * Kopiere nun alle *.OUT Dateien ins Zielverzeichnis * (diese wurden bereits mit den Empfängerdaten für Tobit erstellt) */ public static void createPCL(String sourcedir) throws Exception { File in = null; File out = null; //System.out.println("Kopieren aller *.OUT Dateien von >" + sourcedir + "< nach David"); in = new File(sourcedir); if (!in.exists()) { System.out.println("Pfad oder Datei " + in + " nicht vorhanden !"); System.out.println("Keine Dateien zum Faxserver TOBIT kopiert."); } else { if (in.isFile()) { System.out.println(in + " ist kein Pfad !"); System.out.println("Keine Dateien zum Faxserver TOBIT kopiert."); } else { String[] dateien = in.list(); //System.out.println("dateien.length:" + dateien.length); for (int i = 0; i < dateien.length; i++) { // file ist eine Datei String inName = dateien[i]; if (inName.endsWith(".OUT")) { System.out.println("Datei " + inName + " wird bearbeitet."); // PCL-Datei nach David kopieren String outName = inName.substring(0, inName.length() -3); outName = outName + "PCL"; //System.out.println("outName:" + outName); File inf = new File(sourcedir + "/" + inName); //System.out.println("inf:" + inf); SmbFile outf = new SmbFile("smb://faxgw:gwfax@10.10.10.252/ImportPCL/" + outName); copyFile(inf, outf, null); //System.out.println("Zieldatei " + outf + " erstellt."); inf.delete(); //System.out.println("Quelldatei " + inf + " gelöscht."); } } } } } public static void main( String[] argv ) throws Exception { String sourcedir = "/home/BFSFax"; try { // PCL-Datei nach OUT-Datei kopieren // und OUT-Datei modifiszieren copy2david.createOUT(argv); // Dateien ins David-Verzeichnis kopieren copy2david.createPCL(sourcedir); // Meldung erzeugen. Wichtig für Fehlerbehandlung auf AS/400 System.out.println("Fax erfolgreich erstellt."); } catch (Exception e) { System.out.println("main: Fehler:"); e.printStackTrace(); } } } mit freundlichen Grüßen, Stefan Lorei, BYK-Chemie GmbH, D - 46462 Wesel, Abelstraße 45 DV-Abteilung Telefon: +049 281 670 451 Fax: +049 281 670 9451
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.