I can upload file using Struts 2.
But I want to display details (Filename
, filesize
, filetype
) about uploaded file in Struts 2.
I am trying to get details using <s:property value="myFile" />
tag,
but it's always displays [Object HTMLDocument]
.
My code is following.
uploadfile.jsp
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="success-image">
<s:property value="myFile" />
<s:head theme="ajax" />
<s:form id="asset-upload" name="asset-upload" action="uploadFile.action" method="POST" theme="ajax" enctype="multipart/form-data">
<s:file name="myFile" label="Upload Image"/>
<s:submit targets="success-image" showLoadingText="false" indicator="uploading-image" id="asdf"/>
</s:form>
</div>
</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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.multipart.maxSize" value="1000000000" />
<package name="default" extends="struts-default,json-default" namespace="/">
<action name="uploadFile" class="com.isam.struts.uploadFile">
<result name="success">
<param name="myFile">myFile</param>
</result>
<result name="error">login.jsp</result>
</action>
<action name="autoCompleterList" class="com.isam.struts.AutoCompleterList">
<result type="json" />
</action>
</package>
</struts>
UploadFile.java
:
package com.isam.struts;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class uploadFile extends ActionSupport {
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;
public String execute() {
/* Copy file to a safe location */
destPath = "C:/temp/";
try {
System.out.println("Src File name: " + myFile);
System.out.println("Dst File name: " + myFileFileName);
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);
} catch (IOException e) {
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
}
So what should I need to add in JSP file to show details,
and what should I need to add in struts.xml
file?
To show file details on the same page after uploading it change your action result to:
<action name="uploadFile" class="com.isam.struts.uploadFile">
<result name="success">uploadfile.jsp</result>
<result name="error">login.jsp</result>
</action>
And in JSP use <s:property>
tag to display values:
<s:property value="myFile.length()"/>
<s:property value="myFileContentType"/>
<s:property value="myFileFileName"/>