Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>// For upload Image in Struts2 // Jsp Page is: &lt;s:form method="post" action="test.action" enctype="multipart/form-data"&gt; &lt;s:file name="imageFile" label="User Image" /&gt; &lt;s:submit value="submit"&gt;&lt;/s:submit&gt; //Struts.xml &lt;struts&gt; &lt;package name="default" extends="struts-default"&gt; &lt;action name="test" class="Test"&gt; &lt;result name="success"&gt;welcome.jsp&lt;/result&gt; &lt;/action&gt; &lt;/package&gt; &lt;/struts&gt; //Test.java import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.StringTokenizer; import com.opensymphony.xwork2.ActionSupport; import org.apache.commons.io.FileUtils; public class test extends ActionSupport{ private static final long serialVersionUID = 1L; private File imageFile; public File getImageFile() { return imageFile; } public void setImageFile(File imageFile) { this.imageFile = imageFile; } public String execute()throws Exception { try{ //code for image random name // start from here to DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); String dt = format.format(date); String name = ""; StringTokenizer str = new StringTokenizer(dt); while (str.hasMoreElements()) { String nm=(String) str.nextElement(); name+=nm; } String name1=""; StringTokenizer strg = new StringTokenizer(name,"/"); while (strg.hasMoreElements()) { String nam=(String) strg.nextElement(); name1+=nam; } String imgname=""; StringTokenizer strge = new StringTokenizer(name1,":"); while (strge.hasMoreElements()) { String na=(String) strge.nextElement(); imgname+=na; } //code for copy image to specific path String sourceFilePath=imageFile.getAbsolutePath(); //System.out.println(sourceFilePath); File sourceFile=new File(sourceFilePath); File destnationFile=new File("E:/Jaydip_Baldha/workspace_new/Struts2Upload/WebContent/upload_image/"+imgname+".jpg"); FileUtils.copyFile(sourceFile, destnationFile); } catch(Exception e) { e.printStackTrace(); } return SUCCESS; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload