Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

How to upload with Postlet Applet in Java

05.03.2011
| 4345 views |
  • submit to reddit
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" />
</applet>

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!"); }
</script>

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();
answer.append("POSTLET REPLY\nPOSTLET:YES\nEND POSTLET REPLY\n");

PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires",0);

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

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

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,
originalFilename.length());

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

out.println(answer);

}catch (Exception exception)
{
answer.delete(0, answer.length());
answer.append("POSTLET REPLY\nPOSTLET:NO\nPOSTLET:SERVER ERROR\n
POSTLET:ABORT ALL\nEND POSTLET REPLY\n");

out.println(answer);
} 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

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Igor Polevoy replied on Sat, 2011/05/07 - 11:11pm

there was a time when we used applets extensively...in the past century.... I developed a number of sophisticated projects using applets, until Sun successfully killed this great technoology. As of today, this is one of those dead things that keep floating up once in a while. Browser support (lack thereof) and pain to install JRE by regular folks is so great, it absolutely kills any prospects of using applets... and that is really unfortunate. We could do in applets 10 years ago what Flex developers do today, only because Adobe knows how to distribute a VM and SUN does not!

 igor

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.