Seeking Oracle out of the matrix

Google
 
Web aboutoracle.blogspot.com
Oracle New Articles
Oracle Critical Patches
Oracle jDeveloper News
Oracle Blogs of interest


This blog records my personal experience.
Latest news from the front
               

Thoughts about oracle Author's note : Thank you all for your supporting letters. You have been a motivation to this web spot's posting. I wish you all the very best for you and your families for the new year.
Interested to know about a next post? You can suscribe to my RSS feed
HOW TO:

TIP: Upload a file using struts (includes jdeveloper 10g complete example)
Wednesday, December 06, 2006
Struts simplify the file uploading process really easy.
You dont need third party stuff; just a few lines of code and a couple of tricks
will do the job :)

I have included a sample project for you to download and experiment with.

Key parts are :

1. Your form that will include the <html:file> tag, must be configured to handle multipart/form-data and to POST the data to the server, eg :

<html:form enctype="multipart/form-data" method="POST" action="upload.do">


2. The form bean that will handle the uploading, must have a property for the file been uploaded set like this :

<form-property name="filename" type="org.apache.struts.upload.FormFile">


After that, coding is really simple. I have included a complete project for uploads at the end of this post.
Here follows the java source part for the upload file process:



package fileuploader.view;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.text.DecimalFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;

public class UploadAction extends Action
{
/**
* This is the main action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws javax.servlet.ServletException
* @throws java.io.IOException
* @return
*/

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, Exception
{
String toPage = "";
DynaActionForm df = null;
FormFile webFile = null;
FileOutputStream ServerFileStream = null;
File SaveToFile = null;
int FILE_SIZE_LIMIT = 5000000;
double filesize = 0 ;

try
{
//convert to a DynaActionForm
df = (DynaActionForm) form;
//class cast to a struts type FormFile filename
webFile = (FormFile) df.get("filename");

//get the size of the file that user wants to upload
filesize = webFile.getFileSize();

//i have included a file size limitation in this version
if (filesize > FILE_SIZE_LIMIT )
{
//format the size of the file to return two decimals
String pattern = "####.##";
DecimalFormat dblFormat = new DecimalFormat(pattern);
//file size returned to megabytes
String strFileSize = dblFormat.format((filesize / 1024) / 1024);

//redirect to the page that informs the user that there is a
//file size limitation
toPage = "filesizepage";
request.setAttribute("fsize", strFileSize);
return mapping.findForward(toPage);
}
//get the real path of the project
String strFilePath = getServlet().getServletContext().getRealPath("/");
//we want to upload to UploadedFiles directory
strFilePath = strFilePath + "UploadedFiles";

//initialize a file to write to
SaveToFile = new File(strFilePath, webFile.getFileName());


//in this version, if the file to upload already exists on the server
//i delete the server-side one and reupload it
if(SaveToFile.exists())
{
//delete the file
SaveToFile.delete();
}
//save the file to server
ServerFileStream = new FileOutputStream(SaveToFile);
ServerFileStream.write(webFile.getFileData());
ServerFileStream.flush();
ServerFileStream.close();
ServerFileStream = null;

toPage="success";
}
catch(Exception e)
{
//just print the trace
e.printStackTrace();
//and redirect to an error page
toPage = "error";
}
finally
{
//release resources
df = null;
webFile = null;
ServerFileStream = null;
SaveToFile = null;
}

return mapping.findForward(toPage);

}
}



Download the full jdeveloper sample project
(right-click and select "save target as")

just remember to rename it to .rar

posted by Admin @ 10:42 PM  
add to del.icio.us Digg it! Furl this! add to reddit! add to dzone!
5 Comments:
  • At 3:38 AM, Anonymous Anonymous said…

    Thanks for the source man

     
  • At 3:39 AM, Anonymous Anonymous said…

    Yeah, really cool source..
    By the way, how did you do it?

     
  • At 10:12 PM, Anonymous Anonymous said…

    :-)

     
  • At 6:56 PM, Blogger mc jabzter said…

    yeah really cool source comment at kingjabzter.blogspot.com

     
  • At 5:35 AM, Blogger Unknown said…

    Thanks a lot for your help! I spent one day in resolving this issue.

     
Post a Comment
<< Home

About Me

Name: John Galanopoulos
Home: The NeverLands
About Me: A source code wonderer since the early 80s with my first ZX81 by Sinclair, home computer.
See my complete profile
Previous Post
Archives
Links
ΣΚΛΗΡΥΝΣΗ ΚΑΤΑ ΠΛΑΚΑΣ - ΕΓΚΕΦΑΛΟΣ - ΕΓΚΕΦΑΛΟΓΡΑΦΗΜΑ - ΑΝΟΙΑ - ΝΕΥΡΟΛΟΓΟΣ - ΨΥΧΙΑΤΡΟΣ - ΛΟΙΜΩΔΗΣ ΜΟΝΟΠΥΡΗΝΩΣΗ - ΠΑΡΚΙΝΣΟΝ - ΑΓΧΟΣ - ΚΑΤΑΘΛΙΨΗ - ALZHEIMER - EPSTEIN BARR Eurolife
Email notification

Enter your email address and get notified whenever there is a new post:

Delivered by FeedBurner

This website abides by a strict policy : no spam, just posts; and that's a promise.

Powered by

Free Blogger Templates

BLOGGER


Register for a skinnyscore at www.blogskinny.com and increase traffic
Software Blogs -  Blog Catalog Blog Directory

© 2005 Seeking Oracle out of the matrix Template by Isnaini Dot Com