Hello sir, I am stuck with a problem of uploading multiple images on server. i have done a code which works fine for uploading single image,but it doesn't work with uploading multiple images dynamically on server. one live example of this is like facebook uploading images. so please help me to solve this problem.
1)MultipleFileUpload.java
import java.io.File; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class MultipleFileUpload extends ActionSupport{ private List<File> fileUpload = new ArrayList<File>(); private List<String> fileUploadContentType = new ArrayList<String>(); private List<String> fileUploadFileName = new ArrayList<String>(); public List<File> getFileUpload() { return fileUpload; } public void setFileUpload(List<File> fileUpload) { this.fileUpload = fileUpload; } public List<String> getFileUploadContentType() { return fileUploadContentType; } public void setFileUploadContentType(List<String> fileUploadContentType) { this.fileUploadContentType = fileUploadContentType; } public List<String> getFileUploadFileName() { return fileUploadFileName; } public void setFileUploadFileName(List<String> fileUploadFileName) { this.fileUploadFileName = fileUploadFileName; } public String upload() throws Exception{ for (File file: fileUpload) { System.out.println("File :" + file); } for (String fileName: fileUploadFileName) { System.out.println("Filename : " + fileName); } for (String fileContentType: fileUploadContentType) { System.out.println("File type : " + fileContentType); } return SUCCESS; } public String display() { return NONE; } }
2)fileupload.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <s:head /> </head> <body> <h1>Struts 2 upload multiple files example</h1> <s:form action="resultAction" namespace="/" method="POST" enctype="multipart/form-data"> <s:file label="File 1" name="fileupload" size="40" /> <s:file label="File 2" name="fileupload" size="40" /> <s:file label="FIle 2" name="fileupload" size="40" /> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
3)result.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 upload multiple files example</h1> <h4> File Name : <s:property value="fileUploadFileName"/> </h4> <h4> Content Type : <s:property value="fileUploadContentType"/> </h4> <h4> File : <s:property value="fileupload"/> </h4> </body> </html>
4)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.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="multipleFileUpload" class="../MultipleFileUpload" method="display"> <result name="none">pages/fileupload.jsp</result> </action> <action name="resultAction" class="../MultipleFileUpload" method="upload"> <result name="success">pages/result.jsp</result> </action> </package> </struts>
Ads