KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MultipartFileUploadApp


1 /*
2  * $Header$
3  * $Revision: 1.2 $
4  * $Date$
5  * ====================================================================
6  *
7  * Copyright 2002-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 import java.awt.GridBagConstraints JavaDoc;
32 import java.awt.GridBagLayout JavaDoc;
33 import java.awt.Insets JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.WindowAdapter JavaDoc;
37 import java.awt.event.WindowEvent JavaDoc;
38 import java.io.File JavaDoc;
39
40 import javax.swing.DefaultComboBoxModel JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JComboBox JavaDoc;
43 import javax.swing.JFileChooser JavaDoc;
44 import javax.swing.JFrame JavaDoc;
45 import javax.swing.JLabel JavaDoc;
46 import javax.swing.JScrollPane JavaDoc;
47 import javax.swing.JTextArea JavaDoc;
48 import javax.swing.JTextField JavaDoc;
49
50 import org.apache.commons.httpclient.HttpClient;
51 import org.apache.commons.httpclient.HttpStatus;
52 import org.apache.commons.httpclient.methods.MultipartPostMethod;
53
54 /**
55  *
56  * This is a Swing application that demonstrates
57  * how to use the Jakarta HttpClient multipart POST method
58  * for uploading files
59  *
60  * @author Sean C. Sullivan
61  * @author Michael Becke
62  *
63  */

