Suhail Gupta
Suhail Gupta

Reputation: 23276

Error 500 while trying to upload file using apache commons . Logs say java.lang.NoClassDefFoundError

While trying to upload a file into to my project folder images using apache commons following exception was generated :

I am copying these logs from the log viewer of google appengine.

2012-08-04 17:52:38.238 /UploadImagesToAisle 500 4381ms 0kb Mozilla/5.0 (X11; 
Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0

106.200.242.109 - - [04/Aug/2012:05:22:38 -0700] "POST /UploadImagesToAisle 
HTTP/1.1" 500 0 "http://programworks.appspot.com/ValidateCredentials" 
"Mozilla/5.0 (X11; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0" 
"programworks.appspot.com" ms=4382 cpu_ms=1773 api_cpu_ms=0 cpm_usd=0.066922 
loading_request=1 instance=00c61b117ce6b4d0249c878697d13e1a0a649c

W 2012-08-04 17:52:38.178

Error for /UploadImagesToAisle
java.lang.NoClassDefFoundError: java.rmi.server.UID is a restricted class. Please see the Google App Engine developer's guide for more details.
at com.google.apphosting.runtime.security.shared.stub.java.rmi.server.UID.<clinit>(UID.java)
at org.apache.commons.fileupload.disk.DiskFileItem.<clinit>(DiskFileItem.java:109)
at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:199)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:361)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at Servlets.UploadImagesToAisle.doPost(UploadImagesToAisle.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)

........

I am trying to upload a file while using google appengine as the server and I am copying the file uploaded into my project directory. i.e inside a directory under web-pages.

Here is the servlet that I used to upload file :

   @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String path = request.getParameter("Data");
    PrintWriter writer = response.getWriter();
     try {
       boolean isMultipart = ServletFileUpload.isMultipartContent(request);
       if( !isMultipart ) {
           writer.println("File cannot be uploaded !");
       } else {
           DiskFileItemFactory diskFileItem = new DiskFileItemFactory();
           ServletFileUpload fileUpload = new ServletFileUpload(diskFileItem);
           List list = null;

           try {
               list = fileUpload.parseRequest(request);
           }catch(Exception exc) {
               writer.println(exc);
           }

           Iterator iterator = list.iterator();
           while(iterator.hasNext()) {
              FileItem fileItem = (FileItem)iterator.next();
              if(fileItem.isFormField()) {
                  // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
              } else {
                  // Process form file field (input type="file").
                  String fieldName = fileItem.getFieldName();
                  String fileName = FilenameUtils.getName(fileItem.getName());
                  File file = new File("images/",fileName);
                  fileItem.write(file);
              }
           }
       }
    }catch(Exception exc) {
        writer.println(exc);
    }
}

Html snippet

            <form method="post" enctype="multipart/form-data" action="../../UploadImagesToAisle">
            <input type="file" name="Data"/>
            <input type="submit" value="upload"/>
        </form>

Images directory inside the project where I want to upload the file :

enter image description here

Upvotes: 1

Views: 1248

Answers (1)

vivek_jonam
vivek_jonam

Reputation: 3297

You cannot write to the file system in Google App Engine. All the applications are sand-boxed. They can only read the files that are uploaded with the project and cannot write to the filesystem. This is the reason you are getting this error.

To overcome this you can make use of Google App Engine Virtual File System. Its a library that is implemented using GAE datastore and memcache APIs. This might help.

EDIT:
Add the library file downloaded at http://code.google.com/p/gaevfs/downloads/detail?name=gaevfs-0.3.zip&can=2&q= to your web server.
Try this and shoot comments:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String path = request.getParameter("Data");
    PrintWriter writer = response.getWriter();
     try {
       boolean isMultipart = ServletFileUpload.isMultipartContent(request);
       if( !isMultipart ) {
           writer.println("File cannot be uploaded !");
       } else {

           ServletFileUpload fileUpload = new ServletFileUpload();
           List list = null;

           try {
               list = fileUpload.parseRequest(request);
           }catch(Exception exc) {
               writer.println(exc);
           }

           Iterator iterator = list.iterator();
           while(iterator.hasNext()) {
              FileItem fileItem = (FileItem)iterator.next();
              if(fileItem.isFormField()) {
                  // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
              } else {
                  // Process form file field (input type="file").
                  String fieldName = fileItem.getFieldName();
                  String fileName = FilenameUtils.getName(fileItem.getName());
                  GaeVFS.setRootPath( getServletContext.getRealPath( "/" ) );
                  FileSystemManager fsManager = GaeVFS.getManager();
                    FileObject file = fsManager.resolveFile( "gae://images/"+filename );
                  fileItem.write(file);
              }
           }
       }
    }catch(Exception exc) {
        writer.println(exc);
    }
}

Add this at import:

import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.FileObject;
import com.newatlanta.commons.vfs.provider.gae.GaeVFS;

Upvotes: 2

Related Questions