java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use multipart POST method for uploading files E-mail
User Rating: / 36
PoorBest 

This is a Swing application that demonstrates how to use the Jakarta HttpClient multipart POST method for uploading files.

/*
 * $Header$
 * $Revision$
 * $Date$
 * ====================================================================
 *
 *  Copyright 2002-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * [Additional notices, if required by prior licensing conditions]
 *
 */

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import 
  org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 *
 * This is a Swing application that demonstrates
 * how to use the Jakarta HttpClient multipart POST method
 * for uploading files
 *
 @author Sean C. Sullivan
 @author Michael Becke
 *
 */
public class MultipartFileUploadApp {
    
    public static void main(String[] args) {
        
        MultipartFileUploadFrame f = new MultipartFileUploadFrame();
        f.setTitle("HTTP multipart file upload application");
        f.pack();
        f.addWindowListener(
                new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }
        );
        f.setVisible(true);
    }
    
    public static class MultipartFileUploadFrame extends JFrame {
        
        private File targetFile;
        private JTextArea taTextResponse;
        private DefaultComboBoxModel cmbURLModel;
        
        public MultipartFileUploadFrame() {
            
            String[] aURLs = {
                "http://localhost:8080/httpclienttest/fileupload"
            };
            
            cmbURLModel = new DefaultComboBoxModel(aURLs);
            final JComboBox cmbURL = new JComboBox(cmbURLModel);
            cmbURL.setToolTipText("Enter a URL");
            cmbURL.setEditable(true);
            cmbURL.setSelectedIndex(0);
            
            JLabel lblTargetFile = new JLabel("Selected file:");
            
            final JTextField tfdTargetFile = new JTextField(30);
            tfdTargetFile.setEditable(false);
            
            final JCheckBox cbxExpectHeader = new JCheckBox(
                    "Use Expect header");
            
            cbxExpectHeader.setSelected(false);
            
            
            final JButton btnDoUpload = new JButton("Upload");
            btnDoUpload.setEnabled(false);
            
            final JButton btnSelectFile = new JButton("Select a file...");
            btnSelectFile.addActionListener(
                    new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    
                    JFileChooser chooser = new JFileChooser();
                    chooser.setFileHidingEnabled(false);
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                    chooser.setMultiSelectionEnabled(false);
                    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
                    chooser.setDialogTitle("Choose a file...");
                    
                    if (
                            chooser.showOpenDialog(
                            MultipartFileUploadFrame.this)
                            == JFileChooser.APPROVE_OPTION
                            ) {
                        targetFile = chooser.getSelectedFile();
                        tfdTargetFile.setText(targetFile.toString());
                        btnDoUpload.setEnabled(true);
                        
                    }
                }
            }
            );
            
            taTextResponse = new JTextArea(1040);
            taTextResponse.setEditable(false);
            
            final JLabel lblURL = new JLabel("URL:");
            
            btnDoUpload.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    String targetURL = cmbURL.getSelectedItem().toString();
                    // add the URL to the combo model 
                    // if it's not already there
                    if (!targetURL
                            .equals(
                            cmbURLModel.getElementAt(
                            cmbURL.getSelectedIndex()))) {
                        cmbURLModel.addElement(targetURL);
                    }
                    
                    PostMethod filePost = new PostMethod(targetURL);
                    
                    filePost.getParams().setBooleanParameter(
                            HttpMethodParams.USE_EXPECT_CONTINUE,
                            cbxExpectHeader.isSelected());
                    
                    try {
                        
                        appendMessage("Uploading " + targetFile.getName() 
                                " to " + targetURL);
                        
                        Part[] parts = {
                            new FilePart(targetFile.getName(), targetFile)
                        };
                        
                        filePost.setRequestEntity(
                                new MultipartRequestEntity(parts, 
                                filePost.getParams())
                                );
                        
                        HttpClient client = new HttpClient();
                        client.getHttpConnectionManager().
                                getParams().setConnectionTimeout(5000);
                        
                        int status = client.executeMethod(filePost);
                        
                        if (status == HttpStatus.SC_OK) {
                            appendMessage(
                                    "Upload complete, response=" 
                                    filePost.getResponseBodyAsString()
                                    );
                        else {
                            appendMessage(
                                    "Upload failed, response=" 
                                    HttpStatus.getStatusText(status)
                                    );
                        }
                    catch (Exception ex) {
                        appendMessage("Error: " + ex.getMessage());
                        ex.printStackTrace();
                    finally {
                        filePost.releaseConnection();
                    }
                    
                }
            });
            
            getContentPane().setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            
            c.anchor = GridBagConstraints.EAST;
            c.fill = GridBagConstraints.NONE;
            c.gridheight = 1;
            c.gridwidth = 1;
            c.gridx = 0;
            c.gridy = 0;
            c.insets = new Insets(10550);
            c.weightx = 1;
            c.weighty = 1;
            getContentPane().add(lblURL, c);
            
            c.anchor = GridBagConstraints.WEST;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridwidth = 2;
            c.gridx = 1;
            c.insets = new Insets(55510);
            getContentPane().add(cmbURL, c);
            
            c.anchor = GridBagConstraints.EAST;
            c.fill = GridBagConstraints.NONE;
            c.insets = new Insets(10550);
            c.gridwidth = 1;
            c.gridx = 0;
            c.gridy = 1;
            getContentPane().add(lblTargetFile, c);
            
            c.anchor = GridBagConstraints.CENTER;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.insets = new Insets(5555);
            c.gridwidth = 1;
            c.gridx = 1;
            getContentPane().add(tfdTargetFile, c);
            
            c.anchor = GridBagConstraints.WEST;
            c.fill = GridBagConstraints.NONE;
            c.insets = new Insets(55510);
            c.gridwidth = 1;
            c.gridx = 2;
            getContentPane().add(btnSelectFile, c);
            
            c.anchor = GridBagConstraints.CENTER;
            c.fill = GridBagConstraints.NONE;
            c.insets = new Insets(10101010);
            c.gridwidth = 3;
            c.gridx = 0;
            c.gridy = 2;
            getContentPane().add(cbxExpectHeader, c);
            
            
            c.anchor = GridBagConstraints.CENTER;
            c.fill = GridBagConstraints.NONE;
            c.insets = new Insets(10101010);
            c.gridwidth = 3;
            c.gridx = 0;
            c.gridy = 3;
            getContentPane().add(btnDoUpload, c);
            
            c.anchor = GridBagConstraints.CENTER;
            c.fill = GridBagConstraints.BOTH;
            c.insets = new Insets(10101010);
            c.gridwidth = 3;
            c.gridheight = 3;
            c.weighty = 3;
            c.gridx = 0;
            c.gridy = 4;
            getContentPane().add(new JScrollPane(taTextResponse), c);
        }
        
        private void appendMessage(String m) {
            taTextResponse.append(m + "\n");
        }
    }
}

Source: Apache HttpClient


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.