Upload file (subir fichero)

index.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
<html>
	<head><title>Struts2 File Upload Example</title></head>
	<body>
		<s:actionerror/>
		<s:fielderror/>
		<s:form action="UploadMyFile" enctype="multipart/form-data" method="POST">
			<s:file name="uploadFile" label="Please select a file to upload"></s:file>
			<s:submit></s:submit>
		</s:form>
	</body>
</html>

struts.xml

<action name="UploadMyFile" class="com.pablomonteserin.action.FileUploadAction">
	<interceptor-ref name="defaultStack">
		<param name="fileUpload.allowedTypes">text/plain,image/png,image/gif,image/jpeg</param>
		<param name="fileUpload.maximumSize">200000</param>
	</interceptor-ref>
	<result name="success">/uploadedFile.jsp</result>
</action>
FileUploadAction.java
//Habría que poner los getters y setters de fileSize(long), uploadFileFileName(String) y uploadFile(File)
public String execute() throws Exception {
	String filePath = "/home/monty/Desktop/"+uploadFileFileName;
	File newFile = new File(filePath);
	FileUtils.copyFile(uploadFile, newFile);
	return SUCCESS;
}

combobox tag

<s:form>
	Para que funcione correctamente, el combobox debe estar dentro de un formulario, ya que dicho formulario será referenciado en el código javascript que genera la etiqueta
	<s:combobox label="My Favourite Color" name="myFavouriteColor" list="#{'red':'red','green':'green','blue':'blue'}" headerKey="-1" headerValue="--- Please Select ---" emptyOption="true" value="green" />
	<s:combobox label="Personas" name="persona" list="personas" headerKey="-1" headerValue="--- Please Select ---" listValue="nombre" listKey="id" emptyOption="true" value="Juan" />
</s:form>
← Cómo hacer una plantilla en Struts 2 con tiles
Struts 2 y Ajax →

Aviso Legal | Política de privacidad