Results 1 to 3 of 3

Thread: JSP: Upload Images with Custom filename

  1. #1
    Join Date
    Feb 2003
    Location
    Kluang, Johor
    Posts
    2,367
    Rep Power
    249

    Question JSP: Upload Images with Custom filename

    Dear webmasters,

    Need some help here.
    Do you have any idea about upload file/image using JSP and store the uploaded file with custom filename that we defined?

    Any clue/tips/website/code pls?
    Thank you in advance.

    *I have search on Internet and dun get much helpful pages... :/

  2. #2
    Join Date
    Feb 2004
    Location
    Shah Alam
    Posts
    175
    Rep Power
    193
    Like ASP, JSP also need a third party library to do this. Do take a look at this

    http://www.jguru.com/faq/view.jsp?EID=160

  3. #3
    Join Date
    Aug 2004
    Location
    Ipoh, Perak, Malaysia.
    Posts
    2
    Rep Power
    0

    Jakarta Commons FileUpload.

    Hi lcf,

    I strongly suggests the Jakarta Commons FileUpload.

    http://jakarta.apache.org/commons/fileupload/

    The input form has to be "multipart/form-data" as follows:
    Code:
    <form action="/products/addCategory.jsp?action=submit&processFlowToken=<c:out value="${requestScope.processFlowToken}" />" method="post" enctype="multipart/form-data" name="frm_Product">
    myUploadForm.jsp


    Put the .jar into your library. Below are some of the key codes you'll be using in your java class.

    Code:
    import org.apache.commons.fileupload.*;
    
       ...
       ...
    
       DiskFileUpload upload = new DiskFileUpload();
    
       if ( upload.isMultipartContent(req) )  {
          List items = upload.parseRequest(req);
                            
          Iterator iter = items.iterator();
          while (iter.hasNext()) {
             FileItem item = (FileItem) iter.next();
    
             if ( item.isFormField() ) {
    
                // process normal input form fields		
                System.out.println("Field: " + item.getFieldName() );
    	    System.out.println("Value: " + item.getString() );
    
             } else  {
    
      	    // process uploaded fields.
                File fullFile = new File( item.getName() );
    
                // finding the physical directory the image is going to be stored at 
                String dirPath = getServletContext().getRealPath("/products/images/");
    
                // just my own little modification to the uploaded image filename.
                String fileName = trimImgFileName(fullFile.getName(), item.getFieldName(), nextId);
    
                // writing the image file
                item.write( new File( dirPath, fileName ) );
             }
          }
       }
    
       ...
       ...
    
        private String trimImgFileName(String fileName, String fieldName, String nextId) {
            StringBuffer sb = new StringBuffer(fileName);
            sb.replace(0, sb.lastIndexOf("."), nextId + "_" + fieldName);
            return sb.toString();
        }
    UploadProcessing.java

    I'm just extracting a few key areas I suggest you'd look at. I hope I'm not confusing u.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Cannot upload my custom avatar
    By MENJ in forum FeedBack & Suggestion
    Replies: 2
    Last Post: 24-10-2009, 06:42 PM
  2. Suggest to add a Photography forum and the ability to upload images.
    By seraphangel in forum FeedBack & Suggestion
    Replies: 10
    Last Post: 06-09-2007, 12:21 AM
  3. How to explain "~pathname\filename.txt"?
    By genzy in forum Webmaster Tools
    Replies: 2
    Last Post: 28-04-2007, 08:50 AM
  4. Color images in wap?
    By soggie in forum Website Design
    Replies: 7
    Last Post: 09-11-2005, 06:45 PM
  5. Replies: 8
    Last Post: 02-08-2005, 12:42 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
  •  

Search Engine Optimization by vBSEO 3.5.0 RC1 PL1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112