aspose file tools*
The moose likes Struts and the fly likes how to upload file and store the path in database using struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to upload file and store the path in database using struts2" Watch "how to upload file and store the path in database using struts2" New topic
Author

how to upload file and store the path in database using struts2

Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
I have been trying to do this task but not able to succeed. I can upload file which can store in "C:\apache-tomcat-6.0.33\work" but i don't know how to store the path in database. here is my code..
package com.rajesh.action;

import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.IOException;

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:/apache-tomcat-7.0.37/upload";

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;
}
}


here is my jsp page to upload file

<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="login" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>


here is my 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.multipart.maxSize" value="1000000" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />

<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.rajesh.action.LoginAction">
<result name="success">admin.jsp</result>
<result name="error">loginstruts.jsp</result>
</action>
<action name="upload" class="com.rajesh.action.UploadFile">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>

please help me out as i am new bee to this struts2. Any help would be greatly appreciated.

Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Hello Acharya ,
I dont see any code related to any DB operation here ? Your action class seems fine and it should be able to get the File object. You even know the path of the file on the local client machine , so what is the problem you are facing in storing this in a DB ?

I would also like to say another thing that storing client side paths in DB is bad practice and should be avoided since it exposes the file hierarchy directly.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
yes, you are right. there is no code related to database, and i do not know how to write or give connection to the database, so i am asking for assistance, please help me, i am only 1 week baby for struts2, i am facing lots of issues with struts2.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

I suggest you learn a bit about the JDBC basics. Here is the basic documentation of JDBC. You can also take a look at our JDBCFAQ for more help
Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
thanks for the suggestion sir, actually i know bit jdbc but my problem here is, which object i should pass it to the database. Nowrmally in my system jdbc connection is.
String URL = "jdbc:mysql://localhost/struts_tutorial";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, "root", "root");
String sql = "insert into file_tbl *********;
PreparedStatement ps = conn.prepareStatement(sql);

here i do not know how to give the uploaded file path to this database, please help me out and thank you very much for your instant reply, it meant lot to me. ...
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

String URL = "jdbc:mysql://localhost/struts_tutorial";


The URL should be pointing towards your hosted Database , NOT your application . The driver class you specified , I am guessing you are using MySQL . So the URL should be something similar to this

Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
String URL = "jdbc:mysql://localhost/struts_tutorial"; here struts_tutorial is my database, nothing else.. it may not look like database name but it is.
Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
.
package com.rajesh.action;

import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.IOException;

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:/apache-tomcat-7.0.37/upload";

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;
}
}


in the above code we get some object after the file upload operation and using that i want to relate to the database. I want to either store the path in database or directly data in database, both are okay for me but do not know how to achieve it... ;(
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

What more is left ? You got the connection , you got your file path and you have even your preparedStatement object ready with the SQL query. Now just execute the query . You said you wanted to save the file path in database. Make the query accordingly. SOmething similar to




Don't forget to close your statement and connection object afterwards.
Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
i am very sorry sir, i am not getting wat are you trying to say. Sir, my doubt here is "how to give a relation between action class and database"??? and my second confusion is how to set the path. thats it sir...again i am very sorry for being so less knowledgable. i recently started to learn java so.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Don't worry about being new to all this :-) We are here to share our knowledge and expertise. There was a time when I was at your position , so I do understand the problems and misunderstanding a newbie programmer can face.

Coming back to the original problem. Now , it seems that you are having some difficulty in properly explaining your problem to me.

Rajeshpurple wrote: how to give a relation between action class and database

When you say "relation" , what does it mean ? There is never any relation with a database and an action class ( or any other java class for that matter ) . There is how-ever a process called "establishing a DB connection" in which you tell your class to acquire a connection to your hosted database and perform some SQL operations on it and then finally close the connection. The code snippet you shared with me
Rajeshpurple wrote:
is absolutely correct for acquiring the DB connection to your DB . Now if your next question is that where to place this code , there are a lot of possibilities. But just for the sake of simplicity lets just put it directly in your action class ( note however , that this may or may not be appropriate , it depends on your project structure ) .



Last , comes the matter of storing your file path. Now for this , I would suggest you to try to attempt something first , then post back here ( with code ) and we'll look into the matter further. Come up with any code or any possible logic that you think may be work and well discuss on that.
Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
thank you so much sir, you have been a greatest help..... thanks a bunch,,,love you...
Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
sir, i got some errors sir, here is my code look like after your suggestion.

package com.rajesh.action;
import java.sql.*;
import java.io.*;

import org.apache.commons.io.FileUtils;
import java.io.IOException;

import com.opensymphony.xwork2.ActionSupport;

public class UploadFile extends ActionSupport{
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;
private String ID;
private String NAME;

public String execute()
{
destPath = "C:/apache-tomcat-6.0.33/work/";
Connection conn = null;
String ret= ERROR;
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;



String URL = "jdbc:mysql://localhost/struts2";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, "root", "root");
String sql = "insert into filetable values(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
File f =new File("C:/apache-tomcat-6.0.33/work");
FileReader fr = new FileReader(f);
ps.setString(1, ID);
ps.setString(2, NAME);
int i=ps.executeUpdate();
System.out.println(i+ "record affected");
ResultSet rs = ps.executeQuery();

while (rs.next()) {
ID = rs.getString(1);
ret = SUCCESS;
}
conn.close();


ret = ERROR;}







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;
}

public String getID() {
return ID;
}

public void setID(String iD) {
ID = iD;
}

public String getNAME() {
return NAME;
}

public void setNAME(String nAME) {
NAME = nAME;
}

}
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Firstly , sorry for the delay in replying , I had gotten busy in some assignment.

Secondly
,,love you...

No need for this hehehe

Now to the problem at hand.
i got some errors sir,


Can you please share what errors are you facing ? And kindly use code tags whenever you are sharing code snippets. Its very difficult to read code in text format.
Rajesha Acharya
Greenhorn

Joined: Mar 18, 2013
Posts: 23
i solved it sir,,,my friend helped me anyway, thanks in advance for future helps... and sorry if my "love you" in case hurt u... ;) but after all i irritated you and still you were answering me so i felt like you are really caring me, so i said love you...
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

No problem Glad you solved you problem
 
 
subject: how to upload file and store the path in database using struts2