读书人

java怎么远程访问一个共享目录

发布时间: 2012-11-08 08:48:11 作者: rapoo

java如何远程访问一个共享目录

由于工作需要读取局域网中一台机器的 共享目录中的文件,需要jcifs-1.1.11.jar的支持,使用SMB协议协议,以下是实现了远程读取并复制到本地,然后删除本地文件的功能:

?

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://10.108.23.200/temp/说明文件.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);            bos=new BufferedOutputStream(new FileOutputStream(localfile));            int length=rmifile.getContentLength();            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) ...{            // TODO Auto-generated catch block            System.out.println(e.getMessage());                    }finally{            try {                bos.close();                bis.close();            } catch (IOException e) {//                // TODO Auto-generated catch block                e.printStackTrace();            }                    }        return localfile;    }    public static boolean removeFile(File file) {        return file.delete();    }}

?

1 楼 wangxuliangboy 2008-11-26 直接用URLCONNECTION 2 楼 sdh5724 2008-11-26 wangxuliangboy 写道直接用URLCONNECTION


你这么做访问不到吧, 如果他的server是linux, 应该需要特定的支持WINDOWS共享的包. 不过呢, 我不大建议使用这么的共享, 最好开个FTP服务, 来完成读/删除文件. SBM的这东西稍微不大那么靠普点. 毕竟这是个CRACK的协议. 3 楼 My_Choice 2008-12-16 <div class='quote_title'>beckdim 写道</div>
<div class='quote_div'>
<p>由于工作需要读取局域网中一台机器的 共享目录中的文件,需要<a href='http://jcifs.samba.org/src/'><span style='color: #075db3;'>jcifs-1.1.11.jar</span></a>的支持,使用<a href='http://www.yuanma.org/data/2006/0614/article_826.htm'><span style='color: #075db3;'>SMB协议</span></a>协议,以下是实现了远程读取并复制到本地,然后删除本地文件的功能:</p>
<p>?</p>
<pre name='code' class='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://10.108.23.200/temp/说明文件.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);
bos=new BufferedOutputStream(new FileOutputStream(localfile));
int length=rmifile.getContentLength();
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) ...{
// TODO Auto-generated catch block
System.out.println(e.getMessage());

}finally{
try {
bos.close();
bis.close();
} catch (IOException e) {
// // TODO Auto-generated catch block
e.printStackTrace();
}
}
return localfile;
}
public static boolean removeFile(File file) {
return file.delete();
}
}
</pre>
<p>?</p>
</div>
<p>?</p> 4 楼 hocus 2008-12-16 sdh5724 写道wangxuliangboy 写道直接用URLCONNECTION


你这么做访问不到吧, 如果他的server是linux, 应该需要特定的支持WINDOWS共享的包. 不过呢, 我不大建议使用这么的共享, 最好开个FTP服务, 来完成读/删除文件. SBM的这东西稍微不大那么靠普点. 毕竟这是个CRACK的协议.
ftp到本地
处理完删除
稳定可靠 5 楼 hackboyo 2009-02-19 Access is denied.
请问我报这个错是什么原因呢
我机子是XP的,远程机子是2003的 6 楼 myreligion 2009-02-19 mount到本地不行?windows上做个磁盘映射,映射成本地的一块磁盘,例如K盘。 7 楼 ylz4647 2009-05-27 请问楼主,如何访问整个共享目录,查看指定目录下的子目录和文件?

读书人网 >软件架构设计

热点推荐