JavaRocks

File Upload using Webwork

If you are using webwork as your web framework and you have a requirement to upload file, webwork can help you.

Webwork comes with inbuilt file Upload interceptor. This interceptor can be used easily into your web application.

When you are using fileUpload of webwork, make changes in webwork.properties present in your classpath

webwork.properties
webwork.multipart.parser=jakarta
webwork.multipart.maxSize=2097152    //in bytes


Interceptor that is based off of MultiPartRequestWrapper, which is automatically applied for any request that includes a file. It adds the following parameters, where [File Name] is the name given to the file uploaded by the HTML form:
·    [File Name] : File - the actual File
·    [File Name]ContentType : String - the content type of the file
·    [File Name]FileName : String - the actual name of the file uploaded (not the HTML name)
i.e when u upload the file, you get filename, contenttype and file directly in your action class. You just need to provide setter method for them.

In this example, I will show you how to upload a file from html page into database blob column.

Step 1: Create table Files with attributes id(integer), filename(varchar), content(blob)

Step 2: Now create a html page to upload the file.

fileUpload.html

<html>
<body>
<form action="fileUpload.action" name="uploadForm" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>File To Uploads</td>
            <td><input type="file" name="doc" id="doc"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Upload"></td>
        </tr>
    </table>
</form>
</body>
</html>


Step 3: Create a webwork action class to do the work of file uploading.

public class FileUploadAction extends ActionSupport {
    private File doc;
    private String docFileName;
    
public String execute() {
        InputStream is = new FileInputStream(doc);
        byte[] result = new byte[is.available()];
        is.read(result);
        
////jdbc code////

Connection db_connection = //get connection from anywhere.
Statement db_statement = db_connection.createStatement();
db_statement.executeUpdate("insert into files values (1,docFileName,reslut);");

///jdbc code ends/////


you can also use hibernate to store the file content.

//hibernate code////Session session=//get session from anywhere.Files files=new Files();  //create ‘Files’ Pojo class for ‘Files’ table.files.setid(1);files.setFileName(docFileName);files.setContent(result);session.save(files);//commit transaction, flush session, close session//hibernate code ends////        return SUCCESS;    }

Step 4: Mapping your action in xwork

<xwork>
    <include file="webwork-default.xml"/>
    <package name="default" extends="webwork-default">
        <default-interceptor-ref name="basicStack"/>

        <action name="fileUpload" class="FileUploadAction">
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">4194304</param> //size is in bytes.
            </interceptor-ref>
            <interceptor-ref name="basicStack"/>
            <result name="success">/successPage.html </result>
        </action>
</package>
</xwork>


Step 5: Run the application

Start your application server and run the fileUpload.html. Give the file path and click upload button. The action will be called and the file content would be upload to your database blob column.