Matlab using Java openStream(url) get :Java exception occurred: java.lang.​NullPointe​rException

Hi,
I want to download one file from a URL. According to this: http://www.mathworks.com/matlabcentral/newsreader/view_thread/294528. all works fine until i go to the line: is = jurl.openStream; This gave one error says Java exception occurred: java.lang.NullPointerException.
Anyone can help? thanks

12 件のコメント

buer - what happens on the previous line
jurl = java.net.URL(linki);
What is linki? Is it a valid URL? I ran the code with a couple of test URLs and was able to download each without any problems.
Hi, the only thing is my URL is few parts and the file name is not in the URL...so I have to define the name again myself...
Company='Abc';
Depar='Fin';
url1='com/';;
url2='/download_h5_Depar=';
url3='.h5' ;
url=strcat(url1,Company,url2,Depar,url3);
links=url;
% copy files to local folder
[pathstr, name, ext ] = fileparts(links);
% name changed here
name=strcat(Company,'-',Depar);
Here you can see....now I am suspect about the security of the website....it needs to be in exact network domain with username and passpord for login...so i am not sure if this matters....
Geoff Hayes
Geoff Hayes 2014 年 11 月 21 日
Have you verified that the link that you build, linki, is valid? i.e. can you copy and paste it into your browser and view the file?
As for needing a username and password, that could be the problem...
buer
buer 2014 年 11 月 24 日
編集済み: buer 2014 年 11 月 24 日
Yes..I did try copy and paste the link, it works perfectly... but when it goes to the program itself, it does nto work...I also think it is most paobabaly the login problem..Do you know how can I solve this log in problem? Thanks
buer
buer 2014 年 11 月 24 日
Do you know how can I solve this log in problem? Thanks
Geoff Hayes
Geoff Hayes 2014 年 11 月 25 日
An alternative to this code may be to use webread or urlread. The first function can be used if you have R2014b. Both seem to allow for username and passwords. See also http://www.mathworks.com/matlabcentral/answers/161160-urlread-vs-view-source-need-view-source-data which may be useful.
buer
buer 2014 年 11 月 25 日
Thanks Geoff, unfortunately I have only R2013b.... any possible way to do it? I am ´sure I can ´not get R2014b,I am using school Matlab....Thanks...
Geoff Hayes
Geoff Hayes 2014 年 11 月 25 日
You may be able to urlread...it should be a function that your version of MATLAB has.
Hi Geoff,
I tried with urlread....
url2=urlread(links, 'Authentication','basic','Username','Myname','Password','Myword');
It gives me such errors all the time:
Error using urlreadwrite (line 91)
The IP address of "HostName" could not be determined.
Error in urlread (line 36)
[s,status] =
urlreadwrite(mfilename,catchErrors,url,varargin{:});
Can you help me with this? Is it possible to add the authentication inside the code I post in the link? Thanks in advance...
Geoff Hayes
Geoff Hayes 2014 年 12 月 2 日
Hi Buer - have you verified that links is valid?
buer
buer 2014 年 12 月 2 日
Hi Groff.
This is the answer...thanks a lot.
Geoff Hayes
Geoff Hayes 2014 年 12 月 2 日
Buer - good that you figured it out. Could you summarize it as an answer below?

サインインしてコメントする。

 採用された回答

buer
buer 2014 年 12 月 17 日
Yes. so the answer is Matlab urlread does not support authentication with lower level request. So we need to code firectly against the Java URL class to use them:
% Matlab's urlread() doesn't do HTTP Request params, so work directly with Java
jUrl = java.net.URL(url);
conn = jUrl.openConnection();
conn.setRequestProperty('Authorization', ['Basic ' base64encode([user ':' password])]);
conn.connect();
info.status = conn.getResponseCode();
info.errMsg = char(readstream(conn.getErrorStream()));
s = readstream(conn.getInputStream());
function out = base64encode(str)
% Uses Sun-specific class, but we know that is the JVM Matlab ships with
encoder = sun.misc.BASE64Encoder();
out = char(encoder.encode(java.lang.String(str).getBytes()));
function out = readstream(inStream)
%READSTREAM Read all bytes from stream to uint8
try
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
byteStream = java.io.ByteArrayOutputStream();
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier();
isc.copyStream(inStream, byteStream);
inStream.close();
byteStream.close();
out = typecast(byteStream.toByteArray', 'uint8'); %'
catch err
out = []; %HACK: quash
end
Hope it will help others later.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

質問済み:

2014 年 11 月 20 日

回答済み:

2014 年 12 月 17 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by