이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Matlab using Java openStream(url) get :Java exception occurred: java.lang.NullPointerException
조회 수: 5 (최근 30일)
이전 댓글 표시
buer
2014년 11월 20일
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
Geoff Hayes
2014년 11월 20일
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.
buer
2014년 11월 21일
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
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...
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
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
2014년 11월 25일
You may be able to urlread...it should be a function that your version of MATLAB has.
buer
2014년 12월 1일
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...
buer
2014년 12월 2일
Hi Groff.
Thanks a lot. It is valid link..I got it solved by learning this http://stackoverflow.com/questions/1317931/how-do-i-provide-a-username-password-to-access-a-web-resource-using-matlab-urlre
This is the answer...thanks a lot.
Geoff Hayes
2014년 12월 2일
Buer - good that you figured it out. Could you summarize it as an answer below?
채택된 답변
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 Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)