通过SMB协议上传,下载⽂件.
项⽬中遇到要和ESKO软件进⾏数据对接,采⽤热⽂件夹形式,⾛xml数据交互.记录下点滴.前提:⽂件⽬录局域⽹共享出来. 上传xml:
//实现过程:先把⼀串XML数据源,写⼊本地⽂件,再通过SMB协议上传到指定服务器.可有办法直接把XML数据源写⼊远程?
String xml = "<xxxxxxx/>";
String fileName="A.xml";
//本地服务的⽬录
String localFile = "\\\\192.168.0.100\\upload\\TEMPXML\\"+fileName;
//远程服务⽬录
String eskofilePath = ESKOFilePath(session,"UP");
//先把xml写⼊本地⽬录
BufferedWriter bw = null;
try {
File dir = new File(localFile);
if (!ists())
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 = Instance("smb:"+eskofilePath);
smb.init();
File f = new File(localFile);
smb.uploadFile(f);//上传⽂件
} catch (Exception e) {
throw new Message());
}
下载xml数据:(这⾥就没必要写⼊⽂件了)
/**根据⽂件名到esko⽬录下到xml⽂件,并转化为bean对象*/
public WorkOrder readXML(String fileName)throws Exception{
String eskofilePath = ESKOFilePath(session,"DOWN");
SMBUtil smb = Instance("smb:"+eskofilePath+fileName);
smb.init();
String xmlStr = smb.downloadFile();
WorkOrder work = (l2Java(xmlStr,WorkOrder.class);
return work;
}
下⾯贴上SMBUtil,想了解 xml2Java 和java2xml.可以关注我的其它⽂章.
package dockingESKO.util;
import java.io.BufferedInputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.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 {
网络上xml是什么意思System.out.println("url:" + this.url);
smbFile = new SmbFile(this.url);
System.out.println("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 + "/" + Name(), 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 ");
System.out.println("file ");
}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||!ists()){
throw new Exception("未到ESKO数据!");
}
String result="";
int length = ContentLength();// 得到⽂件的⼤⼩
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 = Instance("smb://⽤户名:密码@服务器IP/COMMODITY");        File file = new File("E://服务器信dd息.txt");
// smb.uploadFile(file);//上传⽂件
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。