基于SMB/JCIFS协议的共享文件上传和下载(局域网中共享文件获取文件)

来源:互联网 发布:电脑怎么卸载软件 编辑:程序博客网 时间:2024/05/19 03:21

import jcifs.smb.SmbFile;import jcifs.smb.SmbFileInputStream;import jcifs.smb.SmbFileOutputStream;import java.io.*;public class Test {public static void main(String[] args) {// TODO Auto-generated method stubsmbPut("smb://administrator:Xqx1234@192.168.0.249/share", "F:/spfws.7z");smbGet("smb://administrator:Xqx1234@192.168.0.249/share/spfws.7z", "D:/");}/** * 下载 * @param remoteUrl * @param localDir */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 * @param localFilePath */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();}}}}



http://iteye.blog.163.com/blog/static/1863080962012111155514179/

http://supercharles888.blog.51cto.com/609344/1344301

原创粉丝点击