项目中遇到要和ESKO软件进行数据对接,采用热文件夹形式,走xml数据交互.记录下点滴.前提:文件目录局域网共享出来.
上传xml:
//实现过程:先把一串XML数据源,写入本地文件,再通过SMB协议上传到指定服务器.可有办法直接把XML数据源写入远程?
String xml = "<xxxxxxx/>";
String fileName="A.xml";
//本地服务的目录
String localFile = "\\\\192.168.0.100\\upload\\TEMPXML\\"+fileName;
//远程服务目录
String eskofilePath = YssetHelper.getESKOFilePath(session,"UP");
//先把xml写入本地目录
BufferedWriter bw = null;
try {
File dir = new File(localFile);
if (!dir.exists())
dir.createNewFile();
OutputStreamWriter writerStream = new OutputStreamWriter(new FileOutputStream(localFile),"UTF-8");
bw = new BufferedWriter(writerStream);
bw.write(xml);
bw.flush();
} catch (Exception e) {
throw e;
} finally {
if (bw != null) {
bw.close();
}
}
//本地文件传送至esko远程目录 smb://Administrator:yy@192.168.0.189/upload/XML
//若服务器账户密码有关键字@,则不能直接这么拼接.
try {
SMBUtil smb = SMBUtil.getInstance("smb:"+eskofilePath);
smb.init();
File f = new File(localFile);
smb.uploadFile(f);//上传文件
} catch (Exception e) {
throw new Exception(e.getMessage());
}
下载xml数据:(这里就没必要写入文件了)
/**根据文件名到esko目录下找到xml文件,并转化为bean对象*/
public WorkOrder readXML(String fileName)throws Exception{
String eskofilePath = YssetHelper.getESKOFilePath(session,"DOWN");
SMBUtil smb = SMBUtil.getInstance("smb:"+eskofilePath+fileName);
smb.init();
String xmlStr = smb.downloadFile();
WorkOrder work = (WorkOrder)XmlUtil.xml2Java(xmlStr,WorkOrder.class);
return work;
}
下面贴上SMBUtil,想了解 xml2Java 和java2xml.可以关注我的其它文章.
package dockingESKO.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
/**
* 通过SMB协议发送文件到远程服务器
*
*/
public class SMBUtil {
private String url = "";
private SmbFile smbFile = null;
private SmbFileOutputStream smbOut = null;
private static SMBUtil smb = null;
/**
* 得到SMB和连接的方法
* @param url
* @return
*/
public static synchronized SMBUtil getInstance(String url) {
if(smb == null)
return new SMBUtil(url);
return smb;
}
private SMBUtil(String url) {
this.url = url;
}
public void init() {
try {
System.out.println("connecting...url:" + this.url);
smbFile = new SmbFile(this.url);
smbFile.connect();
System.out.println("connect successfu...url:" + this.url);
}catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上传文件到服务器
* @param file
* @return
*/
public void uploadFile(File file) throws Exception{
BufferedInputStream bf = null;
try{
this.smbOut = new SmbFileOutputStream(this.url + "/" + file.getName(), false);
bf = new BufferedInputStream(new FileInputStream(file));
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1){
this.smbOut.write(bt, 0, n);
this.smbOut.flush();
n = bf.read(bt);
}
System.out.println("file transmission complete...");
}finally{
try {
if(null != this.smbOut)
this.smbOut.close();
if(null != bf)
bf.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
}
public String downloadFile()throws Exception{
SmbFile smbFile = new SmbFile(url);
if(smbFile==null||!smbFile.exists()){
throw new Exception("未找到ESKO数据!");
}
String result="";
int length = smbFile.getContentLength();// 得到文件的大小
byte buffer[] = new byte[length];
SmbFileInputStream in = new SmbFileInputStream(smbFile);
// 建立smb文件输入流
while ((in.read(buffer)) != -1) {
result=new String(buffer, "UTF-8");
}
in.close();
System.out.println(result);
return result;
}
public static void main(String[] args) {
SMBUtil smb = SMBUtil.getInstance("smb://用户名:密码@服务器IP/COMMODITY");
File file = new File("E://服务器信dd息.txt");
// smb.uploadFile(file);//上传文件
}
}