Results 1 to 5 of 5
  1. #1
    tommy is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default how to upload a file?

    Do you have sample code to upload a file with JSP?
    thanks

  2. #2
    mary is offline Member
    Join Date
    Jul 2007
    Posts
    51
    Rep Power
    0

    Default

    you can try this: Home - FileUpload

  3. #3
    indula is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default

    Do you have sample code to upload a file with JSP?

  4. #4
    satyadas is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default

    Image upload from JSP to Data Base step by step :

    step 1 :
    In the JSP
    <form name="regform2" method="post" enctype="multipart/form-data">
    <input type="file" name="ImageFile" id="ImageFile" onChange="uploadImage()"/>
    </form>

    java script :
    function uploadImage(){
    document.regform.action ="<%=request.getContextPath()%>/dfdmin?cmd=uploadimage";
    document.regform.submit();
    }


    Step 2.
    In the servlet - add the below code.
    This will upload your image to server (D:\\) or In unix you can mention /home/user like that

    String rtempfile = File.createTempFile("temp","1").getParent();
    MultipartRequest multi = new MultipartRequest(request, rtempfile, 500000 * 1024);
    File rnewfile=null;
    rnewfile = new File("D:\\"+"jsp"+File.separator+"images"+File.sep arator+"uploadImage"+File.separator);
    if(rnewfile.exists()){
    }else{
    rnewfile.mkdirs();
    }

    File f = multi.getFile("ImageFile");
    System.out.println(f.getName());
    FileInputStream fin =new FileInputStream(f);
    RandomAccessFile r = new RandomAccessFile(rnewfile+File.separator+f.getName (),"rw");
    filename = f.getName();
    // FileOutputStream fos =new FileOutputStream(rnewfile);
    byte sizefile[] = new byte[5000000];
    fin.read(sizefile);
    // fos.write(sizefile);
    r.write(sizefile);
    //fos.close();
    r.close();
    fin.close();

    DAO.upload(f); // Call to DAO for insert image into database.

    goto techfaq360.com/jsp_interview_questions.jsp?qid=348 for details

  5. #5
    o0JSP is offline Member
    Join Date
    Jun 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Simple way to upload file via FTP?
    By Hiro ZA in forum New To Java
    Replies: 0
    Last Post: 03-27-2008, 01:27 PM
  2. Applet File Upload
    By genette in forum Java Applets
    Replies: 2
    Last Post: 03-09-2008, 02:06 PM
  3. File Upload
    By ShoeNinja in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-09-2007, 10:09 PM
  4. how to upload a file using servlet
    By simon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 03:50 AM
  5. MultipartRequest File Upload parser 2.0B9
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-12-2007, 12:24 PM

Posting Permissions

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