资源简介
JAVA通过SMB的方式下载、上传远程Linux服务器的文件

代码片段和文件信息
package com.yzj.demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;
import com.yzj.log.LogFactory;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class RemoteAccessData {
private static Logger logger = LogFactory.getInstance(RemoteAccessData.class);
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
smbGet(“smb://username:password@11.132.3.13/prlife_ls_yanshou1_image/001/2013/12/05/10/00000053085901001/1/00000053085901001001.tif“ “D:/download“);
}
/**
* 路径格式:smb://192.168.75.204/test/新建 文本文档.txt
* smb://username:password@192.168.0.77/test
* @param remoteUrl
* 远程路径
* @param localDir
* 要写入的本地路径
*/
public static void smbGet(String remoteUrl String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if (remoteFile != null && remoteFile.exists()) {
String fileName = remoteFile.getName();
File localFile = new File(localDir + File.separator + fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[1024];
}
} else {
// 文件不存在
logger.info(remoteUrl + “ 文件不存在!“);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 向共享目录上传文件
* @param remoteUrl
* @param localFilePath
*/
public static void smbPut(String remoteUrl String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl + “/“ + fileName);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 394990 2014-08-05 17:36 jcifs-1.3.14.jar
文件 3795 2014-08-05 18:15 RemoteAccessData.java
----------- --------- ---------- ----- ----
398785 2
相关资源
- 微博系统(Java源码,servlet+jsp),适
- java串口通信全套完整代码-导入eclip
- jsonarray所必需的6个jar包.rar
- 三角网构TIN生成算法,Java语言实现
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
评论
共有 条评论