[113] | 1 | function location = mget(h,str,targetDirectory) |
---|
| 2 | %MGET Download from an FTP site. |
---|
| 3 | % MGET(FTP,FILENAME) downloads a file. |
---|
| 4 | % |
---|
| 5 | % MGET(FTP,DIRECTORY) downloads a directory and its contents. |
---|
| 6 | % |
---|
| 7 | % MGET(FTP,WILDCARD) downloads a set of files or directories specified |
---|
| 8 | % by a wildcard. |
---|
| 9 | % |
---|
| 10 | % MGET(...,TARGETDIRECTORY) specifies the local target directory, rather |
---|
| 11 | % than the current directory. |
---|
| 12 | |
---|
| 13 | % Matthew J. Simoneau, 14-Nov-2001 |
---|
| 14 | % Copyright 1984-2012 The MathWorks, Inc. |
---|
| 15 | |
---|
| 16 | % Make sure we're still connected. |
---|
| 17 | connect(h) |
---|
| 18 | |
---|
| 19 | if (nargin < 3) |
---|
| 20 | targetDirectory = pwd; |
---|
| 21 | end |
---|
| 22 | |
---|
| 23 | % Set ascii/binary |
---|
| 24 | switch char(h.type.toString) |
---|
| 25 | case 'binary' |
---|
| 26 | h.jobject.setFileType(h.jobject.BINARY_FILE_TYPE); |
---|
| 27 | otherwise |
---|
| 28 | h.jobject.setFileType(h.jobject.ASCII_FILE_TYPE); |
---|
| 29 | end |
---|
| 30 | |
---|
| 31 | if any(str == '*') |
---|
| 32 | list = h.jobject.listNames(str); |
---|
| 33 | listing = cell(size(list)); |
---|
| 34 | for i = 1:length(list) |
---|
| 35 | listing{i} = char(list(i)); |
---|
| 36 | end |
---|
| 37 | else |
---|
| 38 | listing = {str}; |
---|
| 39 | end |
---|
| 40 | |
---|
| 41 | iListing = 0; |
---|
| 42 | location = {}; |
---|
| 43 | while iListing < length(listing) |
---|
| 44 | iListing = iListing+1; |
---|
| 45 | name = char(listing(iListing)); |
---|
| 46 | localName = strrep(fullfile(targetDirectory,name),'/',filesep); |
---|
| 47 | |
---|
| 48 | % Where should we save this file? |
---|
| 49 | fileObject = java.io.File(localName); |
---|
| 50 | |
---|
| 51 | % Are we trying to overwrite a local file we can't change? |
---|
| 52 | if fileObject.exists && ~fileObject.delete |
---|
| 53 | error(message('MATLAB:ftp:CannotOverwrite', localName)) |
---|
| 54 | end |
---|
| 55 | |
---|
| 56 | % Are we trying to write a local file where we don't have permission? |
---|
| 57 | try |
---|
| 58 | d = java.io.File(fileparts(localName)); |
---|
| 59 | d.mkdirs; |
---|
| 60 | canOpen = fileObject.createNewFile; |
---|
| 61 | catch |
---|
| 62 | canOpen = false; |
---|
| 63 | end |
---|
| 64 | if ~canOpen |
---|
| 65 | error(message('MATLAB:ftp:CannotCreate', localName)); |
---|
| 66 | end |
---|
| 67 | |
---|
| 68 | % Download the file. |
---|
| 69 | fos = java.io.FileOutputStream(fileObject); |
---|
| 70 | h.jobject.retrieveFile(name,fos); |
---|
| 71 | fos.close; |
---|
| 72 | fschange(fileparts(localName)) |
---|
| 73 | |
---|
| 74 | % Did it work? |
---|
| 75 | replyCode = h.jobject.getReplyCode; |
---|
| 76 | switch replyCode |
---|
| 77 | case 226 |
---|
| 78 | % "Closing data connection. Requested file action successful." |
---|
| 79 | % Build the list of files uploaded. |
---|
| 80 | location{end+1,1} = localName; |
---|
| 81 | case 550 |
---|
| 82 | % "Requested action not taken. File unavailable." |
---|
| 83 | fileObject.delete; |
---|
| 84 | |
---|
| 85 | % Couldn't find a file with that name. Try a directory. |
---|
| 86 | currentDir = cd(h); |
---|
| 87 | try |
---|
| 88 | cd(h,name); |
---|
| 89 | isDir = true; |
---|
| 90 | catch |
---|
| 91 | isDir = false; |
---|
| 92 | end |
---|
| 93 | cd(h,currentDir); |
---|
| 94 | if isDir |
---|
| 95 | mkdir(localName); |
---|
| 96 | d = dir(h,name); |
---|
| 97 | for i = 1:length(d) |
---|
| 98 | next = d(i).name; |
---|
| 99 | if isequal(next,'.') || isequal(next,'..') |
---|
| 100 | % skip |
---|
| 101 | else |
---|
| 102 | listing{end+1} = [name '/' next]; |
---|
| 103 | end |
---|
| 104 | end |
---|
| 105 | else |
---|
| 106 | error(message('MATLAB:ftp:FileUnavailable', name)); |
---|
| 107 | end |
---|
| 108 | otherwise |
---|
| 109 | error(message('MATLAB:ftp:FTPError',replyCode)) |
---|
| 110 | end |
---|
| 111 | |
---|
| 112 | end |
---|