• 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 params = new LinkedMultiValueMap<>();
    
                ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {
                    @Override
                    public 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> 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 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> iterator = header.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry 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);
        }
    }
    
  • 相关阅读:
    Python大数据之linux学习总结——day10_hive调优
    linux查看各个目录占用磁盘的大小&清理nohup.out日志
    用帆软报表FineReport打造公司数字化经营报表
    【Springboot 入门培训】# 15 MyBatis-Thymeleaf 插件在项目中的应用
    Docker的资源配额
    数据库基础
    MySQL - order by排序查询 (查询操作 四)
    kube-scheduler addAllEventHandlers
    BI与数据治理以及数据仓库有什么区别
    iOS WKWebView 判断跳转链接是否是用户点击
  • 原文地址:https://blog.csdn.net/sidihuo/article/details/133388704