1 30 31 import java.awt.GridBagConstraints ; 32 import java.awt.GridBagLayout ; 33 import java.awt.Insets ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.WindowAdapter ; 37 import java.awt.event.WindowEvent ; 38 import java.io.File ; 39 40 import javax.swing.DefaultComboBoxModel ; 41 import javax.swing.JButton ; 42 import javax.swing.JComboBox ; 43 import javax.swing.JFileChooser ; 44 import javax.swing.JFrame ; 45 import javax.swing.JLabel ; 46 import javax.swing.JScrollPane ; 47 import javax.swing.JTextArea ; 48 import javax.swing.JTextField ; 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 64 public class MultipartFileUploadApp { 65 66 public static void main(String [] args) { 67 MultipartFileUploadFrame f = new MultipartFileUploadFrame(); 68 f.setTitle("HTTP multipart file upload application"); 69 f.pack(); 70 f.addWindowListener( 71 new WindowAdapter () { 72 public void windowClosing(WindowEvent e) { 73 System.exit(0); 74 } 75 } 76 ); 77 f.setVisible(true); 78 } 79 80 public static class MultipartFileUploadFrame extends JFrame { 81 82 private File targetFile; 83 private JTextArea taTextResponse; 84 private DefaultComboBoxModel cmbURLModel; 85 86 public MultipartFileUploadFrame() { 87 String [] aURLs = { 88 "http://localhost:8080/httpclienttest/fileupload" 89 }; 90 91 cmbURLModel = new DefaultComboBoxModel (aURLs); 92 final JComboBox cmbURL = new JComboBox (cmbURLModel); 93 cmbURL.setToolTipText("Enter a URL"); 94 cmbURL.setEditable(true); 95 cmbURL.setSelectedIndex(0); 96 97 JLabel lblTargetFile = new JLabel ("Selected file:"); 98 99 final JTextField tfdTargetFile = new JTextField (30); 100 tfdTargetFile.setEditable(false); 101 102 final JButton btnDoUpload = new JButton ("Upload"); 103 btnDoUpload.setEnabled(false); 104 105 final JButton btnSelectFile = new JButton ("Select a file..."); 106 btnSelectFile.addActionListener( 107 new ActionListener () { 108 public void actionPerformed(ActionEvent evt) { 109 JFileChooser chooser = new JFileChooser (); 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 (10, 40); 128 taTextResponse.setEditable(false); 129 130 final JLabel lblURL = new JLabel ("URL:"); 131 132 btnDoUpload.addActionListener(new ActionListener () { 133 public void actionPerformed(ActionEvent ae) { 134 String targetURL = cmbURL.getSelectedItem().toString(); 135 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 ex) { 162 appendMessage("Error: " + ex.getMessage()); 163 ex.printStackTrace(); 164 } finally { 165 filePost.releaseConnection(); 166 } 167 168 } 169 }); 170 171 getContentPane().setLayout(new GridBagLayout ()); 172 GridBagConstraints c = new GridBagConstraints (); 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 (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 (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 (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 (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 (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 (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 (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 (taTextResponse), c); 231 } 232 233 private void appendMessage(String m) { 234 taTextResponse.append(m + "\n"); 235 } 236 } 237 } 238 | Popular Tags |