`

基于SMB/JCIFS协议的共享文件上传和下载

阅读更多
package com.smb;

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.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

public class Smbtest {
	/**
	 * 从局域网中共享文件中得到文件并保存在本地磁盘上
	 * @param remoteUrl 共享电脑路径 如:smb//administrator:123456@172.16.10.136/smb/1221.zip  , smb为共享文件
	 * 注:如果一直出现连接不上,有提示报错,并且错误信息是 用户名活密码错误 则修改共享机器的文件夹选项 查看 去掉共享简单文件夹的对勾即可。
	 * @param localDir 本地路径 如:D:/
	 */
	public static void smbGet(String remoteUrl,String localDir){
		InputStream in = null;
		OutputStream out = null;
		try {
			SmbFile smbFile = new SmbFile(remoteUrl);
			String fileName = smbFile.getName();
			File localFile = new File(localDir+File.separator+fileName);
			in = new BufferedInputStream(new SmbFileInputStream(smbFile));
			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();
			}
		}
	}
	/**
	 * 把本地磁盘中的文件上传到局域网共享文件下
	 * @param remoteUrl 共享电脑路径 如:smb//administrator:123456@172.16.10.136/smb
	 * @param localFilePath 本地路径 如:D:/
	 */
	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 main(String[] args) {
		smbPut("smb://administrator:123456@172.16.10.136/smb", "E:/1221.zip");
		smbGet("smb://administrator:123456@172.16.10.136/smb/1221.zip", "D:/");

	}

}

分享到:
评论
5 楼 956699 2014-09-23  
请问连接局域网中电脑密码带有@字符怎么办?
4 楼 zhangfan1098730630 2014-04-17  
请问我使用基于SMB/JCIFS协议的共享文件能实现我的电脑向外网服务器传输文件嘛 ?
3 楼 u011483955 2014-04-06  
ziyue0499 写道
android 2.3运行通过,可是在android 4.0.3却崩溃,请问如何修正呢?

在新开的线程中运行这个代码就行了
2 楼 lcak00 2014-03-26  
有BUG 写入的 文件 会多一些空字节
1 楼 ziyue0499 2013-02-26  
android 2.3运行通过,可是在android 4.0.3却崩溃,请问如何修正呢?

相关推荐

Global site tag (gtag.js) - Google Analytics