Coverage report

| %line | %branch
--- | --- | ---
examples.ftp |
0% |
0% |

1 |  |
     /*
2 |  |
      * Copyright 2001-2005 The Apache Software Foundation
3 |  |
      *
4 |  |
      * Licensed under the Apache License, Version 2.0 (the "License");
5 |  |
      * you may not use this file except in compliance with the License.
6 |  |
      * You may obtain a copy of the License at
7 |  |
      *
8 |  |
      *     http://www.apache.org/licenses/LICENSE-2.0
9 |  |
      *
10 |  |
      * Unless required by applicable law or agreed to in writing, software
11 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
12 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 |  |
      * See the License for the specific language governing permissions and
14 |  |
      * limitations under the License.
15 |  |
      */
16 |  |
     package examples;
17 |  |
18 |  |
     import java.io.FileInputStream;
19 |  |
     import java.io.FileOutputStream;
20 |  |
     import java.io.IOException;
21 |  |
     import java.io.InputStream;
22 |  |
     import java.io.OutputStream;
23 |  |
     import java.io.PrintWriter;
24 |  |
     import org.apache.commons.net.ftp.FTP;
25 |  |
     import org.apache.commons.net.ftp.FTPClient;
26 |  |
     import org.apache.commons.net.ftp.FTPConnectionClosedException;
27 |  |
     import org.apache.commons.net.ftp.FTPReply;
28 |  |
29 |  |
     /***
30 |  |
      * This is an example program demonstrating how to use the FTPClient class.
31 |  |
      * This program connects to an FTP server and retrieves the specified
32 |  |
      * file.  If the -s flag is used, it stores the local file at the FTP server.
33 |  |
      * Just so you can see what's happening, all reply strings are printed.
34 |  |
      * If the -b flag is used, a binary transfer is assumed (default is ASCII).
35 |  |
      * <p>
36 |  |
      * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
37 |  |
      * <p>
38 |  |
      ***/
39 | 0 |
     public final class ftp
40 |  |
     {
41 |  |
42 |  |
         public static final String USAGE =
43 |  |
             "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
44 |  |
             "\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
45 |  |
             "\t-s store file on server (upload)\n" +
46 |  |
             "\t-b use binary transfer mode\n";
47 |  |
48 |  |
         public static final void main(String[] args)
49 |  |
         {
50 | 0 |
             int base = 0;
51 | 0 |
             boolean storeFile = false, binaryTransfer = false, error = false;
52 |  |
             String server, username, password, remote, local;
53 |  |
             FTPClient ftp;
54 |  |
55 | 0 |
             for (base = 0; base < args.length; base++)
56 |  |
             {
57 | 0 |
                 if (args[base].startsWith("-s"))
58 | 0 |
                     storeFile = true;
59 | 0 |
                 else if (args[base].startsWith("-b"))
60 | 0 |
                     binaryTransfer = true;
61 |  |
                 else
62 |  |
                     break;
63 |  |
             }
64 |  |
65 | 0 |
             if ((args.length - base) != 5)
66 |  |
             {
67 | 0 |
                 System.err.println(USAGE);
68 | 0 |
                 System.exit(1);
69 |  |
             }
70 |  |
71 | 0 |
             server = args[base++];
72 | 0 |
             username = args[base++];
73 | 0 |
             password = args[base++];
74 | 0 |
             remote = args[base++];
75 | 0 |
             local = args[base];
76 |  |
77 | 0 |
             ftp = new FTPClient();
78 | 0 |
             ftp.addProtocolCommandListener(new PrintCommandListener(
79 |  |
                                                new PrintWriter(System.out)));
80 |  |
81 |  |
             try
82 |  |
             {
83 |  |
                 int reply;
84 | 0 |
                 ftp.connect(server);
85 | 0 |
                 System.out.println("Connected to " + server + ".");
86 |  |
87 |  |
                 // After connection attempt, you should check the reply code to verify
88 |  |
                 // success.
89 | 0 |
                 reply = ftp.getReplyCode();
90 |  |
91 | 0 |
                 if (!FTPReply.isPositiveCompletion(reply))
92 |  |
                 {
93 | 0 |
                     ftp.disconnect();
94 | 0 |
                     System.err.println("FTP server refused connection.");
95 | 0 |
                     System.exit(1);
96 |  |
                 }
97 |  |
             }
98 | 0 |
             catch (IOException e)
99 |  |
             {
100 | 0 |
                 if (ftp.isConnected())
101 |  |
                 {
102 |  |
                     try
103 |  |
                     {
104 | 0 |
                         ftp.disconnect();
105 |  |
                     }
106 | 0 |
                     catch (IOException f)
107 |  |
                     {
108 |  |
                         // do nothing
109 | 0 |
                     }
110 |  |
                 }
111 | 0 |
                 System.err.println("Could not connect to server.");
112 | 0 |
                 e.printStackTrace();
113 | 0 |
                 System.exit(1);
114 | 0 |
             }
115 |  |
116 |  |
     __main:
117 |  |
             try
118 |  |
             {
119 | 0 |
                 if (!ftp.login(username, password))
120 |  |
                 {
121 | 0 |
                     ftp.logout();
122 | 0 |
                     error = true;
123 |  |
                     break __main;
124 |  |
                 }
125 |  |
126 | 0 |
                 System.out.println("Remote system is " + ftp.getSystemName());
127 |  |
128 | 0 |
                 if (binaryTransfer)
129 | 0 |
                     ftp.setFileType(FTP.BINARY_FILE_TYPE);
130 |  |
131 |  |
                 // Use passive mode as default because most of us are
132 |  |
                 // behind firewalls these days.
133 | 0 |
                 ftp.enterLocalPassiveMode();
134 |  |
135 | 0 |
                 if (storeFile)
136 |  |
                 {
137 |  |
                     InputStream input;
138 |  |
139 | 0 |
                     input = new FileInputStream(local);
140 |  |
141 | 0 |
                     ftp.storeFile(remote, input);
142 |  |
143 | 0 |
                     input.close();
144 |  |
                 }
145 |  |
                 else
146 |  |
                 {
147 |  |
                     OutputStream output;
148 |  |
149 | 0 |
                     output = new FileOutputStream(local);
150 |  |
151 | 0 |
                     ftp.retrieveFile(remote, output);
152 |  |
153 | 0 |
                     output.close();
154 |  |
                 }
155 |  |
156 | 0 |
                 ftp.logout();
157 |  |
             }
158 | 0 |
             catch (FTPConnectionClosedException e)
159 |  |
             {
160 | 0 |
                 error = true;
161 | 0 |
                 System.err.println("Server closed connection.");
162 | 0 |
                 e.printStackTrace();
163 |  |
             }
164 | 0 |
             catch (IOException e)
165 |  |
             {
166 | 0 |
                 error = true;
167 | 0 |
                 e.printStackTrace();
168 |  |
             }
169 |  |
             finally
170 |  |
             {
171 | 0 |
                 if (ftp.isConnected())
172 |  |
                 {
173 |  |
                     try
174 |  |
                     {
175 | 0 |
                         ftp.disconnect();
176 |  |
                     }
177 | 0 |
                     catch (IOException f)
178 |  |
                     {
179 |  |
                         // do nothing
180 | 0 |
                     }
181 |  |
                 }
182 |  |
             }
183 |  |
184 | 0 |
             System.exit(error ? 1 : 0);
185 | 0 |
         } // end main
186 |  |
187 |  |
     }
188 |  |

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.