| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
Struts2 example tutorial
Description :
My First example using Apache Struts2 More...
|
Written By : ISHTEK
Title :
Struts2
Description :
Tags such as IF Iterator Tags More...
|
Written By : guddu
Title :
Struts Eclipse MVC
Description :
Step by step demonstration More...
|
Written By : Girish
Title :
Struts Tiles I18N Example
Description :
Code with Sample Action More...
|
Written By : ISHTEK
Title :
Struts2 questions
Description :
open for discussion More...
|
Written By : ISHTEK
Title :
Struts2 Tiles Example
Description :
with Code and article More...
|
| Tags/Keywords : Struts, Struts2 Tutorial, Struts 2 Example, Struts2 Author : ISHTEK Date (Year/Month/Date): 2010-06-05
Struts 2 FileTag example | |
Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways for doing some sort of example
and may not be fit for any other purpose.
In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.
Using 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
end of GUI, and use this file name to create the final
file at the server end.
Like in this example I shall be using some input/output
stream in order to recreate 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 bit 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>
| |
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>
| |
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";
}
}
| |
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).
|
|
|
|
| Home >>> Struts Struts 2 Example Tutorial >>> Struts2 And File Upload >>> Example code discussed |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2010, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
| |  |
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Other names may be trademarks of their respective owners
This web site's Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example code, tips, tutorials etc. Like any printed materials,
content of these pages may become out of date over a period of time. Therefore all visitor/users of this web site are requested/advised
to refer to the originating parties/sources for the latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating parties/sources. These examples with source code or
without source code, have not been thoroughly tested under all conditions. Interview-questions-tips-forum.net therefore, cannot guarantee
or imply reliability of these example source code or programs.
By accessing and using this website in any ways, including, without limitation, browsing the website pages, using any information, using
any content and/or downloading any materials, you agree to and are bound by the terms of use described in this page and Usage Terms and Conditions.
If you do not agree to all of the terms and conditions contained in the terms of use described in this page and
Usage Terms and Conditions, do not use this website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being taken for providing accurate and up-to-date
information but like any printed materials, these blog(s)/contents may eventually be outdated one day, so if you are using any of these
information, please refer original content/documentation from respective sources. And under no circumstances shall the Author of these
contents and/or this web site be liable for any loss, damage, expense incurred or suffered which is claimed to have occurred because of
usage of the contents of this web site. If you have any questions/queries/feedback/suggestions then please write to this web site owner
at contact.
|  |
|
|
|
|
|