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