function [output,status]=urlwrite_auth(url, user, password,location,wanted)
jUrl = java.net.URL(url);
conn = jUrl.openConnection();
conn.setRequestProperty('Authorization', ['Basic ' base64encode([user ':' password])]);
conn.connect()
file = java.io.File(location);
try
file = file.getCanonicalFile;
catch
error('MATLAB:urlwrite:InvalidOutputLocation','Could not resolve file "%s".',char(file.getAbsolutePath));
end
pathy=strcat(location,'\',wanted);
try
fileOutputStream = java.io.FileOutputStream(pathy);
catch
error('MATLAB:urlwrite:InvalidOutputLocation','Could not open output file "%s".',char(file.getAbsolutePath));
end
try
inputStream = conn.getInputStream;
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
isc.copyStream(inputStream,fileOutputStream);
inputStream.close;
fileOutputStream.close;
output = char(file.getAbsolutePath);
catch
fileOutputStream.close;
delete(file);
if catchErrors, return
else error('MATLAB:urlwrite:ConnectionFailed','Error downloading URL. Your network connection may be down or your proxy settings improperly configured.');
end
end
status = 1;
function out = base64encode(str)
encoder = sun.misc.BASE64Encoder();
out = char(encoder.encode(java.lang.String(str).getBytes()));
Best Answer