java读取远程共享文件
来源:互联网 发布:生风灿烂的日子 知乎 编辑:程序博客网 时间:2024/05/17 01:18
Java代码
例子一
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class ReadShareFile {
public static void main(String[] args) {
try {
SmbFile smbFile = new SmbFile(
"smb://test:test@192.168.1.1/out/test.txt");
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();
} catch (Exception e) {
e.printStackTrace();
}
}
}
例子二
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class TestReadSmb {
public static void main(String[] args){
String smbMachine="smb://test:test@10.108.23.200/temp/test.txt";
String localPath="D:\\temp";
File file=readFromSmb(smbMachine,localPath);
removeFile(file);
}
/** ***
* 从smbMachine读取文件并存储到localpath指定的路径
*
* @param smbMachine
* 共享机器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码
* @param localpath
* 本地路径
* @return
*/
public static File readFromSmb(String smbMachine,String localpath){
File localfile=null;
InputStream bis=null;
OutputStream bos=null;
try{
SmbFile rmifile = new SmbFile(smbMachine);
String filename=rmifile.getName();
bis=new BufferedInputStream(new SmbFileInputStream(rmifile));
localfile=new File(localpath+File.separator+filename);
System.out.println("localfile=="+localfile);
bos=new BufferedOutputStream(new FileOutputStream(localfile));
int length=rmifile.getContentLength();
System.out.println("length=="+length);
byte[] buffer=new byte[length];
Date date=new Date();
bis.read(buffer);
bos.write(buffer);
Date end=new Date();
int time= (int) ((end.getTime()-date.getTime())/1000);
if(time>0)
System.out.println("用时:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");
} catch (Exception e){
System.out.println(e.getMessage());
}finally{
try {
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return localfile;
}
public static boolean removeFile(File file) {
return file.delete();
}
例子一
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class ReadShareFile {
public static void main(String[] args) {
try {
SmbFile smbFile = new SmbFile(
"smb://test:test@192.168.1.1/out/test.txt");
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();
} catch (Exception e) {
e.printStackTrace();
}
}
}
例子二
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class TestReadSmb {
public static void main(String[] args){
String smbMachine="smb://test:test@10.108.23.200/temp/test.txt";
String localPath="D:\\temp";
File file=readFromSmb(smbMachine,localPath);
removeFile(file);
}
/** ***
* 从smbMachine读取文件并存储到localpath指定的路径
*
* @param smbMachine
* 共享机器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码
* @param localpath
* 本地路径
* @return
*/
public static File readFromSmb(String smbMachine,String localpath){
File localfile=null;
InputStream bis=null;
OutputStream bos=null;
try{
SmbFile rmifile = new SmbFile(smbMachine);
String filename=rmifile.getName();
bis=new BufferedInputStream(new SmbFileInputStream(rmifile));
localfile=new File(localpath+File.separator+filename);
System.out.println("localfile=="+localfile);
bos=new BufferedOutputStream(new FileOutputStream(localfile));
int length=rmifile.getContentLength();
System.out.println("length=="+length);
byte[] buffer=new byte[length];
Date date=new Date();
bis.read(buffer);
bos.write(buffer);
Date end=new Date();
int time= (int) ((end.getTime()-date.getTime())/1000);
if(time>0)
System.out.println("用时:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");
} catch (Exception e){
System.out.println(e.getMessage());
}finally{
try {
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return localfile;
}
public static boolean removeFile(File file) {
return file.delete();
}
}
- java读取远程共享文件
- java读取远程共享文件
- java 读取远程共享文件
- java程序读取远程机器共享文件
- java读取远程文件
- java读取远程文件
- Java读取远程文件
- java读取远程文件
- java读取远程文件
- java打开远程共享文件
- java访问远程共享文件
- c# 建立局域网远程共享连接,读取共享文件
- JAVA实现远程文件读取!
- JAVA实现远程文件读取!
- JAVA实现远程文件读取
- java读取远程主机文件
- smb远程读取共享文件出现的问题
- java基于SMB协议读取共享文件
- HSJ实现(一)
- Java读取文件方法大全
- poj 1002 487-3279
- JAVA编程题全集(50题及答案)
- boost之shared_ptr
- java读取远程共享文件
- Oracle SGA多个部分的组成
- 选择
- 难得一见 十大经典Linux崩溃画面集锦
- java读取文件效率问题
- 3 Flash如何显示物体
- 跳过编译器,获取泛型参数的实际类型
- Android开发之Notification
- Apache common FTP 的使用