/***
* 从局域网共享文件获取文件输入流
* @param remotePathAndFileName:共享的相对路径+文件名(含后缀),如:/qualityNotice/我们的祖国是花园.txt
* @return
*/
public static InputStream smbGetFile(String remotePathAndFileName){
String fullPath = "";
String lastChar = remotePathAndFileName.substring(0,1);
SmbFileInputStream in = null;
String ip = CommonManage.smbgetConnectIp();
String user = CommonManage.smbgetConnectUser();
String pass = CommonManage.smbgetConnectPass();
String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
if (lastChar.equals("/") || lastChar.equals("\\"))
fullPath = targetPath + remotePathAndFileName.trim();
else
fullPath = targetPath + "/" + remotePathAndFileName.trim();
SmbFile file = null;
System.out.println("下载路径:"+fullPath);
try {
file = new SmbFile(fullPath);
System.out.println("下载文件:"+file);
in = new SmbFileInputStream(file);
System.out.println("输出流:"+in);
} catch (Exception e) {
try {
if (in==null){
fullPath = targetPath + "/noPic.jpg";
file = new SmbFile(fullPath);
in = new SmbFileInputStream(file);
}
} catch (Exception e2) {
}
}
return in;
}
/***
* 从局域网共享文件下载文件到指定目录
* @param remotePathAndFileName 共享的相对路径+文件名(含后缀),如:/qualityNotice/我们的祖国是花园.txt
* @param localPath 要存放的本地目录
* @param realFileName 下载后的文件名(含后缀),如果为空则按共享文件夹上的文件名(uuid),如果指定了按指定的,例如:我们的祖国是花园.txt
* @return
*/
public static fileUploadReturn smbDownloadFile(String remotePathAndFileName,String localPath,String realFileName){
InputStream in = null;
OutputStream out = null;
fileUploadReturn fur = new fileUploadReturn();
String ip = CommonManage.smbgetConnectIp();
String user = CommonManage.smbgetConnectUser();
String pass = CommonManage.smbgetConnectPass();
try{
File localFile = null;
String targetFullPath = "";
String oneChar = remotePathAndFileName.substring(0,1);
String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
if (oneChar.equals("/") || oneChar.equals("\\"))
targetFullPath = targetPath + remotePathAndFileName.trim();
else
targetFullPath = targetPath + "/" + remotePathAndFileName.trim();
SmbFile smbFile = new SmbFile(targetFullPath);
if (smbFile==null){
targetFullPath = targetPath + "/noPic.jpg";
smbFile = new SmbFile(targetFullPath);
}
oneChar = localPath.substring(localPath.length()-1);
if (!oneChar.equals("/") && !oneChar.equals("\\"))
localPath = localPath + File.separator;
File existsTest = new File(localPath);
if (!existsTest.exists())
existsTest.mkdirs();
String fileName = smbFile.getName();
if (realFileName.equals("") || realFileName==null){
localFile = new File(localPath + fileName);
fur.setSysFileName(fileName);
} else {
localFile = new File(localPath + realFileName);
fur.setSysFileName(realFileName);
}
in = new SmbFileInputStream(smbFile);
out = new FileOutputStream(localFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
fur.setSucess(true);
fur.setMsg("下载成功。");
} catch (Exception e) {
fur.setSucess(false);
fur.setMsg("下载失败。");
e.printStackTrace();
return fur;
} finally {
try {
if (out!=null)
out.close();
if (in!=null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fur;
}
/***
* 判断局域网共享文件下的文件是否存在
* @param remotePathAndFile 共享电脑路径加文件 如/qualityNotice/a213dadfdc35z432d2yu9.txt
* @return
*/
public static boolean smbCheckFileExists(String remotePathAndFile){
boolean isExists = true;
String ip = CommonManage.smbgetConnectIp();
String user = CommonManage.smbgetConnectUser();
String pass = CommonManage.smbgetConnectPass();
try {
String targetFullPath = "";
String oneChar = remotePathAndFile.substring(0,1);
String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
if (oneChar.equals("/") || oneChar.equals("\\"))
targetFullPath = targetPath + remotePathAndFile.trim();
else
targetFullPath = targetPath + "/" + remotePathAndFile.trim();
SmbFile checkFile = new SmbFile(targetFullPath);
if (checkFile.exists() && checkFile.isFile()){
} else
isExists = false;
} catch (Exception e) {
isExists = false;
e.printStackTrace();
}
return isExists;
}
/***
* 把本地磁盘中的文件上传到局域网共享文件下
* @param remotePath 共享电脑路径 如:/qualityNotice
* @param localPathAndFileName 完整的本地的文件路径,如 C:/htwy/我们的祖国是花园.txt
*/
public static fileUploadReturn smbUploadFile(String remotePath, String localPathAndFileName){
InputStream in = null;
OutputStream out = null;
fileUploadReturn fur = new fileUploadReturn();
String ip = CommonManage.smbgetConnectIp();
String user = CommonManage.smbgetConnectUser();
String pass = CommonManage.smbgetConnectPass();
try {
File localFile = new File(localPathAndFileName);
String targetFullPath = "";
String oneChar = remotePath.substring(0,1);
String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
if (oneChar.equals("/") || oneChar.equals("\\"))
targetFullPath = targetPath + remotePath.trim();
else
targetFullPath = targetPath + "/" + remotePath.trim();
SmbFile existsTest = new SmbFile(targetFullPath);
if (!existsTest.exists())
existsTest.mkdirs();
String fileExt = localPathAndFileName.substring(localPathAndFileName.lastIndexOf('.') + 1);
if (fileExt.equals("") || fileExt==null){
fur.setSucess(false);
fur.setMsg("文件名应包含后缀名。");
} else {
String sysFileName = CommonManage.getUuid() + "." + fileExt;
SmbFile remoteFile = null;
oneChar = targetFullPath.substring(targetFullPath.length()-1);
if (oneChar.equals("/") || oneChar.equals("\\"))
remoteFile = new SmbFile(targetFullPath + sysFileName);
else
remoteFile = new SmbFile(targetFullPath + "/" + sysFileName);
in = new FileInputStream(localFile);
out = new SmbFileOutputStream(remoteFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
fur.setSucess(true);
fur.setSysFileName(sysFileName);
fur.setMsg("上传成功。");
}
} catch (Exception e) {
fur.setSucess(false);
fur.setMsg("上传失败。");
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fur;
}
/***
* 把本地磁盘中的文件上传到局域网共享文件下
* @param remotePath 共享电脑路径 如:/qualityNotice
* @param inputStream 文件的InputStream形式
* @param fileName 本地的文件名(含后缀名),如:我们的祖国是花园.txt
* @return
*/
public static fileUploadReturn smbUploadFile(String remotePath, InputStream inputStream, String fileName){
InputStream in = null;
OutputStream out = null;
fileUploadReturn fur = new fileUploadReturn();
String ip = CommonManage.smbgetConnectIp();
String user = CommonManage.smbgetConnectUser();
String pass = CommonManage.smbgetConnectPass();
try {
String targetFullPath = "";
String oneChar = remotePath.substring(0,1);
String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
if (oneChar.equals("/") || oneChar.equals("\\"))
targetFullPath = targetPath + remotePath.trim();
else
targetFullPath = targetPath + "/" + remotePath.trim();
SmbFile existsTest = new SmbFile(targetFullPath);
if (!existsTest.exists())
existsTest.mkdirs();
String fileExt = fileName.substring(fileName.lastIndexOf('.') + 1);
if (fileExt.equals("") || fileExt==null){
fur.setSucess(false);
fur.setMsg("文件名应包含后缀名。");
} else {
String sysFileName = CommonManage.getUuid() + "." + fileExt;
SmbFile remoteFile = null;
oneChar = targetFullPath.substring(targetFullPath.length()-1);
if (oneChar.equals("/") || oneChar.equals("\\"))
remoteFile = new SmbFile(targetFullPath + sysFileName);
else
remoteFile = new SmbFile(targetFullPath + "/" + sysFileName);
in = inputStream;
out = new SmbFileOutputStream(remoteFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
fur.setSucess(true);
fur.setSysFileName(sysFileName);
fur.setMsg("上传成功。");
}
} catch (Exception e) {
fur.setSucess(false);
fur.setMsg("上传失败。");
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fur;
}
/***
* 把本地磁盘中的文件上传到局域网共享文件下并覆盖文件
* @param remotePath 共享电脑路径 如:/qualityNotice
* @param inputStream 文件的InputStream形式
* @param fileName 覆盖的文件名(含后缀名),如:我们的祖国是花园.txt
* @return
*/
public static fileUploadReturn smbUploadFileEdit(String remotePath, InputStream inputStream, String fileName){
InputStream in = null;
OutputStream out = null;
fileUploadReturn fur = new fileUploadReturn();
String ip = CommonManage.smbgetConnectIp();
String user = CommonManage.smbgetConnectUser();
String pass = CommonManage.smbgetConnectPass();
try {
String targetFullPath = "";
String oneChar = remotePath.substring(0,1);
String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
if (oneChar.equals("/") || oneChar.equals("\\"))
targetFullPath = targetPath + remotePath.trim();
else
targetFullPath = targetPath + "/" + remotePath.trim();
SmbFile existsTest = new SmbFile(targetFullPath);
if (!existsTest.exists())
existsTest.mkdirs();
String fileExt = fileName.substring(fileName.lastIndexOf('.') + 1);
if (fileExt.equals("") || fileExt==null){
fur.setSucess(false);
fur.setMsg("文件名应包含后缀名。");
} else {
String sysFileName = fileName;
SmbFile remoteFile = null;
oneChar = targetFullPath.substring(targetFullPath.length()-1);
if (oneChar.equals("/") || oneChar.equals("\\"))
remoteFile = new SmbFile(targetFullPath + sysFileName);
else
remoteFile = new SmbFile(targetFullPath + "/" + sysFileName);
in = inputStream;
out = new SmbFileOutputStream(remoteFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
fur.setSucess(true);
fur.setSysFileName(sysFileName);
fur.setMsg("上传成功。");
}
} catch (Exception e) {
fur.setSucess(false);
fur.setMsg("上传失败。");
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fur;
}
分享到:
相关推荐
Android读取SMB服务器资源或者下载资源文件
JAVA SMB共享文件夹操作文件jar支持smb2、smb3.rar
最近tomcat做了负载均衡,碰到一个难题,就是上传文件的共享问题。 最后使用的解决方案是使用一个共享地址,该地址可以被多个服务器共同访问。...实现上传文件到网络共享文件夹和从网络共享文件夹下载文件
最近tomcat做了负载均衡,碰到一个难题,就是上传文件的共享问题。 最后使用的解决方案是使用一个共享地址,该地址可以被多个服务器共同访问。...实现上传文件到网络共享文件夹和从网络共享文件夹下载文件
SMB文件共享需要的Jar包,实现文件上传、下载。
java操作网络共享 smb2协议 使用smbj.jar访问共享文件夹,解决SMB2/SMB3共享文件夹访问 使用smbj.jar访问共享文件夹,支持SMB2/SMB3,用于解决使用jcifs.jar不支持SMB2/SMB3的问题
使用smbj.jar访问共享文件夹,支持SMB2/SMB3,用于解决使用jcifs.jar不支持SMB2/SMB3的问题
FTP 服务器是一种文件传输协议服务器,允许用户通过网络上传和下载文件。使用 Linux 系统搭建 FTP 服务器可以提高文件传输的效率和安全性。 四、搭建 SMB 服务器 SMB 服务器是一种服务器消息块服务器,允许用户...
JAVA通过SMB的方式下载、上传远程Linux服务器的文件
详尽介绍SMB协议,协议的实现,文件设备的操作,设备通信,上传,下载,删除,更名等。
它实现了客户端SMB / CIFS协议(SMB1和SMB2),该协议允许您的Python应用程序访问文件和从SMB / CIFS共享文件夹(例如Windows文件共享和Samba文件夹)中传输文件。 主要项目网站: : 文档: : 问题追踪器:请...
如何实现在ftp自动上传下载文件脚本呢?想必还有很多朋友都不太了解吧,下面是ftp自动上传下载文件脚本方法,希望对朋友们有些帮助
免费的psftp软件,可用于Linux系统,上传下载文件到windows,并不需要你创建smb服务。
上传解压后直接执行./install即可 配置文件在/etc/samba/smb.conf,默认的samba密码见install.sh
已经进入了smb模式,有些像ftp,敲help可以查看相关命令,get,put可以下载和上传文件。 五. 小结 以上几步,对于简单使用ubuntu和windows的文件共享于互访已经足够,更详细的资料可以参考:...
# vim /etc/samba/smb.conf #设置smb.conf配置文件 图5:设置smb.conf配置文件 [global] workgroup = WORKGROUP #设置主机工作组 server string = Samba Server Version %v #samba服务注释 log level = 1 #设置日志...
的X PLORE可以在Picasa相册列表图片,下载,创建相册,上传和删除照片,编辑字幕。 SQLite数据库查看器 的X PLORE可以显示SQLite数据库文件(那些。db扩展名)作为表的扩展列表中,用数据库条目的行和列的每个表包含...
例如,通过修改`local_root`可以设定本地用户的FTP根目录,`local_umask`定义了本地用户上传文件的默认权限。 要启用`vsftpd`服务,可以通过`yum`安装,然后用`service vsftpd start/restart`或`/etc/init.d/vsftpd...
SMBInterface是SMB网络(Samba或Windows)的Web界面-与Windows中的“网上邻居”图标相似。 它允许用户浏览网络,上传和下载文件,以及将SMB弹出消息发送到各种主机。
libcurl 是一个免费的客户端 URL 传输库,支持 cookie、DICT、FTP、...该库支持文件 URI 方案、SFTP、Telnet、TFTP、文件传输恢复、FTP 上传、基于 HTTP 表单的上传、HTTPS 证书、LDAPS、代理和用户增强密码身份验证。