Results 1 to 11 of 11
  1. #1
    i4ani is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Question problem with file upload

    Hi guys,
    I'm having problems when trying to upload a picture from my hard drive to my server folder which is at localhost. This is the error i'm getting:

    java.io.FileNotFoundException: /Library/glassfishv3/glassfishv3/glassfish/domains/domain1/eclipseApps/LaFamily/files (Is a directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.j ava:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.j ava:131)
    at org.apache.commons.fileupload.disk.DiskFileItem.wr ite(DiskFileItem.java:449)
    at client.UploadPicture.doPost(UploadPicture.java:89)

    I'm using commons.fileUpload in Eclipse and i'm runing it on Glassfish on osx

    Here is my Servlet code

    private static final String DESTINATION_DIR_PATH ="/files";
    private File destinationDir;



    public void init(ServletConfig config) throws ServletException {
    super.init(config);

    String realPath = getServletContext().getRealPath(DESTINATION_DIR_PA TH);
    destinationDir = new File(realPath);
    if(!destinationDir.isDirectory()) {
    throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
    }
    }

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

    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
    fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB

    ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
    try {
    List<FileItem> items = uploadHandler.parseRequest(request);
    Iterator<FileItem> itr = items.iterator();

    while(itr.hasNext()) {
    FileItem item = (FileItem) itr.next();

    if(item.isFormField()) {
    out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
    } else {

    item.write(destinationDir);

    }
    out.close();
    }
    }catch(FileUploadException ex) {
    log("Error encountered while parsing the request",ex);
    } catch(Exception ex) {
    log("Error encountered while uploading file",ex);
    }
    request.getRequestDispatcher("/index.jsp");
    }

    My JSP looks like this

    <form action="UploadPicture" method="post" enctype="multipart/form-data">
    <input type="file" name="myFile">
    <input type="submit" value="Upload">
    </form>

    Got any ideas??
    Thanks in advance

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    I'm guessing a bit here, but should the File past to here:
    item.write(destinationDir);

    be the destination directory plus the filename?

    In fact, the answer is yes according to the API.

    So you want to pass in a new File, using the File/String constructor.

  3. #3
    i4ani is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Do you mean like this:

    File file = new File(destinationDir,item.getName());
    item.write(file);

    It's not working either, only that now i get no errors but the file is not being copied :(

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    What's the value of getName()?
    What's the value of getAbsolutePath() of the file?
    Debug it, either with a debugger or simple sysout statements.

  5. #5
    i4ani is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Can't check it right now, i'm at school, but what should be the right value??

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Just print them out and see if they look OK.
    The absolute path should be where you want the file to go (ie the full path to the file).
    The name should, presumably, be the name you want for the file...and not something else entirely.

  7. #7
    i4ani is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    The getName() is the file name and the absolutePath() is the value of destinationDir which is in my Project Root Folder and i named it /files.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    You can say that, but until you print it out or debug it you may be making assumptions, because something is clearly not correct.

    getAbsoultePath() for the new file should be destinationDir plus the filename, for example.

    No point assuming this is the case...sysout or debug.

  9. #9
    i4ani is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    So i printed it and this is what i got:
    INFO: name----->IMG_0335.JPG
    INFO: path----->/Library/glassfishv3/glassfishv3/glassfish/domains/domain1/eclipseApps/LaFamily/files/IMG_0335.JPG


    That's exactly where i want to save the file. But it's not happening. I even update my project in Eclipse and no file shows up ;(

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    So when you go to that directory on your system there is no file?

    Is that a *nix system?

  11. #11
    i4ani is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Smile Solved

    Wow this is kind of embarrassing, the file was being saved in /Library/glassfishv3/glassfishv3/glassfish/domains/domain1/eclipseApps/LaFamily/files/. Not in the folder i have for my saved files. This is kind of logic because my application server only knows its own folders, so what i can do is to write some code to move this saved file to my preferred folder. Anyways thanks Tolls for the help couldn't have done it without it. I'm posting the hole code in case that someone is looking for the same answer:

    My Servlet:

    package client;


    import javax.servlet.annotation.WebServlet;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.util.Iterator;
    import java.util.List;

    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFac tory;
    import org.apache.commons.fileupload.servlet.ServletFileU pload;

    @WebServlet("/UploadPicture")
    public class UploadPicture extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String DESTINATION_DIR_PATH ="/files";
    private File destinationDir;

    public UploadPicture() {
    super();
    }

    public void init(ServletConfig config) throws ServletException {
    super.init(config);

    String realPath = getServletContext().getRealPath(DESTINATION_DIR_PA TH);
    destinationDir = new File(realPath);
    if(!destinationDir.isDirectory()) {
    throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
    }
    }

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

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

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


    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
    fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB

    ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
    try {
    List<FileItem> items = uploadHandler.parseRequest(request);
    Iterator<FileItem> itr = items.iterator();
    while(itr.hasNext()) {
    FileItem item = (FileItem) itr.next();

    if(item.isFormField()) {
    out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
    } else {

    out.println("Field Name = "+item.getFieldName()+
    ", File Name = "+item.getName()+
    ", Content type = "+item.getContentType()+
    ", File Size = "+item.getSize());

    File file = new File(destinationDir,item.getName());
    item.write(file);
    }
    out.close();
    }
    }catch(FileUploadException ex) {
    log("Error encountered while parsing the request",ex);
    } catch(Exception ex) {
    log("Error encountered while uploading file",ex);
    }
    request.getRequestDispatcher("/index.jsp");
    }

    }

    My JSP

    <form action="UploadPicture" method="post" enctype="multipart/form-data">
    <input type="file" name="myFile">
    <input type="submit" value="Upload">
    </form>

    That's it, working beautifly ;)
    Thanks for the help.

Similar Threads

  1. file upload in jsp
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-20-2010, 08:10 PM
  2. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 12:21 AM
  3. File Upload Servlet problem
    By jeniramires in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 07:34 PM
  4. how to upload a file?
    By tommy in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-30-2008, 02:50 PM
  5. file upload
    By sundarjothi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-27-2008, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •