Java读取共享文件夹下面的文件(利用smb协议)
一、开启smb协议
1.1 控制面板->程序->启用或关闭window功能->smb1.0
二、设置共享文件
2.1 选择你想要共享的文件夹设置everyone或者指定的用户
授权给某个用户。需要密码
三、准备好对应的jar
百度云盘:https://pan.baidu.com/s/1TzAtUVRJl8_IcZCZ-JCO1w
提取码:sv6h
引入jar
四、代码
1 /** 2 * 读单个文件 3 * */ 4 @PostMapping("/queryCard/getfile") 5 public Response queryFile(@RequestBody String fileName) throws IOException { 6 String ip = sysOptionService.getString("FileServerIpSetting").trim(); 7 String user = sysOptionService.getString("FileServerUserSetting").trim(); 8 String pass = sysOptionService.getString("FileServerPassSetting").trim(); 9 String path = sysOptionService.getString("FileServerPathSetting").trim(); 10 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, pass); 11 String filePath = getSmFileStr() +"/"+ fileName; 12 log.info("获取共享服务器文件,路径为:" + filePath); 13 SmbFile smbFile = new SmbFile(getSmFileStr() +"/"+ fileName, auth); 14 BufferedReader reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(smbFile), StandardCharsets.UTF_8));//如果有中文乱码,换成gbk试试 15 int tempchar = 0; 16 while ((tempchar = reader.read()) != -1) { 17 if (((char) tempchar) != '\r') { 18 System.err.print((char) tempchar); 19 } 20 } 21 reader.close(); 22 return Response.success("ok"); 23 } 24 25 /** 26 * 读多个文件 27 * */ 28 @PostMapping("/queryCard/getfiles") 29 public Response getfiles(@RequestBody String fileName) throws IOException { 30 String ip = sysOptionService.getString("FileServerIpSetting").trim(); 31 String user = sysOptionService.getString("FileServerUserSetting").trim(); 32 String pass = sysOptionService.getString("FileServerPassSetting").trim(); 33 String path = sysOptionService.getString("FileServerPathSetting").trim(); 34 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, pass); 35 String filePath = getSmFileStr() +"/"+ fileName; 36 log.info("获取共享服务器多个文件,路径为:" + filePath); 37 SmbFile smbFile = new SmbFile(getSmFileStr() +"/", auth); 38 System.out.println("SMB files -" + smbFile.listFiles()); 39 return Response.success("ok"); 40 } 41 42 /** 43 * 删除文件 44 * */ 45 @PostMapping("/queryCard/deletefile") 46 public Response deletefile(@RequestBody String fileName) throws IOException { 47 String ip = sysOptionService.getString("FileServerIpSetting").trim(); 48 String user = sysOptionService.getString("FileServerUserSetting").trim(); 49 String pass = sysOptionService.getString("FileServerPassSetting").trim(); 50 String path = sysOptionService.getString("FileServerPathSetting").trim(); 51 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, pass); 52 String filePath = getSmFileStr() +"/"+ fileName; 53 log.info("删除共享服务器多个文件,路径为:" + filePath); 54 SmbFile smbFile = new SmbFile(getSmFileStr() +"/"+ fileName, auth); 55 if (smbFile != null){ 56 smbFile.delete(); 57 } 58 return Response.success("ok"); 59 } 60 61 /** 62 * 存文件 63 * */ 64 @PostMapping("/queryCard/savefile") 65 public Response queryFile(@RequestParam("file") MultipartFile file) { 66 InputStream in = null; 67 OutputStream out = null; 68 String ip = sysOptionService.getString("FileServerIpSetting").trim(); 69 String user = sysOptionService.getString("FileServerUserSetting").trim(); 70 String pass = sysOptionService.getString("FileServerPassSetting").trim(); 71 String path = sysOptionService.getString("FileServerPathSetting").trim(); 72 try { 73 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, pass); 74 SmbFile checkRemotedir = new SmbFile(getSmFileStr(), auth); 75 if (!checkRemotedir.exists()) {//路径不存在,新建文件夹 76 checkRemotedir.mkdirs(); 77 } 78 File localFile = multipartFileToFile(file); 79 String fileName = localFile.getName(); 80 String filePath = getSmFileStr() +"/"+ fileName; 81 log.info("保存共享服务器文件,路径为:" + filePath); 82 SmbFile remoteFile = new SmbFile(getSmFileStr()+ "/" + fileName, auth); 83 in = new BufferedInputStream(new FileInputStream(localFile)); 84 out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); 85 byte[] buffer = new byte[1024]; 86 while (in.read(buffer) != -1) { 87 out.write(buffer); 88 buffer = new byte[1024]; 89 } 90 out.flush(); 91 } catch (Exception e) { 92 e.printStackTrace(); 93 } finally { 94 try { 95 out.close(); 96 in.close(); 97 } catch (IOException e) { 98 e.printStackTrace(); 99 } 100 } 101 return Response.success("ok"); 102 } 103 104 105 public static File multipartFileToFile(MultipartFile multiFile) { 106 // 获取文件名 107 String fileName = multiFile.getOriginalFilename(); 108 // 获取文件后缀 109 String prefix = fileName.substring(fileName.lastIndexOf(".")); 110 // 若须要防止生成的临时文件重复,能够在文件名后添加随机码 111 try { 112 File file = File.createTempFile(fileName, prefix); 113 multiFile.transferTo(file); 114 return file; 115 } catch (Exception e) { 116 e.printStackTrace(); 117 } 118 return null; 119 } 120 121 /** 122 * 获取smb文件url 123 * 按照年、月、日文件夹存取 124 * */ 125 private String getSmFileStr(){ 126 String ip = sysOptionService.getString("FileServerIpSetting").trim(); 127 String user = sysOptionService.getString("FileServerUserSetting").trim(); 128 String pass = sysOptionService.getString("FileServerPassSetting").trim(); 129 String path = sysOptionService.getString("FileServerPathSetting").trim(); 130 if (StringUtils.isEmpty(ip)){ 131 throw new AttachmentException("文件服务器IP不能为空,请联系管理员在系统设置中配置!"); 132 } 133 if (StringUtils.isEmpty(user)){ 134 throw new AttachmentException("文件服务器用户不能为空,请联系管理员在系统设置中配置!"); 135 } 136 if (StringUtils.isEmpty(pass)){ 137 throw new AttachmentException("文件服务器密码不能为空,请联系管理员在系统设置中配置!"); 138 } 139 if (StringUtils.isEmpty(path)){ 140 throw new AttachmentException("文件服务器路径不能为空,请联系管理员在系统设置中配置!"); 141 } 142 //创建远程文件对象 smb://用户名:密码@ip地址/共享的路径/... 143 String smFileStr = "smb://"+user+":"+pass+"@"+ip+path+"/"+getToYearDateStr()+"/"+getToMonthDateStr()+"/"+getToDayDateStr(); 144 return smFileStr; 145 } 146 147 /** 148 * 获取当天 149 * */ 150 private String getToDayDateStr(){ 151 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 152 String str = sdf.format(new Date()); 153 return str; 154 } 155 156 /** 157 * 获取当月 158 * */ 159 private String getToMonthDateStr(){ 160 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); 161 String str = sdf.format(new Date()); 162 return str; 163 } 164 165 /** 166 * 获取当年 167 * */ 168 private String getToYearDateStr(){ 169 SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); 170 String str = sdf.format(new Date()); 171 return str; 172 }
代码中涉及到四个参数(服务器ip、用户、密码、路径),分别存在数据库中。方便后期维护,比如整体挪动文件夹。
五、测试
用postman测试,能成功
yian