从 URLConnection 读取二进制文件
2022-09-02 00:30:28
我正在尝试从 URL 连接中读取二进制文件。当我用文本文件测试它时,它似乎工作正常,但对于二进制文件,它却不能。当文件被发送出去时,我在服务器上使用以下哑剧类型:
application/octet-stream
但到目前为止,似乎没有任何效果。这是我用来接收文件的代码:
file = File.createTempFile( "tempfile", ".bin");
file.deleteOnExit();
URL url = new URL( "http://somedomain.com/image.gif" );
URLConnection connection = url.openConnection();
BufferedReader input = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
Writer writer = new OutputStreamWriter( new FileOutputStream( file ) );
int c;
while( ( c = input.read() ) != -1 ) {
writer.write( (char)c );
}
writer.close();
input.close();