当前位置: 首页 > news >正文

java通过接口转发文件(上传下载)

java接口转发上传的文件

@RequestMapping(value = "/XXXX/fileUpload", method = RequestMethod.POST)
public String getFileUpload2(@RequestParam("file") MultipartFile file, HttpServletRequest request) 
public static String hotMapPost3(String url, String jsonParams,String dddd, MultipartFile file, HttpServletRequest request) {JSONObject result = null;try {MultiValueMap<String,Object> params = new LinkedMultiValueMap<>();ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {@Overridepublic String getFilename() {return file.getOriginalFilename();}};params.add("file",resource);RestTemplate restTemplate = new RestTemplate();HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.add("Content-Type", "multipart/form-data");httpHeaders.add("userLoginToken", dddd);String orgCode = request.getHeader("orgCode");String orgName = request.getHeader("orgName");httpHeaders.add("orgCode", orgCode);httpHeaders.add("orgName", orgName);HttpEntity<MultiValueMap<String,Object>> stringHttpEntity2 = new HttpEntity<>(params, httpHeaders);result = restTemplate.postForObject(url , stringHttpEntity2, JSONObject.class);} catch (Exception e) {log.info(e.toString());}if(null == result){return  null;}else {return result.toJSONString();}}

java接口转发下载的文件

public static void postDownLoadStream(String uri, String jsonPara, HttpServletResponse response, Map<String, String> header) {HttpURLConnection connection = null;log.info("下载类型接口调用开始: uri = {}", uri);String result = "";long currentTimeMillisStart = System.currentTimeMillis();try {URL urlNet = new URL(uri);connection = (HttpURLConnection) urlNet.openConnection();connection.setRequestMethod("POST");connection.setDoInput(true);connection.setDoOutput(true);connection.setUseCaches(false);connection.setConnectTimeout(20000);connection.setReadTimeout(20000);connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("Connection", "Keep-Alive");connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");if (header != null) {Iterator<Map.Entry<String, String>> iterator = header.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<String, String> next = iterator.next();connection.setRequestProperty(next.getKey(), next.getValue());}}connection.connect();BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));writer.write(jsonPara);writer.close();int responseCode = connection.getResponseCode();log.info("httpResponseCode = {}", responseCode);if (responseCode == HttpURLConnection.HTTP_OK) {String contentType = connection.getContentType();String content = connection.getHeaderField("Content-Disposition");response.reset();response.setContentType(contentType);response.setHeader("Content-Disposition", content);OutputStream outputStream = response.getOutputStream();BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());byte[] temp = new byte[1024];int lenth;while ((lenth = inputStream.read(temp)) > 0) {outputStream.write(temp, 0, lenth);}outputStream.flush();outputStream.close();} else {log.warn("http failed , httpResponseCode = {}", responseCode);throw new RuntimeException("HttpInvoker httpResponseCode " + uri + " " + responseCode);}} catch (MalformedURLException e) {log.warn("http failed , MalformedURLException ", e);throw new RuntimeException("HttpInvoker MalformedURLException " + uri, e);} catch (IOException e) {log.warn("http failed , IOException ", e);throw new RuntimeException("HttpInvoker IOException " + uri, e);} catch (Throwable e) {log.warn("http failed , Throwable ", e);throw new RuntimeException("HttpInvoker Throwable " + uri, e);} finally {if (connection != null) {connection.disconnect();}long currentTimeMillisEnd = System.currentTimeMillis();log.info("HttpInvoker post url = {}  time-consuming = {}", uri, currentTimeMillisEnd - currentTimeMillisStart);log.info("HttpInvoker end post url = {}  response = {}", uri, result);}
}
http://www.lryc.cn/news/177881.html

相关文章:

  • Docker-部署docker-compose以及管理服务
  • Android - Monkey 测试应用出现Crash报错IllegalStateException
  • Spring源码分析 事务 实现原理
  • ADS-B及雷达显示终端8.3
  • 第二章:最新版零基础学习 PYTHON 教程(第二节 - Python 输入/输出–从 Python 控制台获取输入)
  • linux安装配置 flume
  • SSM - Springboot - MyBatis-Plus 全栈体系(十五)
  • win10默认浏览器改不了怎么办,解决方法详解
  • C语言连接MySQL并执行SQL语句(hello world)
  • react实现动态递增展示数字特效
  • 读取.nrrd和.dcm文件格式医学图片可视化与预处理
  • VS CODE中的筛选器如何打开?
  • vue 多环境文件配置(开发,测试,生产)
  • 在服务器上搭建pulseaudio的运行环境,指定其运行目录、状态目录和模块目录
  • [Qt]QListView 重绘实例之一:背景重绘
  • 国庆周《Linux学习第二课》
  • 6年前的麒麟980依旧可以再战
  • JS计算任意多边形的面积
  • ios xcode15 navigationController?.navigationBar.isHidden = false无效
  • Python二级 每周练习题20
  • 深度学习-一个简单的深度学习推导
  • ES写入数据报错:retrying failed action with response code: 429
  • Redis给Lua脚本的调用
  • Spring工具类--ReflectUtils的使用
  • 联盟 | 彩漩 X HelpLook,AI技术赋能企业效率提升
  • MATLAB m文件格式化
  • ​分拆菜鸟将使阿里巴巴股票迎来新一轮上涨?
  • Excel 技巧记录-那些复杂的公式和函数
  • vue里使用elementui的级联选择器el-cascader进行懒加载的怎么实现数据回显?
  • Qt raise()问题