酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

java实现文件上传至samba,给出示例,samba的连接信息为samba: host: 172.16.51.202 sharename: shyj username: shyj password: shyj-2024

以下是一个使用 Java 实现将文件上传到 Samba 共享的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileOutputStream; public class SambaFileUpload { public static void main(String[] args) { String sambaUrl = "smb://shyj:shyj-2024@172.16.51.202/shyj/"; String localFilePath = "/your/local/file/path"; // 替换为本地文件的实际路径 uploadFile(sambaUrl, localFilePath); } public static void uploadFile(String sambaUrl, String localFilePath) { try { File localFile = new File(localFilePath); SmbFile sambaFile = new SmbFile(sambaUrl + localFile.getName()); InputStream inputStream = new FileInputStream(localFile); SmbFileOutputStream outputStream = new SmbFileOutputStream(sambaFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer))!= -1) { outputStream.write(buffer, 0, bytesRead); } inputStream.close(); outputStream.close(); System.out.println("File uploaded successfully."); } catch (IOException e) { e.printStackTrace(); } } } ``` 请确保将`/your/local/file/path`替换为要上传的本地文件的实际路径。还需要添加`jcifs`库的依赖。 [2024-11-14 15:21:12 | AI写代码神器 | 384点数解答]

相关提问