保存到sd卡
public class DownloadFile {
public int downFile(String urlStr,String path,String fileName){
final String SDPATH = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;
FileOutputStream fos = null;
InputStream inputStream = null;
File localDir = new File(SDPATH+path);
File localFile = new File(SDPATH+path+fileName);
String remoteFilePath = urlStr+fileName;
try {
//FileInputStream fis = new FileInputStream(inputSteam);
//InputStreamReader isr = new InputStreamReader(fis);
//BufferedReader br = new BufferedReader(isr);
if(localFile.exists()){
return 1;
}
if(!localDir.exists()){
localDir.mkdir();
}
if(!localFile.exists()){
localFile.createNewFile();
}
fos = new FileOutputStream(localFile);
//OutputStreamWriter osw = new OutputStreamWriter(fos);
//BufferedWriter bw = new BufferedWriter(osw);
URL url = new URL(remoteFilePath);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
inputStream = urlConn.getInputStream();
byte[] buff = new byte[1024];
int data=0;
while((data=inputStream.read(buff))!=-1){
fos.write(buff, 0, data);
}
fos.close();
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
return 0;
}
}
谢谢