博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从服务器上共享文件上下载文件或上传文件
阅读量:6793 次
发布时间:2019-06-26

本文共 5498 字,大约阅读时间需要 18 分钟。

package com.login.example;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;

import jcifs.UniAddress;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import jcifs.smb.SmbSession;

public class LonginExample {

public static String userName ="administrator";

public static String password ="administrator";

public static String domainIP ="Tiancom.net";

 

NtlmPasswordAuthentication auth;{

getInParam();

}

 

 

private void getInParam(){

try {

 

UniAddress dc = UniAddress.getByName(domainIP);

 

System.out.println("UniAddress:"+dc);

 auth = new NtlmPasswordAuthentication(domainIP, userName, password);

 

System.out.println("auth:"+auth.getDomain());

 

System.out.println("username:"+auth.getUsername());

 

System.out.println("password:"+auth.getPassword());

 

SmbSession.logon(dc,auth);

} catch (UnknownHostException e) {

 

e.printStackTrace();

 

System.out.println("登录失败!!!");

} catch (SmbException e) {

 

e.printStackTrace();

 

System.out.println("无法访问!!!");

}

}

 

 

public static void main(String[] args) {

 

 

 

final LonginExample test = new LonginExample();

//单次执行

//从共享目录拷贝文件到本地

test.smbGet("smb://192.168.0.10/share/测试.txt","D:\UAPNETFile\HYCZ");

//向共享目录上传文件

test.smbPut("smb://192.168.0.10/share","D:/UAPNETFile/测试.txt");

 

 

 

 

 

 

//线程循环执行

 

 

 

 

 

 

try {

 

Thread.sleep(100000);

} catch (InterruptedException e) {

 

e.printStackTrace();

}

}

 

 

 

/**

 * 从共享目录拷贝文件到本地

 * */

public void smbGet(String remoteUrl, String localDir) {

InputStream in = null;

OutputStream out = null;

try {

 

getInParam();

 

SmbFile remoteFile = new SmbFile(remoteUrl, auth);

 

if (remoteFile == null) {

 

System.out.println("共享文件不存在");

 

return;

 

}

 

String fileName = remoteFile.getName();

 

File localFile = new File(localDir + File.separator + fileName);

 

in = new BufferedInputStream(new SmbFileInputStream(remoteFile));

 

out = new BufferedOutputStream(new FileOutputStream(localFile));

 

byte[] buffer = new byte[1024];

 

int i = 0;

 

while ((i = in.read(buffer)) != -1) {

 

out.write(buffer, 0, i);

 

}

} catch (Exception e) {

 

e.printStackTrace();

} finally {

 

try {

 

out.flush();

 

out.close();

 

in.close();

 

} catch (IOException e) {

 

e.printStackTrace();

 

}

}

}

/**

 * 向共享目录上传文件

 * */

public void smbPut(String remoteUrl,String localFilePath) {

InputStream in = null;

OutputStream out = null;

try {

 

File localFile = new File(localFilePath);

 

String fileName = localFile.getName();

 

SmbFile remoteFile = new SmbFile(remoteUrl, auth);

 

//如果文件夹不存在,先建立文件夹

 

if (!remoteFile.exists()) {

 

remoteFile.mkdirs();

 

}

 

remoteFile = new SmbFile(remoteUrl+fileName, auth);

 

//如果文件不存在先建立文件

 

if(!remoteFile.exists()){

 

remoteFile.createNewFile();

 

}

 

in = new BufferedInputStream(new FileInputStream(localFile));

 

System.out.println("SmbFileOutputStream"+new SmbFileOutputStream(remoteFile));

 

out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));

 

byte[] buffer = new byte[1024];

 

while(in.read(buffer)!=-1){

 

out.write(buffer);

 

buffer = new byte[1024];

 

}

} catch (Exception e) {

 

e.printStackTrace();

} finally {

 

try {

 

if(out!=null){

 

 

out.close();

 

}

 

if(in!=null){

 

 

in.close();

 

}

 

} catch (IOException e) {

 

e.printStackTrace();

 

}

}

}

}

一下是转载的代码,没有用域,参考了下

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import jcifs.smb.SmbFileInputStream;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

public class ReadShareFile {

public static void main(String[] args) {

new Thread() {

 

public void run() {

 

smbGet("smb://Administrator:01@192.168.0.47/share/测试.txt",

 

 

"D:\UAPNETFile\");

 

smbPut("smb://Administrator:01@192.168.0.47/share",

 

 

"D:/远程上传.txt");

 

System.out.println("操作已经完成");

 

}

}.start();

try {
Thread.sleep(100);
 } catch (InterruptedException e) {
e.printStackTrace();
}

}

//获取远程共享的文件内容

public static void smbPut(String remoteUrl, String localFilePath) {

InputStream in = null;

OutputStream out = null;

try {

 

File localFile = new File(localFilePath);

 

String fileName = localFile.getName();

 

SmbFile remoteFile = new SmbFile(remoteUrl +"/"+ fileName);

 

in = new BufferedInputStream(new FileInputStream(localFile));

 

out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));

 

byte[] buffer = new byte[1024];

 

while (in.read(buffer) != -1) {

 

out.write(buffer);

 

buffer = new byte[1024];

 

}

} catch (Exception e) {

 

e.printStackTrace();

} finally {

 

try {

 

out.close();

 

in.close();

 

} catch (IOException e) {

 

e.printStackTrace();

 

}

}

}

//向远程共享文件夹上传内容

public static void smbGet(String remoteUrl, String localDir) {

InputStream in = null;

OutputStream out = null;

try {

 

SmbFile remoteFile = new SmbFile(remoteUrl);

 

if (remoteFile == null) {

 

System.out.println("共享文件不存在");

 

return;

 

}

 

String fileName = remoteFile.getName();

 

File localFile = new File(localDir + File.separator + fileName);

 

in = new BufferedInputStream(new SmbFileInputStream(remoteFile));

 

out = new BufferedOutputStream(new FileOutputStream(localFile));

 

byte[] buffer = new byte[1024];

 

while (in.read(buffer) != -1) {

 

out.write(buffer);

 

buffer = new byte[1024];

 

}

} catch (Exception e) {

 

e.printStackTrace();

} finally {

 

try {

 

out.close();

 

in.close();

 

} catch (IOException e) {

 

e.printStackTrace();

 

}

}

}

}

你可能感兴趣的文章
Hibernate与 MyBatis的比较
查看>>
POJ 3292 Semi-prime H-numbers
查看>>
not only ... but also
查看>>
CentOS7搭建Docker私有仓库----Docker
查看>>
查找Mysql慢查询Sql语句
查看>>
ExtJS组件的xtype属性列表
查看>>
ExtJS ComboBox的用法+代码
查看>>
tensorflow学习笔记
查看>>
python和java的区别
查看>>
Scrum 项目4.0&&5.0
查看>>
记录nodejs的writeHead
查看>>
【贪心】【堆】Gym -100956D - Greedy Game
查看>>
关于输入框被键盘覆盖及收回键盘的问题
查看>>
sorted倒序
查看>>
阶乘之和
查看>>
OpenCASCADE BRep Projection
查看>>
2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple...
查看>>
ftrace: tracing linux function calls
查看>>
李涛ps高手之路
查看>>
技术总结
查看>>