java上传文件的路径在Windows和liunx中不同

我本来是String mp3ImageFullPath = mp3ImageSavePath + ("\\" + mp3FileLabel + ".jpg");可以在Windows可以看到,但是liunx上不行.

后面改成String mp3ImageFullPath = mp3ImageSavePath + ( System.getProperty("file.separator") + mp3FileLabel + ".jpg");就可以了.

file.separator是文件分隔符

要实现在Linux系统上上传文件Windows服务器上,可以通过使用Java的SMB协议相关的库来实现。 SMB(Server Message Block)协议是Windows操作系统上的一种网络通信协议,用于在网络上共享文件、打印机等资源。为了在Linux系统上使用SMB协议来上传文件,可以使用jcifs库。 首先,需要在Java项目引入jcifs库的依赖,以便使用SMB相关的类和方法。具体的引入方式可以根据使用的构建工具来实现,比如使用Maven可以在pom.xml文件添加以下依赖: ``` <dependency> <groupId>jcifs</groupId> <artifactId>jcifs</artifactId> <version>1.3.19</version> </dependency> ``` 接下来,需要编写Java代码来实现上传文件的逻辑。以下是一个简单的样例代码: ```java import jcifs.smb.*; import java.io.*; public class FileUploader { public static void main(String[] args) { String sourceFilePath = "/path/to/local/file.txt"; // 本地文件路径 String destinationFilePath = "smb://windows_server_ip/shared_folder/remote_file.txt"; // Windows服务器共享文件夹路径 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "username", "password"); // Windows服务器的用户名和密码 SmbFile sourceFile; try { sourceFile = new SmbFile(sourceFilePath); SmbFile destinationFile = new SmbFile(destinationFilePath, auth); destinationFile.createNewFile(); // 创建目标文件 // 打开文件输入流和输出流,进行文件拷贝 try (InputStream in = new BufferedInputStream(new FileInputStream(sourceFile)); OutputStream out = new BufferedOutputStream(new SmbFileOutputStream(destinationFile))) { byte[] buffer = new byte[4096]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } System.out.println("文件上传成功!"); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } } } ``` 在上述代码,需要替换`sourceFilePath`为待上传的本地文件路径,`destinationFilePath`为Windows服务器共享文件夹的路径,以及`username`和`password`为Windows服务器的用户名和密码。 通过以上代码,就可以实现将文件从Linux系统上上传到Windows服务器上的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值