0

html code:

                <td>Select File : </td>
            <td><input  name="file" type="file"/> </td>
        </tr>
        <tr>
            <td>Enter Filename : </td>
            <td><input type="text" name="photoname" size="20"/> </td>
        </tr>

servlet code:

Part p1 =  request.getPart("file"); //1
InputStream is = p1.getInputStream();

Part p2  = request.getPart("photoname");//2
Scanner s = new Scanner(p2.getInputStream());
String filename = s.nextLine();   

but at following points I am getting errors: 1.Part p1 = request.getPart("file"); 2.Part p2 = request.getPart("photoname");

EJ Dogar
  • 68
  • 10

1 Answers1

0

In HttpServletRequest, getPart was not implemented in Java EE 5. You need at least Java EE 6. You probably need to upgrade your servlet container.

Compare http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletRequest.html with http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

(For example, if you have Tomcat 5 installed, move up to Tomcat 7 or 8.)

Or just use Apache Commons File Upload. To see how to do that, refer to How to upload files to server using JSP/Servlet?

Community
  • 1
  • 1
developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • I'm talking about the server. Ie. Tomcat, Jetty, etc. The version of your servlet container determines the version of Java EE you have installed. And yes, even if you are using Spring, EE is still installed. The request object wouldn't work otherwise. – developerwjk Dec 04 '13 at 23:45
  • obviously it it. What i am looking for is that is there any alternate way to do it? means if we can replace Path p1 = getPath() with something else. – EJ Dogar Dec 04 '13 at 23:49
  • and at the moment is am using tomcat7 – EJ Dogar Dec 04 '13 at 23:49
  • To deal with multipart/form-data you can use the Apache commons file upload library. Spring also has its own way that builds on top of Apache commons file upload. – developerwjk Dec 04 '13 at 23:51
  • I have been trying that too but there is problem in that code as well. This error pops up "The type List is not generic; it cannot be parameterized with arguments " for List fields = upload.parseRequest(request); – EJ Dogar Dec 05 '13 at 00:00
  • That's odd but you can leave it off. Just say List fields = upload.parseRequest(request); Howvever, then instead of using = itor.next() when pulling from the iterator, you will need to typecast, so = ((FileItem)itor.next()); – developerwjk Dec 05 '13 at 00:13
  • Like its weird for List fields = upload.parseRequest(request); its giving a type mismatch error, which says "Type mismatch: cannot convert from List to List". – EJ Dogar Dec 05 '13 at 00:17
  • There's something wrong with your Java and or Tomcat installation. – developerwjk Dec 05 '13 at 00:19
  • Can I send you my code somewhere just go through it, it will be better. What you say? – EJ Dogar Dec 05 '13 at 00:27