最近接手一個EDI項目,收獲頗多。其實我在第一家公司是接觸過EDI的,當初我們用EDI主要實現了訂單數據傳輸,客戶向我們下達采購訂單,通過VPN及FTP工具將采購訂單以約定的報文形式放到指定的文件服務器中,然后我們EDI系統會定時去文件服務器中獲取報文,最后解析并生成我們的銷售訂單。這些年過去了,我仍記著當初用的最多的是EDI850、EDI855。
一、首先介紹一下EDI的概念
Electronic data interchange,電子數據交換。
EDI其實就是把原來紙質的訂單/發貨通知等業務單據,從傳真/快遞等傳統方式,變成在線的電子數據方式進行交互,從而提高業務效率。同時,通過一些EDI軟件的實施配置,往往可以直接從企業的業務系統生成出相應的單據數據,并自動傳送至客戶/供應商處,實現Application to Application的效果,避免紙質單據中人為造成的錯誤(如數據錯誤等)。所以IT系統完善的大公司更喜歡EDI,因為EDI對于業務人員是透明的,業務人員操作自己日常的業務系統就可以了。
EDI依賴于幾個部分:
1、EDI傳輸途徑,一般EDI通過AS2協議、FTP/S等進行直聯,也可以通過第三方的VAN(增值網)服務商中轉(這個和我們使用電子郵件的模式相同)。因為涉及到業務信息,傳輸安全要求會比較高;
2、EDI標準,即業務數據的組織形式,目前最常用的美標的ANSI X12和聯合國和歐盟的EDIFact,當然還有很多其他標準,只要EDI雙方遵循同一標準,就可以很好的實現EDI交互;
3、兩端的EDI軟件(環境),一般最好的實現方式是由EDI軟件自動收發EDI報文(數據文件),并自動集成到企業業務系統中。
其中傳輸途徑中,我們采用的是FTP進行文件傳輸的,這里我主要介紹一下Java是如何通過FTP工具進行文件的上傳與下載,這里我們的FTP服務器是Linux操作系統。
二、JAVA架包引用
其中“commons-net-1.4.1.jar”、“jakarta-oro-2.0.8.jar”點擊鏈接均可直接下載,并且確保是可以用的,大家可以放心下載。
三、文件路徑
客戶端需要上傳的文件夾路徑"E:\edi\edi850",默認是將該文件夾下所有文件上傳
FTP服務器端上傳路徑為“/home/test/edi850”,如果上傳為路徑則FTP服務器會建立對應同樣的路徑
四、JAVA代碼
FTP類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.pcmall; public class Ftp { private String ipAddr; // ip地址 private Integer port; // 端口號 private String userName; // 用戶名 private String pwd; // 密碼 private String 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類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
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; /** * 獲取ftp連接 * * @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; } /** * 關閉ftp連接 */ public static void closeFtp() { if (ftp != null && ftp.isConnected()) { try { ftp.logout(); ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } /** * ftp上傳文件 * * @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(); } } /** * 下載鏈接配置 * * @param f * @param localBaseDir * 本地目錄 * @param remoteBaseDir * 遠程目錄 * @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() + ">下載失敗" ); } } } } catch (Exception e) { logger.error(e); logger.error( "下載過程中出現異常" ); } } else { logger.error( "鏈接失敗!" ); } } /** * * 下載FTP文件 當你需要下載FTP文件的時候,調用此方法 根據<b>獲取的文件名,本地地址,遠程地址</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()); // 判斷文件是否存在,存在則返回 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( "輸出文件流異常" ); } } } } else { String newlocalRelatePath = relativeLocalPath + ftpFile.getName(); String newRemote = new String(relativeRemotePath + ftpFile.getName().toString()); File fl = new File(newlocalRelatePath); if (!fl.exists()) { fl.mkdirs(); } try { newlocalRelatePath = newlocalRelatePath + '/' ; newRemote = newRemote + "/" ; String currentWorkDir = ftpFile.getName().toString(); boolean changedir = ftp.changeWorkingDirectory(currentWorkDir); 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/" ); //如果是路徑則將上傳該路徑下所有文件,如果是文件則上傳該文件 FtpUtil.upload(file); // 把文件上傳在ftp上 FtpUtil.startDown(ftp, "E:/FTPTEST/" , "/home/test/edi850" ); // 下載ftp文件測試,第二個參數為客戶端下載地址,第三個參數為文件服務器下載路徑 System.out.println( "ok" ); } } |
上傳成功后,FTP文件服務器文件情況如下
下載成功后,客戶端文件如下
打開后,文件內容如下
好了,EDI中如何通過FTP工具進行文件的上傳與下載先介紹到這里,以上代碼本人均親自測試,可以運行。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。