Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: problem with uploading an image to server

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Philippines
    Posts
    4
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post problem with uploading an image to server

    Hi everyone

    what seems to be the problem in my code, what i want to do is to upload an image to a folder named "uploadFiles" in my web application.

    here's my code, can you guys please help me. What seems to be the problem?


     boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
            try {
     
                if (!isMultiPart) {
                    out.println("File not found!");
                } else {
     
                    ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
                    List fileItemsList = servletFileUpload.parseRequest(request);
                    Iterator it = fileItemsList.iterator();
     
                    while (it.hasNext()) {
     
                        FileItem fileItem = (FileItem) it.next();
                        if (fileItem.isFormField()) {
                            /* The file item contains a simple name-value pair of a form field */
                        } else {
     
                            /* The file item contains an uploaded file */
                            long fileSize = fileItem.getSize();
                            String fileDirectory = fileItem.getName();
                            String contentType = fileItem.getContentType();
                            String[] temp = fileDirectory.split("\\\\"); /*Just to get the file name only*/
                            String fileName = temp[temp.length-1];
                            out.println(fileName);
                            File saveTo = new File("/uploadFiles/" + fileName ); /*at this line is my problem. It won't Upload the browsed Image*/
     
                            try {
                                fileItem.write(saveTo);
                            } catch (Exception ex) {
                                Logger.getLogger(doInputServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem with uploading an image to server

    What seems to be the problem?
    It helps if you tell us...does it compile? Are there exceptions? Does it misbehave?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Location
    Philippines
    Posts
    4
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with uploading an image to server

    Quote Originally Posted by copeg View Post
    It helps if you tell us...does it compile? Are there exceptions? Does it misbehave?
    yes it compile and there are no exceptions occur. I tried to print something before the fileItem.write(saveTo) for debugging purposes and surprisingly yes it prints. but when i print another after the said code it doesn't print... Does it mean there's a logical error in my Code?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem with uploading an image to server

    Quote Originally Posted by jzarapsa View Post
    yes it compile and there are no exceptions occur. I tried to print something before the fileItem.write(saveTo) for debugging purposes and surprisingly yes it prints. but when i print another after the said code it doesn't print... Does it mean there's a logical error in my Code?
    Are you positive there is no exception? The behavior you describe sounds like an exception is thrown, in which case the debugging line after the write method call is not called. When you say you 'print', do you mean you do so to the Logger? If not and the logs are not inspected, you might miss an exception as the prints might be sent to different I/O streams, depending upon the configuration

Similar Threads

  1. Method :sending image java server to php client
    By ashi123 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 22nd, 2011, 10:15 AM
  2. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  3. Uploading files
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 10:01 PM
  4. GUI problem with image in java
    By Koâk in forum AWT / Java Swing
    Replies: 6
    Last Post: May 17th, 2009, 04:17 AM
  5. Uploaded file getting saved to Bin directory
    By jazz2k8 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 13th, 2008, 01:59 PM