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

3 篇文章 0 订阅

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()) {
                @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<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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值