I am making an app that uploads a file on the server, the app has 2 buttons, one is browse and the other is upload. But when i hit the upload button it crashes. I get these errors:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/http/Consts at org.apache.http.entity.mime.MIME.(MIME.java:48) at org.apache.http.entity.mime.MultipartEntity.(MultipartEntity.java:71) at org.apache.http.entity.mime.MultipartEntity.(MultipartEntity.java:91) at UploadFile.(UploadFile.java:27) at Frame$3.actionPerformed(Frame.java:64) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.lang.ClassNotFoundException: org.apache.http.Consts at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 41 more
The code from the buttons:
JButton btnNewButton = new JButton("Browse");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser x = new JFileChooser();
if(x.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
String file = x.getSelectedFile().toString();
textField.setText(file);
}
}
});
btnNewButton.setBounds(182, 11, 89, 23);
frame.getContentPane().add(btnNewButton);
JButton btnUpload = new JButton("Upload");
btnUpload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new UploadFile(textField.getText());
}
});
The upload function:
@SuppressWarnings("deprecation")
public class UploadFile {
public UploadFile(String alamat) {
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("URL");
File file = new File(alamat);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "text/plain");
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
System.out.println("Executing request " + httppost.getRequestLine());
HttpResponse response;
try {
response = httpClient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
} catch (IOException ex) {
Logger.getLogger(UploadFile.class.getName()).log(Level.SEVERE, null, ex);
}
httpClient.getConnectionManager().shutdown();
JOptionPane.showMessageDialog(null, "file uploaded succesfully");
}
}