-1

I need to process a file upload using a servlet as follows:

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

But it results in an error page:

HTTP Status 500 - Servlet3.java (The system cannot find the file specified)

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1765496
  • 1
  • 1
  • 2
  • 3
    You should add some more detail to your question - just a blob of code on its own is pretty much unanswerable. – Frederick Cheung Oct 23 '12 at 06:31
  • Also, the status code 505 and the error message "file not found" in your title are contradictory. The status code 505 stands for the error message "HTTP version not supported" (which is a very, very rare error btw) and the error message "file not found" is actually associated with status code 404. See also http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html – BalusC Oct 23 '12 at 13:57
  • this is my error in browser HTTP Status 500 - Servlet3.java (The system cannot find the file specified) – user1765496 Oct 23 '12 at 16:35

1 Answers1

0

You seem to be nowhere extracting the uploaded file content from the HTTP request. You're only collecting the file name and constructing a new File() around it on a path relative to server's current working directory. I'm not sure what you were thinking while writing this code, but just passing file names around would never work when webbrowser runs at a physically different machine than the webserver, because they would not share the same local disk file system.

Instead, you should be passing the entire file content in the request. You can achieve this by using multipart/form-data encoding on the HTML form.

<form action="s1" method="post" enctype="multipart/form-data">

Now, annotate the servlet with @MultipartConfig to enable multipart/form-data support.

@WebServlet("/s1")
@MultipartConfig
public class Account extends HttpServlet {

Then, to extract the uploaded file, just use HttpServletRequest#getPart().

Part s4 = req.getPart("uf");

The file content is as an InputStream available by Part#getInputStream(). So, all with all, the following code

String s4=req.getParameter("uf");
// ...
File file=new File("+s4+");
FileInputStream fis=new FileInputStream(fis);
int len=(int)file.length();
ps.setBinaryStream(4,fis,len);

should be replaced by

Part s4 = req.getPart("uf");
// ...
ps.setBinaryStream(4, s4.getInputStream());

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555