java - Stream file from URL to File without storing it in the memory -


i want download file url , store file system. have memory limitation , don't want store in memory before. not java expert , bit lost class inputstream, bufferedreader, fileoutputstream, etc. me please ?

for have:

urlconnection ucon = url.openconnection(); ucon.connect(); inputstream = ucon.getinputstream(); // create reader input stream. bufferedreader br = new bufferedreader(isr);  // ?  fileoutputstream fos = context.openfileoutput(filename, context.mode_private); // here content can big memory... fos.write(content.getbytes()); fos.close(); 

please, give me clue ? thinking read chunk chunk, not sure easiest java...

you can use apache commons

org.apache.commons.io.fileutils.copyurltofile(url, file) 

i guess may not work on android use code

inputstream input = connection.getinputstream(); byte[] buffer = new byte[4096]; int cnt = - 1;  outputstream output = new fileoutputstream(file); while ( (cnt = input.read(buffer)) != -1) {   output.write(buffer, 0, cnt); } output.close(); 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -