source: prextra/@ftp/mput.m @ 113

Last change on this file since 113 was 113, checked in by bduin, 8 years ago
File size: 2.4 KB
Line 
1function 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.
17connect(h)
18
19% Figure out where the files live.
20[localDir,file,ext] = fileparts(str);
21filename = [file ext];
22if isempty(localDir)
23    localDir = pwd;
24end
25remoteDir = char(h.jobject.printWorkingDirectory);
26
27% Set ascii/binary
28switch 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);
33end
34
35if strfind(filename,'*')
36    % Upload any files and directories that match the wildcard.
37    d=dir(fullfile(localDir,filename));
38    listing = {d.name};
39else
40    listing = {filename};
41end
42
43location = {};
44iListing = 0;
45while (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
88end
Note: See TracBrowser for help on using the repository browser.