java 远程文件操作_java远程文件操作 - osc_88djj30s的个人空间 - OSCHINA - 中文开源技术交流社区...

有时在项目中,会有专门的文件服务器(windows),这个时候我们需要对文件进行操作时,就不能像操作本地文件那样操作文件服务器的文件。这时候就可以用SmbFile来操作了。 首先添加jar包,maven中添加如下代码:

jcifs

jcifs

1.3.17

一般通过文件服务器操作都需要账号密码,这时候需要设置下最基本的设置,

public static final String fileRoot = "smb://remotename:remotepassword@";

public InputStream getFile(String path) throws IOException {

SmbFile smbFile = new SmbFile(fileRoot+path);

return smbFile.getInputStream();

}

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();

}

public static List getAttachmentFiles(String remoteDirectory)

{

List attachmentPOs = new ArrayList();

try {

SmbFile file = new SmbFile(fileRoot + remoteDirectory);

String[] files = file.list();

for(String name : files){

AttachmentPO AttachmentPO = new AttachmentPO();

AttachmentPO.setName(name);

AttachmentPO.setFile_path(remoteDirectory + name);

AttachmentPO.setRef_type(WikiConsts.ATTACHMENT_TYPE_DOWNLOAD);

attachmentPOs.add(AttachmentPO);

}

} catch (MalformedURLException e) {

logger.error("get SmbFile by directory return wrong, the remoteDirectory is" + remoteDirectory);

} catch (SmbException e) {

logger.error("get SmbFiles under directory return wrong, the remoteDirectory is" + remoteDirectory);

}

return attachmentPOs;

}

其它的一些操作方法和本地操作文件方法没什么大的区别,可以自行地研究这个SmbFile类里的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值