How to upload with Postlet Applet in Java

DZone 's Guide to

How to upload with Postlet Applet in Java

· Java Zone ·
Free Resource
Postlet is a Java applet used for uploading files to web servers. It supports multiple file uploadusing a nice and easy to use design. Postlet provide 4 main features:

• File progress bar
• Error checking
• Easy selection and upload of multiple files
• Resizing of images over a set size

For a start, you must download Postlet, the Java applet upload, from http://postlet.com/. Extract the postlet.jar under your web application.

The applet is customizable through a set of parameters:

• destination - which is the URL to your servlet that will accept file upload
• language - which is the language to use for Postlet's user interface
• backgroundcolour - which is the color to use for Postlet background
• tableheadercolour - which is the colour to use for the text of the Table Headers
• tableheaderbackgroundcolour - which is the colour to use for the background of the Table Headers
• fileextensions - which is a list of the file extensions
• warnmessage - which is the ability to show a warning message once the upload button is pressed. It’s value can be “true” or “false”
• autoupload - which is the ability to automatically upload files once they are selected. It’s value can be “true” or “false”
• helpbutton - which is the ability to show a help button. It works on versions newer than 0.10.0 and it’s value can be “true” or “false”
• maxpixels - which is the maximum number of pixels allowed for GIF, JPEG and PNG images when there are uploaded. If files exceed this limit, they are resized to the set limit. If are resized GIF images, there are uploaded as PNG images. It works on version newer than 0.13.
• helppage - which is the URL to show if the user clicks the help button. The default URL is to http://www.postlet.com/help/
• maxthreads - which is the maximum number of connections to use. It can takes values from 1 to 5.
• Endpage - which is the URL to take the user to once upload has completed.
• Failedfilesmessage - which is the ability to show the popup message listing the files that failed to upload

A possible configuration is listed below:
<applet name="postlet" code="Main.class" 
archive="./applets/postlet.jar" width="850" height="150" mayscript>
<param name = "maxthreads" value = "5" />
<param name = "language" value = "" />
<param name = "type" value = "application/x-java-applet;version=1.3.1" />
<param name = "destination"
value = "http://localhost:8085/PostletUpload/applet_upload" />
<param name = "backgroundcolour" value = "56328145" />
<param name = "tableheaderbackgroundcolour" value = "24279327" />
<param name = "tableheadercolour" value = "0" />
<param name = "warnmessage" value = "false" />
<param name = "autoupload" value = "false" />
<param name = "helpbutton" value = "false" />
<param name = "fileextensions" value = "Image Files,jpg,gif,jpeg" />
<param name = "endpage" value = "[*** ENDPAGE URL***]" />
<param name = "helppage"
value = "http://www.postlet.com/help/?thisIsTheDefaultAnyway" />

When the uploads ends, a JavaScript function can fire up a message:

<script type="text/javascript">
function postletFinished(){
alert("Postlet Applet Succesfully Accomplished its Job!"); }

For this application (on the server side) you can use the COS described in this post How to upload in Java (Upload using the COS library). Notice the messages returned by the server should by specific to Postlet Upload – http://postlet.com/ you can see the entire list of possible responses. The COS implementation for Postlet Upload is:
public class AppletUploadServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

final int permitedSize = 314572800; //~ 300 MB bytes
final String[] extensions = {".zip",".xls"};

StringBuffer answer = new StringBuffer();

PrintWriter out = response.getWriter();
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "No-cache");

String strDirectory = "files";
String uploadPath = request.getRealPath("//WEB-INF//"+strDirectory+"//");

//or, like this, from web.xml
String uploadPath_rezerva = request.getRealPath(getServletConfig().

MultipartRequest multipartRequest = new MultipartRequest(request,
uploadPath, permitedSize, "ISO-8859-1", new DefaultFileRenamePolicy());

Enumeration files = multipartRequest.getFileNames();

String extension1 = "";
String extension2 = "";
String filename = "";

while (files.hasMoreElements())
String name = (String)files.nextElement();
filename = multipartRequest.getFilesystemName(name);
String originalFilename = multipartRequest.getOriginalFileName(name);

extension1 = filename.substring(filename.length() - 4, filename.length());
extension2 = originalFilename.substring(originalFilename.length() - 4,

//use O'Reilly COS
File currentFile = multipartRequest.getFile(name);


}catch (Exception exception)
answer.delete(0, answer.length());

} finally { if(out != null) {out.close();} }

The result of this application is shown below in figure below:

Download a complete example tested under Tomcat 6 from here.


From http://e-blog-java.blogspot.com/2011/03/how-to-upoad-with-postlet-applet-in.html


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}