java打开远程共享文件
2013-08-29 16:52
295 查看
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();
}
}
}
例子二
Java代码
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();
}
}
}
例子二
Java代码
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远程共享文件访问jcifs解决密码中特殊符号问题
- java程序读取远程机器共享文件
- 文件共享服务器的超简单玩法教程,打开网页就能远程访问
- OpenSUSE Leap 42.3下通过Firefox Opera Chromium浏览器直接执行java应用程序(打开java jnlp文件)实现在服务器远程虚拟控制台完成远程管理的方法
- java读取远程共享文件
- java访问远程共享文件
- java远程访问局域网内其他机器的共享文件
- java远程共享文件访问jcifs解决密码中特殊符号问题
- Ubuntu下通过Firefox Opera Chromium浏览器直接执行java应用程序(打开java jnlp文件)实现在服务器远程虚拟控制台完成远程管理的方法
- java 读取远程共享文件
- java远程共享文件访问jcifs解决密码中特殊符号问题
- java获取远程文件(保证文件的完整性,不会出现无法打开的情况)
- java 将远程共享的文件复制到本地
- ios开发之使用UIWebView打开本地的HTML、txt、PDF、PPT文件、打开远程的共享资源
- JAVA作业—从命令行输入两个参数打开文件并计算代码行数 .
- JAVA Dialog 文件打开和保存程序
- java下载上传远程文件
- 附加自动命名的数据库 但失败。已存在同名的数据库 或指定的文件无法打开或位于 unc 共享目录中。【解决方法】
- java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息