|
a |
|
b/functions/adminfunc/plugin_urlwrite.m |
|
|
1 |
function [output,status] = plugin_urlwrite(urlChar,location,method,params) |
|
|
2 |
% URLWRITE Save the contents of a URL to a file. |
|
|
3 |
% URLWRITE(URL,FILENAME) saves the contents of a URL to a file. FILENAME |
|
|
4 |
% can specify the complete path to a file. If it is just the name, it will |
|
|
5 |
% be created in the current directory. |
|
|
6 |
% |
|
|
7 |
% F = URLWRITE(...) returns the path to the file. |
|
|
8 |
% |
|
|
9 |
% F = URLWRITE(...,METHOD,PARAMS) passes information to the server as |
|
|
10 |
% part of the request. The 'method' can be 'get', or 'post' and PARAMS is a |
|
|
11 |
% cell array of param/value pairs. |
|
|
12 |
% |
|
|
13 |
% [F,STATUS] = URLWRITE(...) catches any errors and returns the error code. |
|
|
14 |
% |
|
|
15 |
% Examples: |
|
|
16 |
% urlwrite('http://www.mathworks.com/',[tempname '.html']) |
|
|
17 |
% urlwrite('ftp://ftp.mathworks.com/README','readme.txt') |
|
|
18 |
% urlwrite(['file:///' fullfile(prefdir,'history.m')],'myhistory.m') |
|
|
19 |
% |
|
|
20 |
% From behind a firewall, use the Preferences to set your proxy server. |
|
|
21 |
% |
|
|
22 |
% See also URLREAD. |
|
|
23 |
|
|
|
24 |
% Adapted by A. Delorme from |
|
|
25 |
% Matthew J. Simoneau, 13-Nov-2001 |
|
|
26 |
% Copyright 1984-2011 The MathWorks, Inc. |
|
|
27 |
% $Revision: 1.4.4.14 $ $Date: 2011/09/03 22:43:01 $ |
|
|
28 |
|
|
|
29 |
% This function requires Java. |
|
|
30 |
if ~usejava('jvm') |
|
|
31 |
error(message('MATLAB:urlwrite:NoJvm')); |
|
|
32 |
end |
|
|
33 |
|
|
|
34 |
import('com.mathworks.mlwidgets.io.InterruptibleStreamCopier'); |
|
|
35 |
|
|
|
36 |
% Be sure the proxy settings are set. |
|
|
37 |
com.mathworks.mlwidgets.html.HTMLPrefs.setProxySettings |
|
|
38 |
|
|
|
39 |
% Check number of inputs and outputs. |
|
|
40 |
if ~ischar(urlChar) |
|
|
41 |
error('MATLAB:urlwrite:InvalidInput','The first input, the URL, must be a character array.'); |
|
|
42 |
end |
|
|
43 |
if ~ischar(location) |
|
|
44 |
error('MATLAB:urlwrite:InvalidInput','The second input, a filename, must be a character array.'); |
|
|
45 |
end |
|
|
46 |
if (nargin > 2) && ~strcmpi(method,'get') && ~strcmpi(method,'post') |
|
|
47 |
error('MATLAB:urlwrite:InvalidInput','Second argument must be either "get" or "post".'); |
|
|
48 |
end |
|
|
49 |
|
|
|
50 |
% Do we want to throw errors or catch them? |
|
|
51 |
if nargout == 2 |
|
|
52 |
catchErrors = true; |
|
|
53 |
else |
|
|
54 |
catchErrors = false; |
|
|
55 |
end |
|
|
56 |
|
|
|
57 |
% Set default outputs. |
|
|
58 |
output = ''; |
|
|
59 |
status = 0; |
|
|
60 |
|
|
|
61 |
% GET method. Tack param/value to end of URL. |
|
|
62 |
if (nargin > 2) && strcmpi(method,'get') |
|
|
63 |
if mod(length(params),2) == 1 |
|
|
64 |
error('MATLAB:urlwrite:InvalidInput','Invalid parameter/value pair arguments.'); |
|
|
65 |
end |
|
|
66 |
for i=1:2:length(params) |
|
|
67 |
if (i == 1), separator = '?'; else separator = '&'; end |
|
|
68 |
param = char(java.net.URLEncoder.encode(params{i})); |
|
|
69 |
value = char(java.net.URLEncoder.encode(params{i+1})); |
|
|
70 |
urlChar = [urlChar separator param '=' value]; |
|
|
71 |
end |
|
|
72 |
end |
|
|
73 |
|
|
|
74 |
% Create a urlConnection. |
|
|
75 |
[urlConnection,errorid,errormsg] = plugin_urlreadwrite(mfilename,urlChar); |
|
|
76 |
if isempty(urlConnection) |
|
|
77 |
if catchErrors, return |
|
|
78 |
else error(errorid,errormsg); |
|
|
79 |
end |
|
|
80 |
end |
|
|
81 |
|
|
|
82 |
pluginSize = 2*plugin_urlsize(urlChar)/1000000; |
|
|
83 |
timeOut = max(round(pluginSize*1000), 5000); |
|
|
84 |
urlConnection.setReadTimeout(timeOut); % timeout in 5 seconds |
|
|
85 |
|
|
|
86 |
% POST method. Write param/values to server. |
|
|
87 |
if (nargin > 2) && strcmpi(method,'post') |
|
|
88 |
try |
|
|
89 |
urlConnection.setDoOutput(true); |
|
|
90 |
urlConnection.setRequestProperty( ... |
|
|
91 |
'Content-Type','application/x-www-form-urlencoded'); |
|
|
92 |
printStream = java.io.PrintStream(urlConnection.getOutputStream); |
|
|
93 |
for i=1:2:length(params) |
|
|
94 |
if (i > 1), printStream.print('&'); end |
|
|
95 |
param = char(java.net.URLEncoder.encode(params{i})); |
|
|
96 |
value = char(java.net.URLEncoder.encode(params{i+1})); |
|
|
97 |
printStream.print([param '=' value]); |
|
|
98 |
end |
|
|
99 |
printStream.close; |
|
|
100 |
catch |
|
|
101 |
if catchErrors, return |
|
|
102 |
else error('MATLAB:urlwrite:ConnectionFailed','Could not POST to URL.'); |
|
|
103 |
end |
|
|
104 |
end |
|
|
105 |
end |
|
|
106 |
|
|
|
107 |
% Specify the full path to the file so that getAbsolutePath will work when the |
|
|
108 |
% current directory is not the startup directory and urlwrite is given a |
|
|
109 |
% relative path. |
|
|
110 |
file = java.io.File(location); |
|
|
111 |
if ~file.isAbsolute |
|
|
112 |
location = fullfile(pwd,location); |
|
|
113 |
file = java.io.File(location); |
|
|
114 |
end |
|
|
115 |
|
|
|
116 |
% Make sure the path isn't nonsense. |
|
|
117 |
try |
|
|
118 |
file = file.getCanonicalFile; |
|
|
119 |
catch |
|
|
120 |
error('MATLAB:urlwrite:InvalidOutputLocation','Could not resolve file "%s".',char(file.getAbsolutePath)); |
|
|
121 |
end |
|
|
122 |
|
|
|
123 |
% Open the output file. |
|
|
124 |
try |
|
|
125 |
fileOutputStream = java.io.FileOutputStream(file); |
|
|
126 |
catch |
|
|
127 |
error('MATLAB:urlwrite:InvalidOutputLocation','Could not open output file "%s".',char(file.getAbsolutePath)); |
|
|
128 |
end |
|
|
129 |
|
|
|
130 |
% Read the data from the connection. |
|
|
131 |
try |
|
|
132 |
inputStream = urlConnection.getInputStream; |
|
|
133 |
% This StreamCopier is unsupported and may change at any time. |
|
|
134 |
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier; |
|
|
135 |
isc.copyStream(inputStream,fileOutputStream); |
|
|
136 |
inputStream.close; |
|
|
137 |
fileOutputStream.close; |
|
|
138 |
output = char(file.getAbsolutePath); |
|
|
139 |
catch |
|
|
140 |
fileOutputStream.close; |
|
|
141 |
delete(file); |
|
|
142 |
if catchErrors, return |
|
|
143 |
else error('MATLAB:urlwrite:ConnectionFailed','Error downloading URL. Your network connection may be down or your proxy settings improperly configured.'); |
|
|
144 |
end |
|
|
145 |
end |
|
|
146 |
|
|
|
147 |
status = 1; |