Question

I have been trying to upload file into MySQL DB using a blob datatype. This is my JSP page

<body>
    <c:choose>
        <c:when test="${not empty sessionScope.username}">
            <form method="post" enctype="multipart/form-data" action="postResume.do">
                <h4>Please paste your resume below</h4>
                <input name="t1" type="file"/>
                <input type="submit" value="Post"/>
            </form>                
        </c:when>
        <c:otherwise>
            <h1>Please Login First</h1>
        </c:otherwise>
    </c:choose>
</body>

The bean has a getter and setter method

public class CandyResume extends org.apache.struts.action.ActionForm {

    private FormFile t1;

    public FormFile getT1() {
        return t1;
    }
    public void setT1(FormFile t1) {
        this.t1 = t1;
    }
}

The bean actionform has the following code

public class CandyResumeAction extends org.apache.struts.action.Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    String result = null;
    HttpSession session = request.getSession();
    CandyResume val = (CandyResume)form;
    Connection con = DBConnection.DBConnection.justConnect();
    try{            
        FormFile formFile = val.getT1();
        File file = new File(formFile.getFileName());
        FileInputStream fin = new FileInputStream(file);
    //    String filePath = getServlet().getServletContext().getRealPath("") +"/"+formFile.getFileName();
        Integer filesize = formFile.getFileSize();
        PreparedStatement ps = con.prepareStatement("INSERT INTO seek_resumeupdate VALUES(?,?)");
        ps.setInt(1, Integer.parseInt(session.getId()));
        ps.setBinaryStream(2, fin, filesize);
        int insert = ps.executeUpdate();
        if(insert!=0)
            result = "uploaded";
        else 
            result = "failed";
    }catch(SQLException ex){
        ex.printStackTrace();
    }
    return mapping.findForward(result);
 }
}

If I specify the path manually the code works fine but not if I need the user to upload a desired file. It gives me the following error. Here is the stacktrace.

java.io.FileNotFoundException: Aman_Resume (1).doc (The system cannot find the file specified)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(FileInputStream.java:138)
Action.CandyResumeAction.execute(CandyResumeAction.java:36)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

Why am I seeing this error and why does it work if path to the file is specified manually?

Was it helpful?

Solution 2

Here is the solution to the problem so that other people looking at something similar can get a hint. The only changes made were in the Struts Action file.

CandyResumeAction.java

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    String result = null;
    HttpSession session = request.getSession();
    String name = (String)session.getAttribute("username");
    int a = Integer.parseInt(name);
    CandyResume val = (CandyResume)form;
    Connection con = DBConnection.DBConnection.justConnect();
    try{            
        FormFile formFile = val.getFile();
        InputStream fin = formFile.getInputStream();
        PreparedStatement ps = con.prepareStatement("INSERT INTO seek_resumeupdate VALUES(?,?)");
        ps.setInt(1, a);
        ps.setBlob(2, fin);
        int insert = ps.executeUpdate();
        if(insert!=0)
            result = "uploaded";
        else 
            result = "failed";
    }catch(SQLException ex){
        ex.printStackTrace();
    }
    return mapping.findForward(result);
}

The problem happened to be the session attribute that I set and confused it with getId() function.

OTHER TIPS

You really do not want to upload the file to the server. You just want to store some of the file information to the database. But in your action class, you are trying to find out the server path to store the file in the server, which is unnecessary for your requirement.

try {
            CandyResume resumeForm = (CandyResume) form;
            FormFile formFile = resumeForm.getT1();
            Connection con = DBConnection.DBConnection.justConnect();
            /*
             * this is where you are going to upload your file to . You cannot
             * expect your file in this path. note that this is your File
             * Upload Action class
             */
            // getting the server's upload directory real path name

            // If your intention is not to upload on server, then there is no need of the below line.
            // You already have your file which is 'formFile'

            String filePath = getServlet().getServletContext().getRealPath("");

            /*
             * check the value of filePath because the method returns null if the
             * container is unable to translate to a real path , in case you intend
             * to pass a parameter
             */

            // for fileName
            String fileName = formFile.getFileName();

            // do what you want to do with the file which is 'formFile'


        } catch (Exception ex) {
            ex.printStackTrace();
        }

One more thing though irrelevant to your question, please don't name the property as t1 as in private FormFile t1 in CandyResume.java . Name some thing like private FormFile file for meaningful naming

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top