从服务器上共享文件上下载文件或上传文件


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");




//线程循环执行
/*new Thread() {
public void run() {
LonginExample test = new LonginExample();
test.smbPut("smb://192.168.0.10/share","D:/UAPNETFile/测试.txt");
System.out.println("1");
}
}.start();*/



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();
}
}
}
}




一下是转载的代码,没有用域,参考了下
[code]
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();
}
}
}
}
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值