java从网络外链接下载文件并回传到其他网络服务器

  • 场景
    • 描述
    • 举例
  • 解决方案
  • 准备
  • 例子
    • 说明,如果是springboot项目
  • 测试
  • 其他更细粒度实现的文件上传至远程(网络)服务器例子

场景

描述

从网络地址1下文件,然后将文件上传给网络地址2

举例

  • 网络地址1上的文件:百度上任意找一张图片
  • 上传到网络地址2:http://47.97.154.171:8080/file/upload

该地址为一个接收文件上传的接口

解决方案

将网络地址1上的文件下载到本地,然后再上传至网络地址2.

准备

  • 网络地址1上的图片一张
    https://cj.jj20.com/2020/down.html?picurl=/up/allimg/1111/0Q91Q50307/1PQ9150307-7.jpg
  • 网络地址2上文件接收接口
    http://47.97.154.171:8080/file/upload
package com.xl.test.logtest.controller;import java.util.UUID;import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;@RestController
@RequestMapping("/file")
public class FileuploadController {@PostMapping("/upload")public String uploadFile(@RequestParam("file") MultipartFile file) {System.out.println("===================file.name======================"+file.getName());System.out.println("===================file.size======================"+file.getSize());return UUID.randomUUID().toString();}}

例子

package com.xl.test.json;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;public class RemoteFileTest {public static void main(String[] args) throws IOException {String sourcePath = "https://cj.jj20.com/2020/down.html?picurl=/up/allimg/1111/0Q91Q50307/1PQ9150307-7.jpg";String localTempPath = "E:\\localtempfile\\";String destinationPath = "http://47.97.154.171:8080/file/upload";URL url = new URL(sourcePath);URLConnection connection = url.openConnection();InputStream inputStream = connection.getInputStream();File dir = new File(localTempPath);if (!dir.exists()) {dir.mkdirs();}File file = new File(localTempPath+"test.jpg");FileOutputStream outputStream = new FileOutputStream(file);IOUtils.copy(inputStream, outputStream);// 通过指定header和body来创建HttpEntityHttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.MULTIPART_FORM_DATA);MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();// FileSystemResource有多个构造返方法body.add("file", new FileSystemResource(file)); // file值为文件服务器上传接口的参数HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(body, headers);// 调用template.postForEntity( )完成连接建立并发送文件到服务器RestTemplate template = getTemplate();ResponseEntity<String> response = template.postForEntity(destinationPath, requestEntity, String.class);String data = response.getBody();System.out.println("目标地址(上传接口)返回的数据data = "+data);// 删除locationPath中的临时文件 ,删除前先关闭流inputStream.close();outputStream.close();}private static RestTemplate getTemplate() {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setReadTimeout(180000);//单位为msfactory.setConnectTimeout(5000);RestTemplate template = new RestTemplate(factory);return template;}}

说明,如果是springboot项目

  • 生成 RestTemplate 实例使用@Bean注解方式,如下:
@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory) {return new RestTemplate(factory);}@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory() {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setReadTimeout(180000);factory.setConnectTimeout(5000);return factory;}
  • 使用RestTemplate 实例,直接使用@Autowired注解,如
@Autowiredprivate RestTemplate template;

测试

直接运行main方法即可

在网络地址2查看文件接收情况

其他更细粒度实现的文件上传至远程(网络)服务器例子

java上传文件到远程服务器(一)—HttpURLConnection方式

java上传文件到远程服务器(二)—HttpClient方式

【java从网络外链接下载文件并回传到其他网络服务器】相关推荐

  1. java 从网络Url中下载文件

    转自:http://blog.csdn.net/xb12369/article/details/40543649 /*** 从网络Url中下载文件* @param urlStr* @param fil ...

  2. java调用下载窗口_java 从网络Url中下载文件 java调用url接口

    /** * 从网络Url中下载文件 * @param urlStr * @param fileName * @param savePath * @throws IOException */ publi ...

  3. java下载网络文件_java 从网络Url中下载文件

    /** * 从网络Url中下载文件 * @param urlStr * @param fileName * @param savePath * @throws IOException */ publi ...

  4. HTML a链接下载文件之图片,文件,乱码等问题

    我们在做需求的时候,经常会遇到下载文件 前端下载文件一般分为两种方式: 使用 a 链接进行下载: <a herf="url" >下载</a> 向后端发送请求 ...

  5. Python 从 Excel 读取链接下载文件

    Python 从 Excel 读取链接下载文件 import os from urllib.parse import urljoin, quote, unquoteimport requests im ...

  6. java设置ContentType,设置下载文件名称

    java设置ContentType,设置下载文件名称 根据上传文件名设置ContentType 设置下载文件名称 根据上传文件名设置ContentType 几种常用上传文件如下: private St ...

  7. 前端通过url链接下载文件

    前端通过url链接下载文件 网上找到几个相关的方法,如下 // 方法一: const form = document.createElement('form'); form.setAttribute( ...

  8. 【Java】网络编程——多线程下载文件

    前言 多线程下载文件,比单线程要快,当然,线程不是越多越好,这和获取的源文件还有和网速有关. 原理:在请求服务器的某个文件时,我们能得到这个文件的大小长度信息,我们就可以下载此长度的某一个片段,来达到 ...

  9. jsp页面链接下载文件

    jsp中实现文件下载的最简单的方式是在网页上做超级链接,如: href= "music/abc.mp3"点击下载.但是这样服务器上的目录资源会直接暴露给最终用户,会给网站带来一些不 ...

最新文章

  1. Maven学习(一) - Maven基础
  2. 机器学习项目的备忘清单!
  3. 05后都上清华了!首批丘成桐数学领军人才名单发布,三位菲尔兹奖得主为其授课,周末就来学校报到...
  4. 2011年使用天正建筑8.0注册版(附注册机)
  5. 机器人把大龙拉出来_LECW4D1综述:塞拉斯一日三登赛场 IgNar机器人重现江湖
  6. Vue:利用Vue生成的网页,在浏览器中的标签页中的图标与标题怎么修改为自己的?
  7. Windows Phone 7 位图编程
  8. msm8974 camera driver添加新摄像头kernel hal修改
  9. 开户oracle监听,R12:银行账户开户人?
  10. Linux学习笔记-生成动态库(补充说明)
  11. CUDA内存分配、释放、传输,固定内存
  12. python后台架构Django教程——项目配置setting
  13. 【故障诊断预测】基于matlab FFT与DBN轴承故障诊断预测【含Matlab源码 1741期】
  14. linux6.5输错密码锁定,干货分享:解决redhat6.5登陆后跳回Login,循环登陆问题
  15. nfc pm3 模拟加密门禁卡_关于如何用PM3将有密门禁卡写入支持NFC的手机
  16. Golang(2)win10下安装Go语言
  17. python 登录新浪微博_Python模拟新浪微博登录
  18. ax200 兼容性问题 老路由器_我的华硕AX89X 160频宽和MU-MIMO问题,小米10测速-路由器交流...
  19. 赛门铁克SSL证书chrome不支持解决方法
  20. 中软国际实习 dzy15

热门文章

  1. 计算机在小学音乐研修心得,浅谈信息技术在中小学音乐教学中的应用
  2. 时间复杂度和空间复杂度(C站最详细的)
  3. 【oracle】 一千条记录查200到300的记录怎么查
  4. 如何以“API”接口的形式帮助电商商家解决货源及运营难题?(淘宝/京东/1688API接口的接入)
  5. 手机到底该怎么充电?想不到和之前自己了解的都不一样,涨知识了
  6. Python函数day12
  7. java代码实现amr格式转换为mp3格式
  8. Docker学习(4)——如何通过阿里云的镜像加速器快速拉取镜像到本地
  9. Android 获取U盘的卷标(支持中文卷标)
  10. 润和HH-SCDAYU200开发套件技术指南