64 public class MultipartFileUploadApp {
65
66     public static void main(String JavaDoc[] args) {
67         MultipartFileUploadFrame f = new MultipartFileUploadFrame();
68         f.setTitle("HTTP multipart file upload application");
69         f.pack();
70         f.addWindowListener(
71             new WindowAdapter JavaDoc() {
72                 public void windowClosing(WindowEvent JavaDoc e) {
73                     System.exit(0);
74                 }
75             }
76         );
77         f.setVisible(true);
78     }
79
80     public static class MultipartFileUploadFrame extends JFrame JavaDoc {
81
82         private File JavaDoc targetFile;
83         private JTextArea JavaDoc taTextResponse;
84         private DefaultComboBoxModel JavaDoc cmbURLModel;
85
86         public MultipartFileUploadFrame() {
87             String JavaDoc[] aURLs = {
88                 "http://localhost:8080/httpclienttest/fileupload"
89             };
90             
91             cmbURLModel = new DefaultComboBoxModel JavaDoc(aURLs);
92             final JComboBox JavaDoc cmbURL = new JComboBox JavaDoc(cmbURLModel);
93             cmbURL.setToolTipText("Enter a URL");
94             cmbURL.setEditable(true);
95             cmbURL.setSelectedIndex(0);
96             
97             JLabel JavaDoc lblTargetFile = new JLabel JavaDoc("Selected file:");
98             
99             final JTextField JavaDoc tfdTargetFile = new JTextField JavaDoc(30);
100             tfdTargetFile.setEditable(false);
101             
102             final JButton JavaDoc btnDoUpload = new JButton JavaDoc("Upload");
103             btnDoUpload.setEnabled(false);
104
105             final JButton JavaDoc btnSelectFile = new JButton JavaDoc("Select a file...");
106             btnSelectFile.addActionListener(
107                 new ActionListener JavaDoc() {
108                     public void actionPerformed(ActionEvent JavaDoc evt) {
109                         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
110                         chooser.setFileHidingEnabled(false);
111                         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
112                         chooser.setMultiSelectionEnabled(false);
113                         chooser.setDialogType(JFileChooser.OPEN_DIALOG);
114                         chooser.setDialogTitle("Choose a file...");
115                         if (
116                             chooser.showOpenDialog(MultipartFileUploadFrame.this)
117                             == JFileChooser.APPROVE_OPTION
118                         ) {
119                             targetFile = chooser.getSelectedFile();
120                             tfdTargetFile.setText(targetFile.toString());
121                             btnDoUpload.setEnabled(true);
122                         }
123                     }
124                 }
125             );
126             
127             taTextResponse = new JTextArea JavaDoc(10, 40);
128             taTextResponse.setEditable(false);
129
130             final JLabel JavaDoc lblURL = new JLabel JavaDoc("URL:");
131
132             btnDoUpload.addActionListener(new ActionListener JavaDoc() {
133                 public void actionPerformed(ActionEvent JavaDoc ae) {
134                     String JavaDoc targetURL = cmbURL.getSelectedItem().toString();
135                     // add the URL to the combo model if it's not already there
136
if (!targetURL
137                         .equals(
138                             cmbURLModel.getElementAt(
139                                 cmbURL.getSelectedIndex()))) {
140                         cmbURLModel.addElement(targetURL);
141                     }
142
143                     MultipartPostMethod filePost =
144                         new MultipartPostMethod(targetURL);
145
146                     try {
147                         appendMessage("Uploading " + targetFile.getName() + " to " + targetURL);
148                         filePost.addParameter(targetFile.getName(), targetFile);
149                         HttpClient client = new HttpClient();
150                         client.setConnectionTimeout(5000);
151                         int status = client.executeMethod(filePost);
152                         if (status == HttpStatus.SC_OK) {
153                             appendMessage(
154                                 "Upload complete, response=" + filePost.getResponseBodyAsString()
155                             );
156                         } else {
157                             appendMessage(
158                                 "Upload failed, response=" + HttpStatus.getStatusText(status)
159                             );
160                         }
161                     } catch (Exception JavaDoc ex) {
162                         appendMessage("Error: " + ex.getMessage());
163                         ex.printStackTrace();
164                     } finally {
165                         filePost.releaseConnection();
166                     }
167
168                 }
169             });
170
171             getContentPane().setLayout(new GridBagLayout JavaDoc());
172             GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
173
174             c.anchor = GridBagConstraints.EAST;
175             c.fill = GridBagConstraints.NONE;
176             c.gridheight = 1;
177             c.gridwidth = 1;
178             c.gridx = 0;
179             c.gridy = 0;
180             c.insets = new Insets JavaDoc(10, 5, 5, 0);
181             c.weightx = 1;
182             c.weighty = 1;
183             getContentPane().add(lblURL, c);
184
185             c.anchor = GridBagConstraints.WEST;
186             c.fill = GridBagConstraints.HORIZONTAL;
187             c.gridwidth = 2;
188             c.gridx = 1;
189             c.insets = new Insets JavaDoc(5, 5, 5, 10);
190             getContentPane().add(cmbURL, c);
191
192             c.anchor = GridBagConstraints.EAST;
193             c.fill = GridBagConstraints.NONE;
194             c.insets = new Insets JavaDoc(10, 5, 5, 0);
195             c.gridwidth = 1;
196             c.gridx = 0;
197             c.gridy = 1;
198             getContentPane().add(lblTargetFile, c);
199
200             c.anchor = GridBagConstraints.CENTER;
201             c.fill = GridBagConstraints.HORIZONTAL;
202             c.insets = new Insets JavaDoc(5, 5, 5, 5);
203             c.gridwidth = 1;
204             c.gridx = 1;
205             getContentPane().add(tfdTargetFile, c);
206
207             c.anchor = GridBagConstraints.WEST;
208             c.fill = GridBagConstraints.NONE;
209             c.insets = new Insets JavaDoc(5, 5, 5, 10);
210             c.gridwidth = 1;
211             c.gridx = 2;
212             getContentPane().add(btnSelectFile, c);
213
214             c.anchor = GridBagConstraints.CENTER;
215             c.fill = GridBagConstraints.NONE;
216             c.insets = new Insets JavaDoc(10, 10, 10, 10);
217             c.gridwidth = 3;
218             c.gridx = 0;
219             c.gridy = 2;
220             getContentPane().add(btnDoUpload, c);
221
222             c.anchor = GridBagConstraints.CENTER;
223             c.fill = GridBagConstraints.BOTH;
224             c.insets = new Insets JavaDoc(10, 10, 10, 10);
225             c.gridwidth = 3;
226             c.gridheight = 3;
227             c.weighty = 3;
228             c.gridx = 0;
229             c.gridy = 3;
230             getContentPane().add(new JScrollPane JavaDoc(taTextResponse), c);
231         }
232         
233         private void appendMessage(String JavaDoc m) {
234             taTextResponse.append(m + "\n");
235         }
236     }
237 }
238
Popular Tags