0

database snapshotfile diretory snapshotI am using eclipse galelio and apache tomcat server 5.5

jsp page code:

 <form action="Add" method=post enctype="multipart/form-data">
        <p>Book name:  <input type="text" name="bname" required/></p>
        <p>Price:<input type="text" name="bprice" required/></p>
        <p>Quantity:<input type="text" name="bqty" required/></p>
        <p>Image: <input type="file" name="file" required/></p>
        <p>Course: <select name="course">
        <option>course 1</option>
        <option>course 2</option> <!-- Some more options-->
        <input type="submit" value="Add" name="Submit"/></p>
    </form>

servlet code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");
    PrintWriter out=response.getWriter();

    String path="images/default";
    MultipartRequest m=new MultipartRequest(request,path);
    String cy="";// i am assigng the value to cy based on some if else conditions on the value of course field
    String bname=m.getParameter("bname");
    String bprice=m.getParameter("bprice");
    String bqty=m.getParameter("bqty");
    String course=m.getParameter("course");
    String file=m.getFilesystemName("file");
try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pro","root","root");


        PreparedStatement ps=con.prepareStatement("select * from  product where bname=? and course=?");
        ps.setString(1,bname);  
        ps.setString(2,course); 
        ResultSet rs=ps.executeQuery();

        if(rs.next())
        {
            out.print("<p style='position:absolute;top:130px;left:360px;color:#CC0066'>Product already exists! You might want to <b>update</b>.</p>");
        }
        else
        {
            ps=con.prepareStatement("insert into product(bname,cy,course,price,qty,path,type) values (?,?,?,?,?,?,'n')");
            ps.setString(1,bname);  
            ps.setString(2,cy); 
            ps.setString(3,course);
            ps.setString(4,bprice); 
            ps.setString(5,bqty);   
            ps.setString(6,path+"/"+file);

            ps.executeUpdate();
            out.print("<p style='position:absolute;top:160px;left:550px;color:#CC0066'>Product added!</p>");

        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    RequestDispatcher rd=request.getRequestDispatcher("admin.jsp");
    rd.include(request, response);

}

I am getting this error:

java.lang.IllegalArgumentException: Not a directory: images/default

I definately have the directory in my webcontent folder of the project in eclipse. It should work fine but isn't. Where's the problem?? I have imported: import com.oreilly.servlet.MultipartRequest; and have cos.jar file in my lib see the image attached.

user63762453
  • 1,734
  • 2
  • 22
  • 44

1 Answers1

0

As you can see in the src code from the class the path must existis and must be a directory. So i geuss the directory images/default does not exist`s.

  /**
   * Constructs a new MultipartRequest to handle the specified request,
   * saving any uploaded files to the given directory, and limiting the
   * upload size to the specified length.  If the content is too large, an
   * IOException is thrown.  This constructor actually parses the
   * <tt>multipart/form-data</tt> and throws an IOException if there's any
   * problem reading or parsing the request.
   * <p/>
   * To avoid file collisions, this constructor takes an implementation of the
   * FileRenamePolicy interface to allow a pluggable rename policy.
   *
   * @param request       the servlet request.
   * @param saveDirectory the directory in which to save any uploaded files.
   * @param maxPostSize   the maximum size of the POST content.
   * @param encoding      the encoding of the response, such as ISO-8859-1
   * @param policy        a pluggable file rename policy
   * @throws IOException if the uploaded content is larger than
   *                     <tt>maxPostSize</tt> or there's a problem reading or parsing the request.
   */
  public MultipartRequest(HttpServletRequest request,
                          String saveDirectory,
                          int maxPostSize,
                          String encoding,
                          FileRenamePolicy policy) throws IOException {
    // Sanity check values
    if (request == null)
      throw new IllegalArgumentException("request cannot be null");
    if (saveDirectory == null)
      throw new IllegalArgumentException("saveDirectory cannot be null");
    if (maxPostSize <= 0) {
      throw new IllegalArgumentException("maxPostSize must be positive");
    }

    // Save the dir
    File dir = new File(saveDirectory);

    // Check saveDirectory is truly a directory
    if (!dir.isDirectory())
      throw new IllegalArgumentException("Not a directory: " + saveDirectory);
Jens
  • 67,715
  • 15
  • 98
  • 113
  • please see the image attached. i have the directory. – user63762453 Jul 09 '14 at 12:58
  • @Nivedita I don`t think that webContent is the working directory. – Jens Jul 09 '14 at 13:03
  • I have all the .jsp files in web content and servlet files in src – user63762453 Jul 09 '14 at 13:04
  • It was working fine before i renamed the folder to uploaded and then again to default (If that makes a difference) – user63762453 Jul 09 '14 at 13:07
  • @Nivedita Add `File f = new File("images/default"); System.out.println(f.getAbsolutePath());` and look where it points to. – Jens Jul 09 '14 at 13:10
  • got this: C:\Users\lenovi\Documents\Nivedita\Softwares\eclipse cmc\eclipse\images\default – user63762453 Jul 09 '14 at 13:18
  • I don't understand when i use the relative path from images/somthng.jpg in it works. but why is it showing this error here. – user63762453 Jul 09 '14 at 13:20
  • @Nivedita the img tag is interpreted by the client(Browser). The other code is executed on the server! – Jens Jul 09 '14 at 13:22
  • i want the image here: D:\AdvJava\proimp\WebContent\images\default – user63762453 Jul 09 '14 at 13:23
  • that code would only get me the path. and not store the image to the desired location – user63762453 Jul 09 '14 at 13:31
  • Look [jere](http://stackoverflow.com/questions/18664579/recommended-way-to-save-files-uploaded-to-a-tomcat-servlet) maybe this helps. – Jens Jul 09 '14 at 13:33
  • i changed the path to: String path="D:/uploads"; the upload now works. but when i fetch the path from database and try to display the image using same path in src: nothing is being displayed – user63762453 Jul 09 '14 at 13:38
  • Yes of course. You can not access a file outside you document-root directory. And D:/uploads is definitively outside. You have to use a directory like `getServletContext().getRealPath("/images/default");` – Jens Jul 09 '14 at 13:40
  • where do i need to make changes so that the code works the way i want – user63762453 Jul 09 '14 at 13:42
  • replace `String path="images/default";` with `String path=getServletContext().getRealPath("/images/default");` – Jens Jul 09 '14 at 13:44
  • got this path in DB : D:\AdvJava\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\proimp\web content\images\default\download (7).jpg no image is displayed – user63762453 Jul 09 '14 at 13:52
  • Sorry I have no time to write your code. One Hint: You have to use `String path=getServletContext().getRealPath("/images/default");` to save the file in the db store only `/images/default` and if you read the information you have ony the short path in your browser. – Jens Jul 09 '14 at 14:02