java利用SMB读取远程文件的方法

本文实例为大家分享了java利用SMB读取远程文件的具体代码,供大家参考,具体内容如下

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 

} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • java读取properties配置文件的方法

    本文实例讲述了java读取properties配置文件的方法.分享给大家供大家参考.具体分析如下: 这两天做java项目,用到属性文件,到网上查资料,好半天也没有找到一个满意的方法能让我读取到.properties文件中属性值,很是郁闷,网上讲的获取属性值大概有以下方法,以下三种方法逐渐优化,以达到最好的效果以下都以date.properties文件为例,该文件放在src目录下,文件内容为: startdate=2011-02-07 totalweek=25 方法一: public class

  • Java如何读取XML文件 具体实现

    今天的CSDN常见问题来讲解下在Java中如何读取XML文件的内容. 直接上代码吧,注释写的很清楚了! 复制代码 代码如下: import java.io.*;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;im

  • java实现读取txt文件中的内容

    我们先来看个例子 import java.io.*; /** * Created by liguoqing on 2016/3/28. */ public class ReadTxtFile { public static void readTxt(String filePath) { try { File file = new File(filePath); if(file.isFile() && file.exists()) { InputStreamReader isr = new

  • Java读取Excel文件内容的简单实例

    借助于apathe的poi.jar,由于上传文件不支持.jar所以请下载后将文件改为.jar,在应用程序中添加poi.jar包,并将需要读取的excel文件放入根目录即可 本例使用java来读取excel的内容并展出出结果,代码如下: 复制代码 代码如下: import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundExceptio

  • Java数据导入功能之读取Excel文件实例

    在编程中经常需要使用到表格(报表)的处理主要以Excel表格为主.下面给出用java读取excel表格方法: 1.添加jar文件 java导入导出Excel文件要引入jxl.jar包,最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件.下载地址:http://www.andykhan.com/jexcelapi/ 2.jxl对Excel表格的认识 (1)每个单元格的位置认为是由一个二维坐标(i,j)给定,其中i表示列,j表示

  • java中读取配置文件中数据的具体方法

    1.先在项目中创建一个包(如:config),再创建一个配置文件(如:a.properties),添加配置信息如下:比如: 复制代码 代码如下: name=kakaage=28 2.代码: 复制代码 代码如下: import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertyTest { public static void main(String[

  • Java读取文件的简单实现方法

    本文实例讲述了Java读取文件的简单实现方法,非常实用.分享给大家供大家参考之用.具体方法如下: 这是一个简单的读取文件的代码,并试着读取一个log文件,再输出. 主要代码如下: import java.io.*; public class FileToString { public static String readFile(String fileName) { String output = ""; File file = new File(fileName); if(file.

  • java读取csv文件示例分享(java解析csv文件)

    复制代码 代码如下: import java.io.*;import java.util.*;public class HandleCsv {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(   new InputStreamReader(    new FileInputStream("test.csv")   )); String li

  • Java实现按行读取大文件

    Java实现按行读取大文件 String file = "F:" + File.separator + "a.txt"; FileInputStream fis = new FileInputStream(file); RandomAccessFile raf = new RandomAccessFile(new File(file),"r"); String s ; while((s =raf.readLine())!=null){ Syste

  • java利用SMB读取远程文件的方法

    本文实例为大家分享了java利用SMB读取远程文件的具体代码,供大家参考,具体内容如下 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 jav

  • java利用POI读取excel文件的方法

    摘要:利用java读取excel文件,读取文件并获取文件中每一个sheet中的值. 一.需要提前导入的包: import java.io.File; import java.io.FileInputStream; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook

  • Java利用FileUtils读取数据和写入数据到文件

    目录 一.添加FileUtils依赖 二.读入文件内容 三.写入数据 前言:用一行代码实现读取文件内容 代码如下: 一.添加FileUtils依赖 <!-- FileUtils依赖--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </depend

  • java读取properties文件的方法实例分析

    本文实例讲述了java读取properties文件的方法.分享给大家供大家参考.具体分析如下: 1.不在项目中读取: Properties properties = new Properties(); BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream("文件的路径"),"utf-8")); properties.load(read); properti

  • java读取properties文件的方法

    本文实例讲述了java读取properties文件的方法.分享给大家供大家参考.具体实现方法如下: package com.test.demo; import java.util.Properties; import java.io.InputStream; import java.io.IOException; /** * 读取Properties文件的例子 * File: TestProperties.java */ public final class TestProperties { p

  • java使用POI读取properties文件并写到Excel的方法

    本文实例讲述了java使用POI读取properties文件并写到Excel的方法.分享给大家供大家参考.具体实现方法如下: package com.hubberspot.code; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import

  • Java实现从jar包中读取指定文件的方法

    本文实例讲述了Java实现从jar包中读取指定文件的方法.分享给大家供大家参考,具体如下: 以下的Java代码实现了从一个jar包中读取指定文件的功能: /** * This class implements the funcationality of reading and writing files in jar files. */ package com.leo.util; import java.io.InputStream; import java.io.FileOutputStrea

  • 详解Java项目中读取properties文件

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 1.最常用读取properties文件的方法InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面.如果在不同的包中,必须使用: InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/test

  • java正则匹配读取txt文件提取特定开头和结尾的字符串

    目录 前言 一.使用FileInputStream处理 二.使用正则开始匹配 1.匹配规则书写 2.pattern 代码案例 总结 前言 前天刚入职的算法同事,过来问我怎么提取txt文件中的数据,我一看这还不简单,结果…搞了好久. 正则不用真的会忘记,写篇博客增加一下记忆吧. 需求:提取txt文件中,有特定开头(双引号) ,特定结尾(双引号) 的中间的数据,打印出来 一.使用FileInputStream处理 FileInputStream:是java中的字节输入流,就是通过字节的形式进行读取

  • Java使用Socket通信传输文件的方法示例

    本文实例讲述了Java使用Socket通信传输文件的方法.分享给大家供大家参考,具体如下: 前面几篇文章介绍了使用Java的Socket编程和NIO包在Socket中的应用,这篇文章说说怎样利用Socket编程来实现简单的文件传输. 这里由于前面一片文章介绍了NIO在Socket中的应用,所以这里在读写文件的时候也继续使用NIO包,所以代码看起来会比直接使用流的方式稍微复杂一点点. 下面的示例演示了客户端向服务器端发送一个文件,服务器作为响应给客户端回发一个文件.这里准备两个文件E:/test/

随机推荐