海关报关步骤:
1、跨境电商中,海关报关中报关文件的上传和下载通过 FTP 的形式执行。
2、跨境电商中每笔订单都是需要报关,将订单的相关参数生成 xml 格式的文件,g根据海关加密报关需求,将文件加密。
3、根据海关提供的 FTP 地址,将加密后的文件上传到相应的 FTP 文件夹。
4、报关文件上传成功完成之后,一般在 10 分钟之内,不管报关成功与否,都会生成相应的响应文件。
5、最后服务端可以将相应的响应文件下载下来,通过解密,就可以看到报关的具体情况了。
现在就说说报关文件在 FTP 中上传下载的具体代码实现:
1)、FTP 的相关参数配置文件 test.properties
#报关文件所在的目录
dirpath = /Users/**/Code/ftp/test/
#移动文件所在的目录
ultimatepath = /Users/**/Code/ftp/tests/
#下载FTP文件
downloadpath = /Users/**/Code/ftp/download/
#ftp 所在的IP 、账号 、密码 、端口 、目录
ip = 10.1.2.100
username = ZYD
password = ZYD
port = 2312
path = /
2)、上传报关文件
public class FTPUtil {
/**
* 连接ftp服务器
*
* @param url
* @param port
* @param username
* @param password
* @return FtpClient
* @throws FtpProtocolException
* @throws IOException
*/
public FtpClient connectFTP(String url, int port, String username, String password) { // 创建ftp
FtpClient ftp = null;
try {
// 创建地址
SocketAddress addr = new InetSocketAddress(url, port);
// 连接
ftp = FtpClient.create();
ftp.connect(addr);
// 登陆
ftp.login(username, password.toCharArray());
ftp.setBinaryType();
System.out.println(ftp.getWelcomeMsg());
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ftp;
}
/**
* 切换目录
*
* @param ftp
* @param path
*/
public void changeDirectory(FtpClient ftp, String path) {
try {
ftp.changeDirectory(path);
System.out.println(ftp.getWorkingDirectory());
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭ftp
*
* @param ftp
*/
public void disconnectFTP(FtpClient ftp) {
try {
ftp.close();
System.out.println("disconnect success!!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上传文件
*
* @param localFile
* @param ftpFile
* @param ftp
*/
public void upload(File localFile, String ftpFile, FtpClient ftp, String filename, Map<String,String> map) {
OutputStream os = null;
FileInputStream fis = null;
FileOutputStream fos = null;
String path = map.get("ultimatepath");
String filepath = path+filename;
try {
File destFile = new File(filepath);
fos = new FileOutputStream(destFile);
// 将ftp文件加入输出流中。输出到ftp上
os = ftp.putFileStream(ftpFile);
//File file = new File(localFile);
File file = localFile;
// 创建一个缓冲区
fis = new FileInputStream(file);
byte[] bytes = new byte[1024];
int c;
while ((c = fis.read(bytes)) != -1) {
os.write(bytes, 0, c);
fos.write(bytes, 0, c);
}
System.out.println("upload success!!");
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null)
os.close();
fos.close();
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件下载
*
* @param localFile
* @param ftpFile
* @param ftp
*/
public void download(String localFile, String ftpFile, FtpClient ftp) {
InputStream is = null;
FileOutputStream fos = null;
try {
// 获取ftp上的文件
is = ftp.getFileStream(ftpFile);
File file = new File(localFile);
byte[] bytes = new byte[1024];
int i;
fos = new FileOutputStream(file);
while ((i = is.read(bytes)) != -1) {
fos.write(bytes, 0, i);
}
System.out.println("download success!!");
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String testTransform(File rootpath, String filename , Map<String,String> map) {
FTPUtil ftpUtil = new FTPUtil();
String ip = map.get("ip");
int port = Integer.parseInt(map.get("port"));
String username = map.get("username");
String password = map.get("password");
String path = map.get("path");
// 连接ftp
FtpClient ftp = ftpUtil.connectFTP(ip, port, username, password);
// 切换目录
ftpUtil.changeDirectory(ftp, path);
//上传报关文件
System.out.println("-----start transform----");
ftpUtil.upload(rootpath, "/UPLOAD/" + filename + "", ftp, filename, map);
return "success";
//下载报关响应文件
// System.out.println("-----start downloading----");
// ftpUtil.download(map.get("downloadpath"), "/test_upload.xlm", ftp);
}
3)、代码中的配置数据都是通过 map 集合获取的。文件的上传和下载都是针对单个文件。