Hi,

My database is showing null value when I try to insert the file path I upload.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        String name=request.getParameter("fileUpload");
        String destPath = "";
        String relativeDestPath=getServletContext().getRealPath(destPath);
        System.out.println("Relativepath is"+relativeDestPath);
        PrintWriter out = response.getWriter();
        String fileName=name.substring(name.lastIndexOf("\\"));
        String pathName=name.substring(0,name.lastIndexOf("\\"));
        String destName = relativeDestPath+fileName;
        System.out.println(fileName);
        System.out.println(pathName);
        System.out.println(name);
        System.out.println("Dest Name is "+destName);
        UploadBean uBean = new UploadBean();
        uBean.setFileName(destName);
        boolean result = UploadProcess.uploadImage(uBean);
        if(result)
        {
            FileInputStream fis = new FileInputStream(name);
            FileOutputStream fos=new FileOutputStream(destName);
            try
            {
                byte[] buf = new byte[1024];

                int len;

                while((len = fis.read(buf)) > 0){

                    fos.write(buf, 0, len);

                }
                System.out.println("Transfer successful");
            }
            catch (Exception e) {
                // TODO: handle exception
                System.out.println("In Exception");
            }
            //RequestDispatcher view= 
        }


        }
}

Thanks for your help

Newcoder

Recommended Answers

All 2 Replies

What's the full text of the error message? Add a call to the printStackTrace() method to the catch block.

Hi,

I'm not getting any error. Its just that at line 15 when i print the destName its showing it correctly but once the UploadBean object is created and I try to set the destName, a null value is being set in the database. Below is the rest of the code

UploadBean class

package ImageUploader;

public class UploadBean {
String fileName;

public UploadBean() {
    this.fileName = fileName;
}
public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}


}

UploadProcess Class

package ImageUploader;

public class UploadProcess {

    public static boolean uploadImage(UploadBean b){
        boolean result= false;
        int returnValue;
        returnValue = UploadDAO.uploadImage(b);
        if(returnValue==1)
        {
                System.out.println("value inserted");
                result = true;
        }
        else
        {
            System.out.println("value not inserted");
            result=false;
        }
        return result;
    }

}

UploadDAO class

package ImageUploader;
import java.sql.Connection;
import java.sql.Statement;

import Authentication.DBConn;

public class UploadDAO {
 static Connection con = null;
    public static int uploadImage(UploadBean b){
        con = DBConn.getCon();
        if(con!=null)
         {
         System.out.println("After conncetion");
         }
         else
         {
             System.out.println("con is null");
         }
        UploadBean imageName = new UploadBean(); 
        Statement stmt=null;
        try
        {
         stmt=con.createStatement();    
        }
        catch (Exception e) {
            System.out.println("Ex");
             e.printStackTrace();   
        }
        System.out.println("File Name is"+imageName.getFileName());
        String query ="insert into imagestore value('"+imageName.getFileName()+ "')";
        int returnValue=0;
        try
        {
             returnValue = stmt.executeUpdate(query);
            System.out.println("return value is"+returnValue);          
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return returnValue;
    }
}

Thanks
NewCoder

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.