I recently took over an EDI project and gained a lot. In fact, I had contact with EDI in my first company. At that time, we used EDI to mainly realize order data transmission. Customers placed purchase orders to us and placed the purchase orders in the form of agreed messages to the designated file server through VPN and FTP tools. Then our EDI system will regularly go to the file server to obtain messages, and finally parse and generate our sales orders. After these years, I still remember that the ones I used the most were EDI850 and EDI855.
1. First, let’s introduce the concept of EDI
Electronic data interchange, electronic data exchange.
EDI actually uses the original paper order/shipment notice and other business documents from traditional methods such as fax/express to be used to interact online electronic data to improve business efficiency. At the same time, through the implementation and configuration of some EDI software, corresponding document data can often be generated directly from the enterprise's business system and automatically transmitted to customers/suppliers to achieve the effect of Application to Application and avoid human errors (such as data errors, etc.) in paper documents. Therefore, large companies with complete IT systems prefer EDI because EDI is transparent to business personnel, and business personnel can operate their daily business systems.
EDI depends on several parts:
1. EDI transmission pathway. Generally, EDI is directly connected through AS2 protocol, FTP/S, etc., and can also be transferred through third-party VAN (value-added network) service providers (this is the same as our email mode). Because it involves business information, the transmission security requirements will be relatively high;
2. EDI standards, that is, the organizational form of business data, the most commonly used American standards, ANSI X12, and EDIFact of the United Nations and the European Union. Of course, there are many other standards. As long as both EDI parties follow the same standard, EDI interaction can be achieved well;
3. The best way to implement EDI software (environment) at both ends is to automatically send and receive EDI messages (data files) by EDI software and automatically integrate them into the enterprise business system.
In the transmission path, we use FTP for file transfer. Here I will mainly introduce how Java uploads and downloads files through FTP tools. Here our FTP server is the Linux operating system.
2. JAVA package reference
Among them, "commons-net-1.4.1.jar" and "jakarta-oro-2.0.8.jar" can be downloaded directly by clicking on the link, and it is guaranteed to be used, so everyone can download it with confidence.
3. File path <br />The folder path "E:/edi/edi850" that the client needs to upload, by default, upload all files in this folder.
The upload path of the FTP server side is "/home/test/edi850". If uploaded as a path, the FTP server will establish the corresponding path.
4. JAVA code
FTP class
package com.pcmall; public class Ftp { private String ipAddr;// ip address private Integer port;// port number private String userName;// username private String pwd;// password private String path;// path public String getIpAddr() { return ipAddr; } public void setIpAddr(String ipAddr) { this.ipAddr = ipAddr; } public Integer getPort() { return port; } public void setPort(Integer port) { this.port = port; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } } FtpUtil class
package com.pcmall; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.apache.log4j.Logger; public class FtpUtil { private static Logger logger = Logger.getLogger(FtpUtil.class); private static FTPClient ftp; /** * Get ftp connection* * @param f * @return * @throws Exception */ public static boolean connectFtp(Ftp f) throws Exception { ftp = new FTPClient(); boolean flag = false; int reply; if (f.getPort() == null) { ftp.connect(f.getIpAddr(), 21); } else { ftp.connect(f.getIpAddr(), f.getPort()); } ftp.login(f.getUserName(), f.getPwd()); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return flag; } ftp.changeWorkingDirectory(f.getPath()); flag = true; return flag; } /** * Close ftp connection*/ public static void closeFtp() { if (ftp != null && ftp.isConnected()) { try { ftp.logout(); ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } /** * ftp upload file* * @param f * @throws Exception */ public static void upload(File f) throws Exception { if (f.isDirectory()) { ftp.makeDirectory(f.getName()); ftp.changeWorkingDirectory(f.getName()); String[] files = f.list(); for (String fstr : files) { File file1 = new File(f.getPath() + "/" + fstr); if (file1.isDirectory()) { upload(file1); ftp.changeToParentDirectory(); } else { File file2 = new File(f.getPath() + "/" + fstr); FileInputStream input = new FileInputStream(file2); ftp.storeFile(file2.getName(), input); input.close(); } } } else { File file2 = new File(f.getPath()); FileInputStream input = new FileInputStream(file2); ftp.storeFile(file2.getName(), input); input.close(); } } /** * Download link configuration* * @param f * @param localBaseDir * Local directory* @param remoteBaseDir * Remote directory* @throws Exception */ public static void startDown(Ftp f, String localBaseDir, String remoteBaseDir) throws Exception { if (FtpUtil.connectFtp(f)) { try { FTPFile[] files = null; boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir); if (changedir) { ftp.setControlEncoding("GBK"); files = ftp.listFiles(); for (int i = 0; i < files.length; i++) { try { downloadFile(files[i], localBaseDir, remoteBaseDir); } catch (Exception e) { logger.error(e); logger.error("<" + files[i].getName() + ">Download failed"); } } } } catch (Exception e) { logger.error(e); logger.error("Exception occurred during downloading"); } } else { logger.error("Link failed! "); } } /** * * Download FTP file When you need to download the FTP file, call this method to download based on the file name, local address, and remote address obtained by <b>* * @param ftpFile * @param relativeLocalPath * @param relativeRemotePath */ private static void downloadFile(FTPFile ftpFile, String relativeLocalPath, String relativeRemotePath) { if (ftpFile.isFile()) { if (ftpFile.getName().indexOf("?") == -1) { OutputStream outputStream = null; try { File locaFile = new File(relativeLocalPath + ftpFile.getName()); // Determine whether the file exists, and if (locaFile.exists()) { return; } else { outputStream = new FileOutputStream(relativeLocalPath + ftpFile.getName()); ftp.retrieveFile(ftpFile.getName(), outputStream); outputStream.flush(); outputStream.close(); } } catch (Exception e) { logger.error(e); } finally { try { if (outputStream != null) { outputStream.close(); } } catch (IOException e) { logger.error("Output file stream exception"); } } } } } else { String newlocalRelatePath = relativeLocalPath + ftpFile.getName(); String newRemote = new String(relativeRemotePath + ftpFile.getName().toString()); File fl = new File(newlocalRelatePath); if (!fl.exists()) { fl.mkdir(); } try { newlocalRelatePath = newlocalRelatePath + '/'; newRemote = newRemote + "/"; String currentWorkDir = ftpFile.getName().toString(); boolean changeir = ftp.changeWorkingDir); if (changedir) { FTPFile[] files = null; files = ftp.listFiles(); for (int i = 0; i < files.length; i++) { downloadFile(files[i], newlocalRelatePath, newRemote); } } if (changedir) { ftp.changeToParentDirectory(); } } catch (Exception e) { logger.error(e); } } } public static void main(String[] args) throws Exception { Ftp ftp = new Ftp(); ftp.setIpAddr("192.168.16.128"); ftp.setUserName("test"); ftp.setPwd("123456"); FtpUtil.connectFtp(ftp); File file = new File("E:/edi/edi850/");//If it is a path, all files under the path will be uploaded. If it is a file, the file will be uploaded FtpUtil.upload(file);// Upload the file on ftp FtpUtil.startDown(ftp, "E:/FTPTEST/", "/home/test/edi850");// Download the ftp file to test, the second parameter is the client download address, and the third parameter is the file server download path System.out.println("ok"); } }After the upload is successful, the FTP file server file situation is as follows
After the download is successful, after the client file is opened as follows, the file content is as follows. How to upload and download files through FTP tools in EDI will be introduced here first. The above codes I personally tested and can be run.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.