001: /*
002: * $Header$
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: import java.awt.GridBagConstraints;
033: import java.awt.GridBagLayout;
034: import java.awt.Insets;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.awt.event.WindowAdapter;
038: import java.awt.event.WindowEvent;
039: import java.io.File;
040:
041: import javax.swing.DefaultComboBoxModel;
042: import javax.swing.JButton;
043: import javax.swing.JCheckBox;
044: import javax.swing.JComboBox;
045: import javax.swing.JFileChooser;
046: import javax.swing.JFrame;
047: import javax.swing.JLabel;
048: import javax.swing.JScrollPane;
049: import javax.swing.JTextArea;
050: import javax.swing.JTextField;
051:
052: import org.apache.commons.httpclient.HttpClient;
053: import org.apache.commons.httpclient.HttpStatus;
054: import org.apache.commons.httpclient.methods.PostMethod;
055: import org.apache.commons.httpclient.methods.multipart.FilePart;
056: import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
057: import org.apache.commons.httpclient.methods.multipart.Part;
058: import org.apache.commons.httpclient.params.HttpMethodParams;
059:
060: /**
061: *
062: * This is a Swing application that demonstrates
063: * how to use the Jakarta HttpClient multipart POST method
064: * for uploading files
065: *
066: * @author Sean C. Sullivan
067: * @author Michael Becke
068: *
069: */
070: public class MultipartFileUploadApp {
071:
072: public static void main(String[] args) {
073:
074: MultipartFileUploadFrame f = new MultipartFileUploadFrame();
075: f.setTitle("HTTP multipart file upload application");
076: f.pack();
077: f.addWindowListener(new WindowAdapter() {
078: public void windowClosing(WindowEvent e) {
079: System.exit(0);
080: }
081: });
082: f.setVisible(true);
083: }
084:
085: public static class MultipartFileUploadFrame extends JFrame {
086:
087: private File targetFile;
088: private JTextArea taTextResponse;
089: private DefaultComboBoxModel cmbURLModel;
090:
091: public MultipartFileUploadFrame() {
092: String[] aURLs = { "http://localhost:8080/httpclienttest/fileupload" };
093:
094: cmbURLModel = new DefaultComboBoxModel(aURLs);
095: final JComboBox cmbURL = new JComboBox(cmbURLModel);
096: cmbURL.setToolTipText("Enter a URL");
097: cmbURL.setEditable(true);
098: cmbURL.setSelectedIndex(0);
099:
100: JLabel lblTargetFile = new JLabel("Selected file:");
101:
102: final JTextField tfdTargetFile = new JTextField(30);
103: tfdTargetFile.setEditable(false);
104:
105: final JCheckBox cbxExpectHeader = new JCheckBox(
106: "Use Expect header");
107: cbxExpectHeader.setSelected(false);
108:
109: final JButton btnDoUpload = new JButton("Upload");
110: btnDoUpload.setEnabled(false);
111:
112: final JButton btnSelectFile = new JButton(
113: "Select a file...");
114: btnSelectFile.addActionListener(new ActionListener() {
115: public void actionPerformed(ActionEvent evt) {
116: JFileChooser chooser = new JFileChooser();
117: chooser.setFileHidingEnabled(false);
118: chooser
119: .setFileSelectionMode(JFileChooser.FILES_ONLY);
120: chooser.setMultiSelectionEnabled(false);
121: chooser.setDialogType(JFileChooser.OPEN_DIALOG);
122: chooser.setDialogTitle("Choose a file...");
123: if (chooser
124: .showOpenDialog(MultipartFileUploadFrame.this ) == JFileChooser.APPROVE_OPTION) {
125: targetFile = chooser.getSelectedFile();
126: tfdTargetFile.setText(targetFile.toString());
127: btnDoUpload.setEnabled(true);
128: }
129: }
130: });
131:
132: taTextResponse = new JTextArea(10, 40);
133: taTextResponse.setEditable(false);
134:
135: final JLabel lblURL = new JLabel("URL:");
136:
137: btnDoUpload.addActionListener(new ActionListener() {
138: public void actionPerformed(ActionEvent ae) {
139: String targetURL = cmbURL.getSelectedItem()
140: .toString();
141: // add the URL to the combo model if it's not already there
142: if (!targetURL.equals(cmbURLModel
143: .getElementAt(cmbURL.getSelectedIndex()))) {
144: cmbURLModel.addElement(targetURL);
145: }
146:
147: PostMethod filePost = new PostMethod(targetURL);
148:
149: filePost.getParams().setBooleanParameter(
150: HttpMethodParams.USE_EXPECT_CONTINUE,
151: cbxExpectHeader.isSelected());
152: try {
153: appendMessage("Uploading "
154: + targetFile.getName() + " to "
155: + targetURL);
156: Part[] parts = { new FilePart(targetFile
157: .getName(), targetFile) };
158: filePost
159: .setRequestEntity(new MultipartRequestEntity(
160: parts, filePost.getParams()));
161: HttpClient client = new HttpClient();
162: client.getHttpConnectionManager().getParams()
163: .setConnectionTimeout(5000);
164: int status = client.executeMethod(filePost);
165: if (status == HttpStatus.SC_OK) {
166: appendMessage("Upload complete, response="
167: + filePost
168: .getResponseBodyAsString());
169: } else {
170: appendMessage("Upload failed, response="
171: + HttpStatus.getStatusText(status));
172: }
173: } catch (Exception ex) {
174: appendMessage("ERROR: "
175: + ex.getClass().getName() + " "
176: + ex.getMessage());
177: ex.printStackTrace();
178: } finally {
179: filePost.releaseConnection();
180: }
181:
182: }
183: });
184:
185: getContentPane().setLayout(new GridBagLayout());
186: GridBagConstraints c = new GridBagConstraints();
187:
188: c.anchor = GridBagConstraints.EAST;
189: c.fill = GridBagConstraints.NONE;
190: c.gridheight = 1;
191: c.gridwidth = 1;
192: c.gridx = 0;
193: c.gridy = 0;
194: c.insets = new Insets(10, 5, 5, 0);
195: c.weightx = 1;
196: c.weighty = 1;
197: getContentPane().add(lblURL, c);
198:
199: c.anchor = GridBagConstraints.WEST;
200: c.fill = GridBagConstraints.HORIZONTAL;
201: c.gridwidth = 2;
202: c.gridx = 1;
203: c.insets = new Insets(5, 5, 5, 10);
204: getContentPane().add(cmbURL, c);
205:
206: c.anchor = GridBagConstraints.EAST;
207: c.fill = GridBagConstraints.NONE;
208: c.insets = new Insets(10, 5, 5, 0);
209: c.gridwidth = 1;
210: c.gridx = 0;
211: c.gridy = 1;
212: getContentPane().add(lblTargetFile, c);
213:
214: c.anchor = GridBagConstraints.CENTER;
215: c.fill = GridBagConstraints.HORIZONTAL;
216: c.insets = new Insets(5, 5, 5, 5);
217: c.gridwidth = 1;
218: c.gridx = 1;
219: getContentPane().add(tfdTargetFile, c);
220:
221: c.anchor = GridBagConstraints.WEST;
222: c.fill = GridBagConstraints.NONE;
223: c.insets = new Insets(5, 5, 5, 10);
224: c.gridwidth = 1;
225: c.gridx = 2;
226: getContentPane().add(btnSelectFile, c);
227:
228: c.anchor = GridBagConstraints.CENTER;
229: c.fill = GridBagConstraints.NONE;
230: c.insets = new Insets(10, 10, 10, 10);
231: c.gridwidth = 3;
232: c.gridx = 0;
233: c.gridy = 2;
234: getContentPane().add(cbxExpectHeader, c);
235:
236: c.anchor = GridBagConstraints.CENTER;
237: c.fill = GridBagConstraints.NONE;
238: c.insets = new Insets(10, 10, 10, 10);
239: c.gridwidth = 3;
240: c.gridx = 0;
241: c.gridy = 3;
242: getContentPane().add(btnDoUpload, c);
243:
244: c.anchor = GridBagConstraints.CENTER;
245: c.fill = GridBagConstraints.BOTH;
246: c.insets = new Insets(10, 10, 10, 10);
247: c.gridwidth = 3;
248: c.gridheight = 3;
249: c.weighty = 3;
250: c.gridx = 0;
251: c.gridy = 4;
252: getContentPane().add(new JScrollPane(taTextResponse), c);
253: }
254:
255: private void appendMessage(String m) {
256: taTextResponse.append(m + "\n");
257: }
258: }
259: }
|