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?
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.