设为首页 - 加入收藏 焦点技术网
热搜:java
当前位置:首页 >

java读取远程共享文件

2014-02-25 23:45:00.0 java java  
导读:方式一:package example;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;imp...。。。

方式一:

package example;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 ReadShareFileSecond { public static void main(String[] args){  String smbJudge = "smb://test:test@192.168.1.1/temp/test.txt";  String localPath = "D:\\temp";  File file = readFromSmb(smbJudge,localPath);  removeFile(file); }  public static boolean removeFile(File file) {  return file.delete();   } /** ***       * 从smbJudge读取文件并存储到localpath指定的路径       *        * @param smbJudge       *            共享机器的文件,如smb://xxx:xxx@192.168.1.1/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码       * @param localpath       *            本地路径       * @return       */   public static File readFromSmb(String smbJudge, String localPath) {  File localfile = null;  InputStream bis = null;  OutputStream bos = null;  try {   SmbFile rmifile = new SmbFile(smbJudge);   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; }}
方式二:

package example;import jcifs.smb.SmbFile;import jcifs.smb.SmbFileInputStream;public class ReadShareFileFirst { public static void main(String[] args) {  try {   SmbFile smbFile = new SmbFile("http://test:test@192.168.1.1/temp/test.txt");   int length = smbFile.getContentLength();//得到文件的大小   byte buffer[] = new byte[length];   SmbFileInputStream in = new SmbFileInputStream(smbFile);//建立文件输入流   while((in.read(buffer))!=-1){    System.out.write(buffer);    System.out.println(buffer.length);   }   in.close();  } catch (Exception e) {   e.printStackTrace();  } }}



(编辑: u013361361)

网友评论
相关文章