How to upload a file into database by using Servlet?
Posted
by
user1765496
on Stack Overflow
See other posts from Stack Overflow
or by user1765496
Published on 2012-10-23T06:13:03Z
Indexed on
2012/10/23
17:01 UTC
Read the original article
Hit count: 185
Hi all iam working on servlets, so i need to upload a file by using servlet as follows my code.
package com.limrasoft.image.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.sql.*;
@WebServlet(name="serv1",value="/s1")
public class Account extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connecection con=null;
try{
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","sajid");
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
String s1=req.getParameter("un");
string s2=req.getParameter("pwd");
String s3=req.getParameter("g");
String s4=req.getParameter("uf");
PreparedStatement ps=con.prepareStatement("insert into account(?,?,?,?)");
ps.setString(1,s1);
ps.setString(2,s2);
ps.setString(3,s3);
File file=new File("+s4+");
FileInputStream fis=new FileInputStream(fis);
int len=(int)file.length();
ps.setBinaryStream(4,fis,len);
int c=ps.executeUpdate();
if(c==0){pw.println("<h1>Registratin fail");}
else{pw.println("<h1>Registration fail");}
}
finally{if(con!=null)con.close();}
}
catch(ClassNotFoundException ce){pw.println("<h1>Registration Fail");}
catch(SQLException se){pw.println("<h1>Registration Fail");}
pw.flush();
pw.close();
}
}
I have written the above code for file upload into database, but it giving error as "HTTP Status 500 - Servlet3.java (The system cannot find the file specified)"
Could you plz help me to do this code,thanks in advanse.
© Stack Overflow or respective owner