フィルターのクリア

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

2 ビュー (過去 30 日間)
buer
buer 2014 年 11 月 20 日
回答済み: buer 2014 年 12 月 17 日
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
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 件)

カテゴリ

Help Center および File ExchangeWeb Services についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by