java backend interface to forward the request to other documents

Reprinted:  https://blog.csdn.net/lsqingfeng/article/details/90611686

I'm here mainly because the method needs to access the page https http, can not directly access, temporarily add a layer of access.

Upload files to request https, then https to http in the background of

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.3</version>
</dependency>
/**
     * Send files using httpclint
     * @Param url: Interface full path
     * @Param File: Upload File
     * @Param fileParamName: Interface parameters corresponding to the file name: the equivalent @RequestParam ( "fileParamName")
     * @Param headerParams: request headers: You may need to carry a token, be careful not to set the content-type
     * @Param otherParams: Other parameters
     * @return
     */
    public static String uploadApiFile(String url ,MultipartFile file,String fileParamName,Map<String,String>headerParams,Map<String,String>otherParams) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = "";
        try {
            String fileName = file.getOriginalFilename();
            HttpPost httpPost = new HttpPost(url);
            //添加header
            for (Map.Entry<String, String> e : headerParams.entrySet()) {
                httpPost.addHeader(e.getKey(), e.getValue());
            }
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setCharset(Charset.forName("utf-8"));
            builder.setMode (HttpMultipartMode.BROWSER_COMPATIBLE); // add this line of code to solve the garbage problem returned Chinese 
            builder.addBinaryBody (fileParamName, file.getInputStream (), ContentType.MULTIPART_FORM_DATA, fileName); // file stream 
            for (Map.Entry < String, String> E: otherParams.entrySet ()) {
                builder.addTextBody (e.getKey (), e.getValue ()); // similar to a browser form submission, and name corresponding to the input value 
            }
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            Response the HttpResponse = httpClient.execute (HttpPost); // commit 
            the HttpEntity responseEntity = response.getEntity ();
             IF (! ResponseEntity = null ) {
                 // the response contents into a string 
                result = EntityUtils.toString (responseEntity, Charset. the forName ( "UTF-. 8" ));
            }
        } catch (IOException e) {
            e.printStackTrace ();
        } catch (Exception e) {
            e.printStackTrace ();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
        return result;
    }

If you have big brothers know js direct access https pages http upload files! Wang also urged January

Guess you like

Origin www.cnblogs.com/yxgmagic/p/11766149.html