/**
* 从远程向本地拷贝文件
* @param remoteUrl 远程共享文件
* @param localFilePath 本机文件
*/
public static void smbCreateLoaclFile(String remoteUrl ,String localFilePath){ InputStream in = null;
OutputStream out =null;
File localFile = null;
SmbFileInputStream smbIn = null;
FileOutputStream fileOut = null;
byte[] sbyte = null;
try{
SmbFile remoteFile = new SmbFile(remoteUrl);
if (remoteFile == null){
FileLogger.error(remoteUrl +" ,共享文件不存!");
return ;
}
String fileName = remoteFile.getName();
localFile = new File(localFilePath + "/" + fileName);
smbIn = new SmbFileInputStream(remoteFile);
in = new BufferedInputStream(smbIn);
fileOut = new FileOutputStream(localFile);
out = new BufferedOutputStream(fileOut);
sbyte = new byte[1024];
while(in.read(sbyte) != -1){
out.write(sbyte);
sbyte = new byte[1024];
}
out.flush();
}catch(Exception e){
FileLogger.error("从远程 " + remoteUrl + "向本地 " + localFilePath + "拷贝文件",e);
}finally{
try{
if(fileOut != null){
fileOut.close();
}
if(smbIn != null){
smbIn.close();
}
if(out != null){
out.close();
}
if(in != null){
in.close();
}
sbyte = null;
localFile = null;
fileOut = null;
smbIn = null;
out = null;
in = null;
}catch(Exception e){
FileLogger.error("从远程 " + remoteUrl + "向本地 " + localFilePath + "拷贝文件 close stream error ",e);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String remoteUrl = "smb://administrator:[email protected]/EXPORT";
File file=new File("D://saveihc");
File[] files=file.listFiles();
for (File file2 : files) {
if(file2.isFile()){
System.out.println(file2.getAbsolutePath());
smbCreateRemotFile(remoteUrl, file2.getAbsolutePath());
}
}
//String localFilePath = "d:/SAVEIHC/1.txt";
// smbCreateRemotFile(remoteUrl, localFilePath);
}
这段代码在本地测试能存入共享文件夹,可丢上服务器运行就报错[ CAUSE]: The network name cannot be found.
jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:563)
at jcifs.smb.SmbTransport.send(SmbTransport.java:663)
at jcifs.smb.SmbSession.send(SmbSession.java:238)
at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176)
at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
at jcifs.smb.SmbFile.connect(SmbFile.java:954)
at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
at jcifs.smb.SmbFile.open0(SmbFile.java:972)
at jcifs.smb.SmbFile.open(SmbFile.java:1006)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:142)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:97)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:67)
at com.topcheer.tools.util.SmbRemoteFile.smbCreateRemotFile(SmbRemoteFile.java:41)
at com.spdb.imexport.ImportFileExecute.findFileAndShare(ImportFileExecute.java:187)
at com.spdb.file.upload.ImportFileServlet.doPost(ImportFileServlet.java:132)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at org.ofbiz.core.control.ContextSecurityFilter.doFilter(ContextSecurityFilter.java:199)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.doIt(WebAppServletContext.java:3684)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3650)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2268)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2174)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1446)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
at 望高手解答..在线等拉...
/**
* 从本地向远程拷贝文件
* @param remoteUrl 远程共享文件
* @param localFilePath 本机文件
*/
public static void smbCreateRemotFile(String remoteUrl ,String localFilePath){
InputStream in = null;
OutputStream out =null;
File localFile = null;
FileInputStream fileIn = null;
SmbFileOutputStream smbFileOut = null;
byte[] sbyte = null;
try{
localFile = new File(localFilePath );
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName );
fileIn = new FileInputStream(localFile);
in = new BufferedInputStream(fileIn);
smbFileOut = new SmbFileOutputStream(remoteFile);
out = new BufferedOutputStream(smbFileOut);
sbyte = new byte[1024];
while(in.read(sbyte) != -1){
out.write(sbyte);
sbyte = new byte[1024];
}
out.flush();
}catch(Exception e){
e.printStackTrace();
FileLogger.error("从本地 " + localFilePath + "向远程 " + remoteUrl + "拷贝文件",e);
}finally{
try{
if(smbFileOut != null){
smbFileOut.close();
}
if(fileIn != null){
fileIn.close();
}
if(out != null){
out.close();
}
if(in != null){
in.close();
}
sbyte = null;
localFile = null;
smbFileOut = null;
fileIn = null;
out = null;
in = null;
}catch(Exception e){
FileLogger.error("从本地 " + localFilePath + "向远程 " + remoteUrl + "拷贝文件 close stream error ",e);
}
}
}
localFile = new File(localFilePath );
String fileName = localFile.getName();
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("IP地址","用户名","密码");
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName, auth);