无法使用 spring 下载文件,没有错误,但代码不起作用

Not able to download the file using spring no error but code not working

我正在尝试通过身份验证从服务器下载文件,下面是我的 java 代码,但我无法下载该文件,但代码 运行 没有错误 首先使用 trustSelfSignedSSL();我将禁用 ssl 握手并在使用 http 实体后 headers 我正在发送凭据

问题可能只在 main 方法中

public class AppTest
{
    public static void main(String arg[]) throws IOException
    {
        ArrayList<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(new ByteArrayHttpMessageConverter());
        AppTest.trustSelfSignedSSL();
         RestTemplate restTemplate = new RestTemplate(messageConverters);


           System.out.println("ok");


            ResponseEntity<byte[]> response = restTemplate.exchange(
                    "https://jira.example.com/rest/api/2/issue/id",
                    HttpMethod.GET, new HttpEntity<String>(createHeaders()), byte[].class, "1");
            System.out.println(response.getBody());
            if (response.getStatusCode() == HttpStatus.OK) {
                System.out.println("inside");
                Files.write(Paths.get("file.html"), response.getBody());
            }


    }

    static HttpHeaders createHeaders(){
        String plainCreds = "user:pass";
        byte[] plainCredsBytes = plainCreds.getBytes();
        byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
        String base64Creds = new String(base64CredsBytes);


        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + base64Creds);
        return headers;
    }

    public static void trustSelfSignedSSL() {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[]{tm}, null);
            SSLContext.setDefault(ctx);
        } catch (Exception ex) {
            ex.printStackTrace();
        }



}
}

我验证了您的 SSL 绕过和基本身份验证,它们似乎没问题。我以前用过这个技巧成功。

对我来说可疑的是 RestTemplate 调用。

ResponseEntity<byte[]> response = restTemplate.exchange(              
    "https://jira.example.com/rest/api/2/issue/id",                      
    HttpMethod.GET, new HttpEntity<String>(createHeaders()), byte[].class, "1");

末尾的“1”参数假定为 uriVariable,但您的 URL 没有任何占位符(例如 https://jira.example.com/rest/api/2/issue/{id})。尝试删除最后一个参数。