background preloader

File upload

Facebook Twitter

HttpClient and FileUpload. Adapted from: Pro Jakarta Commons, by Harshad Oak Publisher: Apress ISBN: 1590592832 All communication over the Internet happens using a standard set of protocols, such as File Transfer Protocol (FTP), Simple Mail Transfer Protocol (SMTP), Post Office Protocol (POP), Hypertext Transfer Protocol (HTTP), and so on. HTTP is one of the most popular of these protocols and is integral to the World Wide Web.

It is also a protocol that many of today’s applications have to be aware of and use wisely. If a Java application has to interact using HTTP, the Commons HttpClient component can make things a bit easier. Using this component, you do not have to worry about all the technicalities of the protocol but just concern yourself with the various classes and methods provided by the HttpClient component. In this article you will have a look at the capabilities of the HttpClient component and also some hands-on examples. Table 9-1 shows the details for the components covered in this article. HttpClient Tutorial. Drag and Drop File Uploading Made Easy. Everyone has had the experience of uploading images to a web site.

You are given a set of 10 browse buttons and have to select each file you want to upload, or worse have to go back to the same page over and over again and select each file. Most applications provide a nice drag and drop interface which allows you to easily select multiple images (as well as other files) and add them to your documents. This article demonstrates a simple and easy way to let users of your website drag and drop files from their file system and upload them to your server. In addition to being easier for your users to use, this program will zip all the files into one archive for faster uploading. This program is a sample application with full source code, allowing you to customize and integrate this functionality into your own web application. This application has two parts: a Java applet and a Java servlet. A version of this application with PHP on the server is available on the Panyasan’s Random Musings.

Open Source Drag & Drop Upload Java Applet for Websites « Panyasan’s Random Musings. Open Source Drag & Drop Upload Java Applet for Websites With traditional HTML 4, file upload through a website is a cumbersome process, especially if you want to upload a large number of files. The recent surge of AJAX technologies has made web pages behave more like normal desktop applications, but it hasn’t been able to overcome the limitations of HTML4 since the ability of JavaScript to access the filesystem remains limited to using the INPUT tag in some form.

The most intuitive form of moving files from one location to another is to drag & drop them from a file sytem explorer window to the desired target.This is not possible on a webpage using JavaScript since JS cannot intercept such drops. Instead, there were many closed-sourced applets with restrictive and expensive licenses, and a few sites which offered code snippets. So I decided to try my luck and build on the code I found. In particular, I used code from a tutorial by Zack Grossbart. The result has the following features: How to Use File Choosers. File choosers provide a GUI for navigating the file system, and then either choosing a file or directory from a list, or entering the name of a file or directory. To display a file chooser, you usually use the JFileChooser API to show a modal dialog containing the file chooser. Another way to present a file chooser is to add an instance of JFileChooser to a container. Note: If you intend to distribute your program as a sandbox Java Web Start application, then instead of using the JFileChooser API you should use the file services provided by the JNLP API.

These services — FileOpenService and FileSaveService — not only provide support for choosing files in a restricted environment, but also take care of actually opening and saving them. An example of using these services is in JWSFileChooserDemo. Click the Launch button to run JWSFileChooserDemo using Java™ Web Start (download JDK 7 or later). When working with the JWSFileChooserDemo example, be careful not to lose files that you need.