Uploading image to server using java servlets

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tranceluv
    New Member
    • Apr 2010
    • 1

    Uploading image to server using java servlets

    Hey guys.

    I am currently working on a project in JSP/Java Servlets, hope this is the right section. So, my problem regards mainly the form through which I will let the user input a file using <input type="file">. I did some research on the internet, telling me that I have two options (on the Servlet side) in order to get the file and store it in a directory on the server, namely O'Reilly's COS MultipartReques t and some other package issued by Apache. Now my problem is that, together with an image upload, I want the user to input other details in the form. I am managing to get the file on the server, however I still cannot get the other details (from textboxes, textareas and drop-down selections) since the form MUST have the enctype="multip art/form-data" property in order for the file upload to work.

    Any ideas regarding what I am doing wrong? Or maybe how should I code this task?

    Here is the source code:


    Code:
    <table align="center" cellspacing="1"><form action="AddTrip" enctype="multipart/form-data" method="POST">
                                        <tbody>
                                            <tr>
                                                <td>Trip Name</td>
                                                <td><input type="text" name="Name" value="" /></td>
                                            </tr>
                                            <tr>
                                                <td>Description</td>
                                                <td><textarea name="Description" rows="3" cols="20" value=""></textarea></td>
                                            </tr>
                                            <tr>
                                                <td>Category</td>
                                                <td><select name="Category" size="4">
                                                        <option>Cruise</option>
                                                        <option>Tour</option>
                                                        <option>Park</option>
                                                        <option>History</option>
                                                        <option>Eco-Tourism</option>
                                                    </select></td>
                                            </tr>
                                            <tr>
                                                <td>Price</td>
                                                <td><input type="text" name="Price" value="" /></td>
                                            </tr>
                                            <tr>
                                                <td>Start Date</td>
                                                <td><input name="StartDate" onfocus="showCalendarControl(this);" type="text"></td>
                                            </tr>
                                            <tr>
                                                <td>End Date</td>
                                                <td><input name="EndDate" onfocus="showCalendarControl(this);" type="text"></td>
                                            </tr>
                                            <tr>
                                                <td>Country</td>
                                                <td><select name="Country" size="4">
                                                        <option>Malta</option>
                                                        <option>Gozo</option>
                                                        <option>Comino</option>
                                                        <option>Sicily</option>
                                                        <option>Ibiza</option>
                                                        <option>Greece</option>
                                                    </select></td>
                                            </tr>
                                            <tr>
                                                <td>Places Available</td>
                                                <td><input name="Places" type="text" value=""/></td>
                                            </tr>
                                            <tr>
                                                <td>Upload Picture</td>
                                                <td><input type="file" name="uploaded" accept="image/gif, image/jpeg, image/png, image.jpg"/></td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" align="center"><input type="submit" id="search-submit-long" name="save" value="Submit Trip"></td>
                                            </tr>
                                        </tbody>
                                        </form></table>
    
    [Servlet]
    
                String Name = request.getParameter("Name");
                String Description = request.getParameter("Description");
                String Category = request.getParameter("Category");
                String Price = request.getParameter("Price");
                String StartDate = request.getParameter("StartDate");
                String EndDate = request.getParameter("EndDate");
                String Country = request.getParameter("Country");
                String Places = request.getParameter("Places");
    
            MultipartRequest multipartRequest = new MultipartRequest(request, getServletContext().getRealPath("/images/"), /* 1MB */ 1024 * 1024, new DefaultFileRenamePolicy());
            if (multipartRequest.getParameter("save") != null) {
                upload(request, response, multipartRequest);
            } else {
                throw new IOException("Display Upload Dialogue");
            }
    
    
            } catch(Exception e) {
                response.sendRedirect("Error.jsp?error=Entry of new trip failed. Please try again! Error is " + e.toString());
                out.close();
            }
  • MusoFreak200
    New Member
    • Oct 2009
    • 96

    #2
    hello newcomer...

    i think that you may get better results if you uploaded this thread into the Java section...

    i for one have no idea how to do what you wanna do but i think someone may know inside the other section...

    try to rename to your thread asking on the front, how to...

    thanks mate...
    cya
    MusoFreak200

    Comment

    • Unregistered

      #3
      i have servlet code for inserting the image and retrieving the image in database(MS-Access)

      Comment

      Working...