Tech I Enjoy Logo

Custom Search




  Home >> Struts


Let us explore file upload specific tag from Struts2
With this example, I shall try to touch upon a very specific
functionality of Struts2 as far as file upload is concerned.

But I am presently struck up with a very common requirement
of mine whereby I shall try to write sample code and try
to get the file name that is being chosen from the browser-side
of the GUI, and use this file name to create the final
file at the server-side.

Like in this example I shall be using some input/output
stream in order to re-create the file that is being sent/uploaded.
But in this way of thinking I am basically using some hard
coded file name by presuming the file name this would be,
which is I think is not required.

Coming back to this example, I shall try to explain various files used in this example, starting from the JSP file to the Action class and the struts.xml file. sample-upload.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/sample.css" />
</head>
<body>
<struts2:actionerror cssClass="ss"/>
<struts2:form id="f" name="fUpload" 
                     enctype="multipart/form-data" 
                     method="post" action="demo" 
                     namespace="/upload-example">
<struts2:file name="fileUploaded"/>
<struts2:submit value="Upload"/>
</struts2:form>
</body>
</html>
In the above JSP file form tag from Struts2 Framework is used. Various attributes of form tag such as enctype, method, namespace and action should have corresponding values as shown above. struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.saveDir" value="c:/tmp" />
    <package name="upload-example" namespace="/upload-example"
                 extends="struts-default">
        <action name="demo" class="sample.ExampleUploadFile" 
	                    method="saveFile">
                <result name="success">/sample1.jsp</result>
        </action>
    </package>
</struts>
Strut's configuration file is having a constant defined with the value as the folder path. package tag contains the name and namespace as upload-example. ExampleUploadFile.java
package sample;
import com.opensymphony.xwork2.ActionSupport;
import java.io.*;
import org.apache.struts2.interceptor.ParameterAware;
import java.util.Map;

public class ExampleUploadFile extends ActionSupport 
{
  private File fileUploaded;

  public void setFileUploaded(File argFile) {
      fileUploaded = argFile;
  }
  public File getFileUploaded() {
      return fileUploaded;
  }
  public String saveFile() {
      System.out.println("File name :"+fileUploaded.getName());
      try{
	    //This is the hard coded file name with path
		//which I was refering about, in my this writing.
		//I am not sure how to avoid these lines of code
		//in order to upload a file from client end 
		//to server end without knowing the file choosen.
		//and ways to find automatically moved the file
		//being uploaded to its destination.
        DataOutputStream dout = new DataOutputStream(
                      new FileOutputStream("c:/tmp1/test.pdf"));
        DataInputStream din = new DataInputStream(
                      new FileInputStream(fileUploaded));
        byte[] bt = new byte[1024];
        while(din.read(bt) != -1) {
          dout.write(bt,0,bt.length);
        }
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      return "success";
  }
}
Above user-defined action support class file gets the File object and writes the contents of uploaded file to the output file. Please be informed that this is not the only way of doing this task, there may be many other and better ways of coding any given problem on hand. As I have already mentioned, I am still looking for some ways (if any) to achieve file upload using the file tag from browser to the server's predefined folder, and there will be no need to code file recreation as is done in this action class's saveFile method. If you are aware or able to do this using Struts2 , then please do share your thought on this (if you can). Please drop an email to me at usingframeworks @ gmail . com If anything missed out , please let me know at techienjoy at yahoo . com
Apache Struts 2 PDF Result :
Example Steps of using Struts 2 with PDF Result
and code explained.
Apache Struts 2 Result PlainText Example :
Example Steps of using Struts 2 PlainText Result
and code explained.
Apache Struts with DOJO Example Part-1 :
Example of DOJO Toolkit with Struts and 
code explained.
List of Struts Example :
Examples List using Struts 2 and 
code explained.
Struts Validation Example :
Example using Validation with Struts 2 and 
code explained.
Apache Struts 2 Custom Validator :
Example Steps of using Struts 2 with Custom Validator
and code explained.
Apache Struts 2 upload Example :
Uploading Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 Validation With Expression :
Validation with Expression using Struts 2
and Example code explained.
Apache Struts with DOJO Example Part-2 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts with DOJO Example Part-3 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Validation With Example :
Validation Example using Struts 2
and Example code explained.
Apache Struts 2 Tags Example :
Example of Struts 2 Tags and code explained.
Apache Struts 2 Example Steps :
Example Steps of using Struts 2 and code explained.
Apache Struts 2 Radio Tag Example :
Example Steps of using Struts 2 Radio Tags
and code explained.
Apache Struts 2 Tiles and I18N Example :
Example Steps of using Struts 2 With Tiles
and I18N code explained.
Apache Struts Validation with Example :
Example of Validation using Struts and 
code explained.
Apache Struts 2 and XSLT With Example :
XSLT Example using Struts 2 and Example 
code explained.
Struts 2 Tiles and I18N Example :
Example using Tiles and I18N with Struts 2 and 
code explained.
Apache Struts Date Validation with Example :
Example of Date Validation using Struts and 
code explained.
Apache Struts 2 Tags Example :
Example Steps of using Struts 2 Tags
and code explained.
Struts 2 Plugin Example :
Example using Struts 2 Plugin
Apache Struts with DOJO Example Part-4 :
Example of DOJO Toolkit with Struts and 
code explained.
Struts 2 with JSF Example :
Example using JSF with Struts 2 and 
code explained.
Struts 2 Result Chain Example :
Example using Result Chain with Struts 2 and 
code explained.
Apache Struts 2 Validation Example :
Validation Example Steps of using Struts 2
and Example code explained.


References :
Tags: Struts 1 Struts2 plugin
Tags: struts jsf integrate
Tags: struts result chain
Tags: struts tiles i18n
Tags: Struts validation struggle
Tags: Struts
Tags: struts1 date validation
Tags: struts1 struts2 validation
Tags: struts2 dojo 1
Tags: struts2 dojo 2
Tags: struts2 dojo 3
Tags: struts2 dojo 4
Tags: Struts2 example steps Tag usage
Tags: Struts2 example steps
Tags: struts2 own validator
Tags: struts2 pdf result
Tags: struts2 radio tag example
Tags: struts2 result plaintext
Tags: struts2 tags example
Tags: struts2 tiles i18n
Tags: struts2 upload example
Tags: struts2 validation 1
Tags: struts2 validation expression
Tags: struts2 validation
Tags: struts2 xslt article



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions