美文网首页
通过路径下载文件并重新上传

通过路径下载文件并重新上传

作者: 来日可期M | 来源:发表于2018-07-02 17:54 被阅读0次

    今天有一个需求,给一个pdf的url,然后下载下来请求令一个服务得到新路径。

    代码如下:

    第一步 下载文件

            String urlStr = "https://***pdf";
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(5*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            InputStream inputStream = conn.getInputStream();
    

    第二步 转换成MultipartFile

            MultipartFile multipartFile = new MockMultipartFile("leaseFile", inputStream);
    

    第三步 发送上传文件的请求

            HashMap<String, String> header = new HashMap<>();
            header.put("Authorization","token");
            HttpUtil.postFile(controllerPath,multipartFile,header,"xxx.pdf");
    

    postFile 工具类

        public static void postFile(String url, MultipartFile excelFile, HashMap<String, String> headers, String fileName) {
    
            CloseableHttpClient httpClient = HttpClients.createDefault();
    
            try {
                //System.out.println("--->"+excelFile.getOriginalFilename());
                HttpPost httpPost = new HttpPost(url);
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.addBinaryBody("leaseFile", excelFile.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
                builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
                HttpEntity entity = builder.build();
                httpPost.setEntity(entity);
                Set<Map.Entry<String, String>> headerSet = headers.entrySet();
                for (Map.Entry<String, String> headerEntry : headerSet) {
                    String key = headerEntry.getKey();
                    String value = headerEntry.getValue();
                    httpPost.setHeader(key, value);
                }
    
                HttpResponse response  = httpClient.execute(httpPost);// 执行提交
                //System.out.println("postFile---response--->"+response);
                int statusCode =0;
                for (int i = 0; i < 3; i++) {
                    try {
                        statusCode = response.getStatusLine().getStatusCode();
                        System.out.println("postFile---statusCode---> "+statusCode);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    if (statusCode== HttpStatus.SC_OK) { // 打印服务器返回的状态
                        break;
                    } else {
    
                        Thread.sleep(1000);
                    }
                }
                HttpEntity responseEntity = response.getEntity();
                //System.out.println("postFile---responseEntity--->"+responseEntity);
                if (responseEntity != null) {
                    // 将响应内容转换为字符串
                    String result  = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
                    System.out.println("postFile---result---> "+result);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    

    部分摘抄于 博客园

    相关文章

      网友评论

          本文标题:通过路径下载文件并重新上传

          本文链接:https://www.haomeiwen.com/subject/cwiluftx.html