繁体   English   中英

BufferedWriter类型的方法write(int)不适用于参数(byte [])

[英]The method write(int) in the type BufferedWriter is not applicable for the arguments (byte[])

我是春天的初学者,

我正在尝试从网上下载文件并将其存储在我的本地磁盘中,但是我遇到了瓷砖中提到的编译错误

我试图将ResponseEntity和json作为String起作用,但是我尝试打开图像时却表示图像已损坏或不受支持

我的代码:

public static void main(String arg[]) throws IOException
    {




        HttpHeaders requestHeaders = new HttpHeaders();
        //requestHeaders.setAcceptEncoding(ContentCodingType.IDENTITY);
        HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

        // Create a new RestTemplate instance
        RestTemplate restTemplate = new RestTemplate();

        // Add the String message converter
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

        // Make the HTTP GET request, marshaling the response to a String
        ResponseEntity<byte[]> response = restTemplate.exchange("http://www.nndb.com/people/954/000354889/duke-kahanamoku-2-sized.jpg", HttpMethod.GET, requestEntity,byte[].class);
        File file = new File("/Users/crohitk/Desktop/image.jpg");

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
        byte[] json =response.getBody();
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(json);
        bw.close();

        System.out.println("Done");     


        System.out.println(response.getBody());
        }

Writer用于写入字符。 使用OutputStream将字节写入文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM