2

I am working on code where i am not able to upload file by browsing as after debugging , everything seems ok but still getting null vaule

here is my code :

<form action="upload" method="post" enctype="multipart/form-data">
        <hr>
        <input type="file" name="fileToUpload">
        <hr>
        <input type="submit" value="go">
        <hr>
    </form>

and java code :

resp.setContentType("text/html");
    System.out.println("insdie Servlet to db");
    InputStream stream;
    Part file = req.getPart("fileToUpload");
    if (file != null) {
        System.out.println(file.getName());
        System.out.println(file.getSize());
        System.out.println(file.getContentType());
        stream = file.getInputStream();
    } else
        System.out.println("file not loaded");

web.xml

<servlet>
      <servlet-name>toDataBase</servlet-name>
      <servlet-class>com.fromHtmlToServlet.ServletToDb</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>toDataBase</servlet-name>
      <url-pattern>/upload</url-pattern>
  </servlet-mapping>

Edit: The Output :

INFO: Server startup in 506 ms
insdie Servlet to db
file not loaded

I do have servlet.jar in deployment descriptor and code is going towards else part in java file . So any one can tell me what is the cause of problem? Thaks

bananas
  • 1,176
  • 2
  • 19
  • 39
  • 1
    *"I do have servlet.jar in deployment descriptor "* this statement is ambiguous. You can't have JAR files in DD (the DD is the `web.xml` file itself). Do you mean that you have a `servlet.jar` in `/WEB-INF/lib`? This is in first place absolutely not correct. Get rid of it immediately. If you did this in an attempt to solve compilation errors, please undo everything you did there and redo based on http://stackoverflow.com/q/4076601. Then retest your current problem. – BalusC May 09 '16 at 09:47
  • i removed servlet jar and still getting error. **Part file=req.getPart("parameter");"" – bananas May 09 '16 at 10:12
  • *providing null tried every possible code but not working is there any way to store files directly to db, instead of moving to third party computer?? – bananas May 09 '16 at 10:15
  • 1
    Did you make sure that your code matches http://stackoverflow.com/q/2422468? – BalusC May 09 '16 at 10:15
  • i think something else is wrong , as all of three are not working – bananas May 09 '16 at 10:34
  • 1
    Which parts exactly of the link didn't you clearly understand? – BalusC May 09 '16 at 10:38
  • Thanks @BalusC i was missing annotation **@MultipartConfig** – bananas May 09 '16 at 11:16

1 Answers1

0

Actually I was missing a simple annotation and here is the answer

@MultipartConfig  <--- was missing 
public class UploadData extends HttpServlet {
// do your job
}
bananas
  • 1,176
  • 2
  • 19
  • 39