求一个小demo啊~各位大神,我只知道发送请求如果是返回数据就用一个 BufferedReader接受,如果返回的是一个文件要怎么接受啊。。求教~~
文件不也是通过数据流传输过来的吗?
只不过传输过来写成一个文件就可以了
public String urlCon(final String input) {
Message message = new Message();
try {
URL url = new URL(PATH + input);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setConnectTimeout(5000);
connection.setReadTimeout(8000);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.flush();
connection.connect();
connection.getResponseCode();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
Log.i("返回的数据是 ", response.toString());
dowmloadfile(response.toString());
return response.toString();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
如果不可以指定文件名 ,那就先服务器传过来要传的文件名呗