[113] | 1 | function location = mput(h,str) |
---|
| 2 | %MPUT Upload to an FTP site. |
---|
| 3 | % MPUT(FTP,FILENAME) uploads a file. |
---|
| 4 | % |
---|
| 5 | % MPUT(FTP,DIRECTORY) uploads a directory and its contents. |
---|
| 6 | % |
---|
| 7 | % MPUT(FTP,WILDCARD) uploads a set of files or directories specified |
---|
| 8 | % by a wildcard. |
---|
| 9 | % |
---|
| 10 | % All of these calling forms return a cell array listing the full path to the |
---|
| 11 | % uploaded files on the server. |
---|
| 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 | % Figure out where the files live. |
---|
| 20 | [localDir,file,ext] = fileparts(str); |
---|
| 21 | filename = [file ext]; |
---|
| 22 | if isempty(localDir) |
---|
| 23 | localDir = pwd; |
---|
| 24 | end |
---|
| 25 | remoteDir = char(h.jobject.printWorkingDirectory); |
---|
| 26 | |
---|
| 27 | % Set ascii/binary |
---|
| 28 | switch char(h.type.toString) |
---|
| 29 | case 'binary' |
---|
| 30 | h.jobject.setFileType(h.jobject.BINARY_FILE_TYPE); |
---|
| 31 | otherwise |
---|
| 32 | h.jobject.setFileType(h.jobject.ASCII_FILE_TYPE); |
---|
| 33 | end |
---|
| 34 | |
---|
| 35 | if strfind(filename,'*') |
---|
| 36 | % Upload any files and directories that match the wildcard. |
---|
| 37 | d=dir(fullfile(localDir,filename)); |
---|
| 38 | listing = {d.name}; |
---|
| 39 | else |
---|
| 40 | listing = {filename}; |
---|
| 41 | end |
---|
| 42 | |
---|
| 43 | location = {}; |
---|
| 44 | iListing = 0; |
---|
| 45 | while (iListing < length(listing)) |
---|
| 46 | iListing = iListing+1; |
---|
| 47 | name = listing{iListing}; |
---|
| 48 | localName = strrep(fullfile(localDir,name),'/',filesep); |
---|
| 49 | if isdir(localName) |
---|
| 50 | mkdir(h,name); |
---|
| 51 | d = dir(localName); |
---|
| 52 | for i = 1:length(d) |
---|
| 53 | next = d(i).name; |
---|
| 54 | if isequal(next,'.') || isequal(next,'..') |
---|
| 55 | % skip |
---|
| 56 | else |
---|
| 57 | listing{end+1} = [name '/' next]; |
---|
| 58 | end |
---|
| 59 | end |
---|
| 60 | else |
---|
| 61 | % Check for the file. |
---|
| 62 | fileObject = java.io.File(localName); |
---|
| 63 | if ~fileObject.exists |
---|
| 64 | error(message('MATLAB:ftp:NotFound', localName)) |
---|
| 65 | end |
---|
| 66 | |
---|
| 67 | % Upload this file. |
---|
| 68 | fis = java.io.FileInputStream(fileObject); |
---|
| 69 | status = h.jobject.storeFile(name,fis); |
---|
| 70 | fis.close; |
---|
| 71 | |
---|
| 72 | % Error if the upload failed. |
---|
| 73 | if (status == 0) |
---|
| 74 | code = h.jobject.getReplyCode; |
---|
| 75 | switch code |
---|
| 76 | case 550 |
---|
| 77 | error(message('MATLAB:ftp:UploadFailed', name)); |
---|
| 78 | case 553 |
---|
| 79 | error(message('MATLAB:ftp:BadFilename', name)); |
---|
| 80 | otherwise |
---|
| 81 | error(message('MATLAB:ftp:FTPError',code)); |
---|
| 82 | end |
---|
| 83 | end |
---|
| 84 | |
---|
| 85 | % Build the list of files uploaded. |
---|
| 86 | location{end+1,1} = [remoteDir '/' name]; |
---|
| 87 | end |
---|
| 88 | end |
---|