打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
基于SMB/JCIFS协议的共享文件上传和下载
Java代码
 
  1. package com.smb;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.io.OutputStream;  
  11.   
  12. import jcifs.smb.SmbFile;  
  13. import jcifs.smb.SmbFileInputStream;  
  14. import jcifs.smb.SmbFileOutputStream;  
  15.   
  16. public class Smbtest {  
  17.     /** 
  18.      * 从局域网中共享文件中得到文件并保存在本地磁盘上 
  19.      * @param remoteUrl 共享电脑路径 如:smb//administrator:123456@172.16.10.136/smb/1221.zip  , smb为共享文件 
  20.      * 注:如果一直出现连接不上,有提示报错,并且错误信息是 用户名活密码错误 则修改共享机器的文件夹选项 查看 去掉共享简单文件夹的对勾即可。 
  21.      * @param localDir 本地路径 如:D:/ 
  22.      */  
  23.     public static void smbGet(String remoteUrl,String localDir){  
  24.         InputStream in = null;  
  25.         OutputStream out = null;  
  26.         try {  
  27.             SmbFile smbFile = new SmbFile(remoteUrl);  
  28.             String fileName = smbFile.getName();  
  29.             File localFile = new File(localDir+File.separator+fileName);  
  30.             in = new BufferedInputStream(new SmbFileInputStream(smbFile));  
  31.             out = new BufferedOutputStream(new FileOutputStream(localFile));  
  32.             byte []buffer = new byte[1024];  
  33.             while((in.read(buffer)) != -1){  
  34.                 out.write(buffer);  
  35.                 buffer = new byte[1024];  
  36.             }  
  37.         } catch (Exception e) {  
  38.             e.printStackTrace();  
  39.         }finally{  
  40.             try {  
  41.                 out.close();  
  42.                 in.close();  
  43.             } catch (IOException e) {  
  44.                 e.printStackTrace();  
  45.             }  
  46.         }  
  47.     }  
  48.     /** 
  49.      * 把本地磁盘中的文件上传到局域网共享文件下 
  50.      * @param remoteUrl 共享电脑路径 如:smb//administrator:123456@172.16.10.136/smb 
  51.      * @param localFilePath 本地路径 如:D:/ 
  52.      */  
  53.     public static void smbPut(String remoteUrl,String localFilePath){  
  54.         InputStream in = null;  
  55.         OutputStream out = null;  
  56.         try {  
  57.             File localFile = new File(localFilePath);  
  58.             String fileName = localFile.getName();  
  59.             SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);  
  60.             in = new BufferedInputStream(new FileInputStream(localFile));  
  61.             out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));  
  62.             byte []buffer = new byte[1024];  
  63.             while((in.read(buffer)) != -1){  
  64.                 out.write(buffer);  
  65.                 buffer = new byte[1024];  
  66.             }  
  67.         } catch (Exception e) {  
  68.             e.printStackTrace();  
  69.         }finally{  
  70.             try {  
  71.                 out.close();  
  72.                 in.close();  
  73.             } catch (IOException e) {  
  74.                 e.printStackTrace();  
  75.             }  
  76.         }  
  77.     }  
  78.       
  79.     public static void main(String[] args) {  
  80.         smbPut("smb://administrator:123456@172.16.10.136/smb""E:/1221.zip");  
  81.         smbGet("smb://administrator:123456@172.16.10.136/smb/1221.zip""D:/");  
  82.   
  83.     }  
  84.   
  85. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java中JCIFS实现局域网访问
在java中如何用键盘输入一个数,字符,字符串
高效读取大数据文本文件(上亿行数据)
JAVA中使用jcifs集成AD域用户认证
Netty成功向客户端发送及接收16进制数据
java压缩解压代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服