利用的是SMB协议从远程服务器下载上传文件

可以在本地做一个共享文件夹放点东西来测试下

这是用到的jar包[jcifs-1.2.3.jar],不能设置0积分有点可惜。(https://download.csdn.net/download/qq_39019865/10276986)

package com.yss.test.FileReadWriter;  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.MalformedURLException;  import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;  public class RemoteAccessData {  /** * @param args * @throws IOException  */  public static void main(String[] args) throws IOException {  smbGet1("smb://192.168.75.204/test/新建 文本文档.txt");  smbGet("smb://192.168.75.204/test/新建 文本文档.txt","e:/");  }  /** * 方法一: *  * @param remoteUrl *            远程路径 smb://192.168.75.204/test/新建 文本文档.txt * @throws IOException */  public static void smbGet1(String remoteUrl) throws IOException {  SmbFile smbFile = new SmbFile(remoteUrl);  int length = smbFile.getContentLength();// 得到文件的大小  byte buffer[] = new byte[length];  SmbFileInputStream in = new SmbFileInputStream(smbFile);  // 建立smb文件输入流  while ((in.read(buffer)) != -1) {  System.out.write(buffer);  System.out.println(buffer.length);  }  in.close();  }  // 从共享目录下载文件  /** * 方法二: *    路径格式:smb://192.168.75.204/test/新建 文本文档.txt *              smb://username:password@192.168.0.77/test * @param remoteUrl *            远程路径 * @param localDir *            要写入的本地路径 */  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();  }  }  }  // 向共享目录上传文件  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();  }  }  }  // 远程url smb://192.168.0.77/test  // 如果需要用户名密码就这样:  // smb://username:password@192.168.0.77/test  }

如果是下载上传远程文件夹的话,需要把文件夹遍历一下,路径也需要修改,smbGet的最后要加“/”

// 从共享目录下载文件夹  /** * 方法二: *  smbGet("smb://tom:123@192.168.0.87/datatrans/price/","f:/");  * @param remoteUrl *            远程路径 * @param localDir *            要写入的本地路径 */  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;  }  SmbFile[] files = remoteFile.listFiles();for (SmbFile smbFile : files) {String fileName = smbFile.getName();File localFile = new File(localDir + File.separator + fileName);  in = new BufferedInputStream(new SmbFileInputStream(remoteFile+fileName));  out = new BufferedOutputStream(new FileOutputStream(localFile));  byte[] buffer = new byte[1024];  while (in.read(buffer) != -1) {  out.write(buffer);  buffer = new byte[1024];  }  }System.out.println("完成");} catch (Exception e) {  e.printStackTrace();  } finally {  try {  out.close();  in.close();  } catch (IOException 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();  }  }  }  

java下载上传远程文件相关推荐

  1. java ftpclient上传txt文件

    java ftpclient上传txt文件 import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp. ...

  2. 远程下载/上传 服务器文件到本地

    做网站的前后端分离时需要将文件上传到云服务器运行/将云服务器上的文件下载到本地 这里提供一种操作较简单的方法,用到的工具是Xshell及WSL Linux子系统 Xshell通过互联网从本地远程连接到 ...

  3. SpringMVC下载上传Excel文件

    前端框架:Bootstrap,后端框架:SpringMVC.Spring 下载.上传的页面如下: 模版内容如下: 下载.上传的前端代码如下: <div class="modal&quo ...

  4. java 分块上传_Java 文件分块上传客户端和服务器端源代码

    本博客介绍如何进行文件的分块上传.本文侧重介绍客户端,服务器端请参考博客<Java 文件分块上传服务器端源代码>.建议读者朋友在阅读本文代码前先了解一下 MIME 协议. 所谓分块上传并非 ...

  5. java实现上传_文件上传(java)

    最近看了一本书上的代码,代码的主要功能是实现文件的上传.但是,当我运行代码的时候竟然报错了.(我用的IDEA).有错就解决吧.以下是我遇到的几个错误. 废话不多说先附上源代码. UploadServl ...

  6. java http 上传大文件上传_java实现大文件的上传

    最近项目经理逼着让偶做树的展开,表嵌套表,可惜偶刚参加工作,水平低,这不在查资料嘛,可是不多久就传来了经理的叫嚣声,这么简单的东西,都一天了,你还没做完..................,哎真是郁闷 ...

  7. java web上传视频文件_怎样使用javaweb实现上传视频和下载功能?

    HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> My JSP 'index.jsp' starting page ...

  8. java websocket 上传大文件,使用java websocket API和Javascript上传文件

    我正在学习websocket并且已经使用websocket / json完成了聊天程序.但我坚持上传ATM的文件.任何建议&回答会很感激. 服务器端: package websocket; i ...

  9. java防止上传恶意文件_从补丁分析到在野利用:揭秘CVE20201464 Windows文件签名验证绕过漏洞疑云...

    背景 2020年8月12日,微软发布了8月份的漏洞风险通告.通告中一个显示已被公开披露和在野利用的漏洞进入了我们的视野.该漏洞编号为CVE-2020-1464,其被描述为Windows验证文件数字签名 ...

最新文章

  1. html5电影在线看,HTML5-电影影评网
  2. 【学习笔记】Python - PyQt
  3. vtk删除一个actor_如何构建一个基于actor的简单区块链
  4. 【转】PF_RING开发指南
  5. Opencv和C++实现canny边缘检测
  6. 苹果笔记本能不能用python_“苹”除了苹果还能组哪些词?苹组词,释义及造句汇总!...
  7. 前苹果M1芯片设计总监跳槽英特尔 或将负责所有SoC架构设计
  8. apicloud代码压缩和全局加密
  9. jQuery lazyload
  10. Qt4_实现自定义模型
  11. c++ 怎么配置头文件路径_OpenGL环境配置
  12. 您好,我是中科院做语音识别的博士生,我想参加阿里招聘但是,北京 爱问知识人...
  13. VBA读取固定文件夹中txt内容
  14. Fiddler抓包工具 学习笔记
  15. Python实验-字典攻击
  16. 实现自己的Protobuf Any
  17. DIY装机的看过来了! 一份实用的台式机硬件选取流程
  18. Avaya PBX sip trunk对接第三方sip server传递UUI的方法
  19. 二阶常微分方程的数值解法(中心差分法和有限体积法)
  20. 在word中用vba为选中区域自动添加行号或删除行号

热门文章

  1. c语言程序优化设计,C程序设计语言的教学策略优化设计
  2. 螺旋矩阵c++语言_一起刷 leetcode 之螺旋矩阵(头条和美团真题)
  3. WindowsAPI中W和A的区别
  4. PyQt5自定义信号与槽带示例讲解
  5. Linux vsyscall和vDSO加速系统调用
  6. git 如何忽略掉文件夹_#PY小贴士# 我的git仓库为什么每次提交都有很多改动?
  7. ihtml2document能不能根据id获取dom_JavaScript学习笔记(十三)-- DOM(上)
  8. 秋风秋雨愁煞人:寒宵独坐心如捣
  9. lisp画垫圈_晓东CAD家园-论坛-LISP/VLISP程序库-[LISP程序]:俺的画内六角圆柱头螺钉的LISP程序-见附件 - Powered by Discuz!...
  10. Linux xmpp网络不通,Pidgin XMPP协议拒绝访问漏洞