java实现通过共享文件夹实现文件上传下载(附源码工具类)

1.简介 

      要实现文件上传下载基于smb协议。

       SMB(Server Message Block)通信协议是微软(Microsoft)和英特尔(Intel)在1987年制定的协议,主要是作为Microsoft网络的通讯协议。SMB 是在会话层(session layer)和表示层(presentation layer)以及小部分应用层(application layer)的协议。

       SMB使用了NetBIOS的应用程序接口 (Application Program Interface,简称API)。另外,它是一个开放性的协议,允许了协议扩展——使得它变得更大而且复杂;大约有65个最上层的作业,而每个作业都超过120个函数,甚至Windows NT也没有全部支持到,最近微软又把 SMB 改名为 CIFS(Common Internet File System)。

2.java实现方式共享文件夹操作

1.首先确保已设置好共享文件夹,局域网测试共享文件夹是否测试成功。网上有很多教程,这里不再叙述。

举例:现有A机(192.168.0.133)和B机(192.168.0.134),在同一局域网下,B机已设置共享文件夹。在A机首先windows+R键,调用命令窗口,输入\\192.168.0.134确保能访问到共享文件夹说明已设置成功,如不成功,请先设置共享文件夹。

2.需要用到jcifs-1.3.18.jar这个jar包。maven引用如下:

		<dependency>
		    <groupId>jcifs</groupId>
		    <artifactId>jcifs</artifactId>
		    <version>1.3.18</version>
		</dependency>

3.SubUtil实现类:

public class SmbUtil {
	/* 文件上传 
	 * String localFilePath:要上传的本地文件路径
	 * String path:远程服务器共享文件夹名称
	 * String username:远程服务器用户名
	 * String password:远程服务器密码
	 * */
	
    public static void upload(String localFilePath,String path,String username,String password) {
        InputStream in = null;
	    OutputStream out = null;
       try {
	    File localFile = new File(localFilePath);
	    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
//	    jcifs.Config.setProperty("jcifs.smb.lmCompatibility","2");
	    String remoteUrl = "smb://192.168.0.134" + path + (path.endsWith("/") ? "" : "/");
	    SmbFile remoteFile = new SmbFile(remoteUrl + "/" + localFile.getName(),auth);
	    remoteFile.connect(); 
	    in = new BufferedInputStream(new FileInputStream(localFile));
	    out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
	    byte[] buffer = new byte[4096];
	    int len = 0;
	            while ((len = in.read(buffer, 0, buffer.length)) != -1) {
	                out.write(buffer, 0, len);
	            }
	            out.flush();
	        }
	        catch (Exception e) {
	        	e.printStackTrace();
	        }
	        finally {
	            try {
	                if(out != null) {
	                    out.close();
	                }
	                if(in != null) {
	                    in.close();
	                }
	            }
	            catch (Exception e) {}
	        }
    }
	/* 文件下载 
	 * String localFilePath:本地文件夹
	 * String path:远程服务器共享文件名
	 * String username:远程服务器用户名
	 * String password:远程服务器密码
	 * */
    public static void download(String localFilePath,String path, String username,String password){
    	InputStream in = null;
 	    OutputStream out = null;
      try {
 	    File localFile = new File(localFilePath);
 	    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
// 	    jcifs.Config.setProperty("jcifs.smb.lmCompatibility","2");
 	    String remoteUrl = "smb://192.168.0.134" + path + (path.endsWith("/") ? "" : "/");
 	    SmbFile remoteFile = new SmbFile(remoteUrl + "/" + localFile.getName(),auth);
 	    remoteFile.connect(); 
 	    in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
 	    out = new BufferedOutputStream(new FileOutputStream(localFile));
 	    byte[] buffer = new byte[4096];
 	    int len = 0;
 	            while ((len = in.read(buffer, 0, buffer.length)) != -1) {
 	                out.write(buffer, 0, len);
 	            }
 	            out.flush();
 	        }
 	        catch (Exception e) {
 	        	e.printStackTrace();
 	        }
 	        finally {
 	            try {
 	                if(out != null) {
 	                    out.close();
 	                }
 	                if(in != null) {
 	                    in.close();
 	                }
 	            }
 	            catch (Exception e) {}
 	        }
    }

}

3.出现的异常

java.net.UnKnownHostException:..__MSBROWSE__..:

首先确保户号和密码完全正确。

错误写法:(用户名和密码不要拼接在remoteUrl里面。)

String remoteUrl = "smb://admin:admin@192.168.0.134" + path + "a.txt";

SmbFile remoteFile = new SmbFile(remoteUrl);

正确写法:

String username="admin";

String password="admin";

String remoteUrl = "smb:/192.168.0.134" + path + "a.txt";

 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);

SmbFile remoteFile = new SmbFile(remoteUrl);

 

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是Java实现获取文件夹内所有文件的绝对路径的完整源码: ```java import java.io.File; import java.util.ArrayList; import java.util.List; public class FolderFiles { public static void main(String[] args) { String folderPath = "/path/to/folder"; // Replace with the actual folder path List<String> filePaths = getAllFiles(folderPath); for (String filePath : filePaths) { System.out.println(filePath); } } public static List<String> getAllFiles(String folderPath) { List<String> filePaths = new ArrayList<>(); File folder = new File(folderPath); File[] files = folder.listFiles(); for (File file : files) { if (file.isFile()) { filePaths.add(file.getAbsolutePath()); } else if (file.isDirectory()) { filePaths.addAll(getAllFiles(file.getAbsolutePath())); } } return filePaths; } } ``` 在这个程序中,我们首先定义了一个文件夹的路径,然后调用getAllFiles方法获取该文件夹内所有文件的绝对路径。getAllFiles方法接受一个文件夹路径作为参数,返回一个包含所有文件绝对路径的列表。 在getAllFiles方法中,我们首先创建一个空的列表filePaths,然后使用Java的File类获取文件夹内所有文件的File对象。对于每个文件,我们判断它是一个文件还是一个文件夹。如果是文件,我们将它的绝对路径添加到filePaths列表中;如果是文件夹,我们递归调用getAllFiles方法获取该文件夹内所有文件的绝对路径,并将它们添加到filePaths列表中。最后,我们返回filePaths列表。 需要注意的是,如果文件夹路径不正确或者没有权限访问该文件夹,会抛出异常,需要在代码中进行异常处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值