/***
* 从局域网共享文件获取文件输入流
* @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包,实现文件上传、下载。
使用smbj.jar访问共享文件夹,支持SMB2/SMB3,用于解决使用jcifs.jar不支持SMB2/SMB3的问题
java操作网络共享 smb2协议 使用smbj.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用于上传和下载文件。在Linux中,常用的FTP服务器软件有vsftpd: 1. 安装vsftpd:`sudo apt-get install vsftpd` 2. 配置文件:`/etc/vsftpd.conf`,设置用户权限、匿名访问等。 3. 启动服务:`sudo service ...
SMB是一种广泛使用的网络文件共享协议,允许不同操作系统之间的文件和打印机共享。由于PHP不阻止SMB URL的加载,攻击者可以利用这一点,通过创建一个允许匿名访问的SMB共享,来包含并执行远程的PHP代码。 攻击场景...
在Linux和Windows系统上分别安装相应的客户端,将文件上传到云端,然后在另一端下载。 5. **网络共享软件**: - 还有一些专门用于跨平台文件传输的软件,如Resilio Sync (BitTorrent Sync)、Syncthing等。这些软件...
这个过程与下载类似,但使用`mput`命令来上传文件,如下所示: ```bash #!/bin/bash ftp -n ! open 192.168.1.171 user guest 123456 binary hash cd /home/data lcd /home/databackup prompt mput * close bye! ! ...
免费的psftp软件,可用于Linux系统,上传下载文件到windows,并不需要你创建smb服务。
上传解压后直接执行./install即可 配置文件在/etc/samba/smb.conf,默认的samba密码见install.sh
在常规的RDP会话中,用户可能需要手动上传文件,这可能涉及到复杂的步骤,比如打开文件资源管理器,选择文件,然后发送到远程桌面。 描述中提到的“非常实用的小工具”可能是一个专门优化了文件传输功能的RDP客户端...
已经进入了smb模式,有些像ftp,敲help可以查看相关命令,get,put可以下载和上传文件。 五. 小结 以上几步,对于简单使用ubuntu和windows的文件共享于互访已经足够,更详细的资料可以参考:...
FTP服务提供文件上传和下载功能。主配置文件`/etc/vsftpd/vsftpd.conf`和用户权限文件(如`/etc/vsftpd/ftpusers`,`/etc/vsftpd/user_list`)控制访问权限。匿名用户通常是`ftp`和`anonymous`,匿名访问时,默认...