java从服务器上下载资源到本地文件夹中

 

这里以从服务器上下载图片为例,photoUrl地址,filePath文件存放路径,fileName下载之后存放的文件名称(这里采用服务器上的图片名称),method请求方式。

话不多说直接上代码。

 

public static void main(String[] args) 
  { 
    String photoUrl = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";       //获取最后一个分隔符后面的图片名称
    String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));  
    System.out.println("fileName----"+fileName); 
    String filePath = "d:/photo";  
    File file = saveUrlAs(photoUrl, filePath , fileName,"GET");  
    System.out.println("filePath ----"+file); 
  } 

public static File saveUrlAs((String url,String filePath,String fileName,String method){ 
 
   //创建不同的文件夹目录 
   File file=new File(filePath); 
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现在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、付费专栏及课程。

余额充值