1

在尝试使用 apache commons将文件上传到我的项目文件夹图像时,生成了以下异常:

我正在从 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)

........

我正在尝试在使用 google appengine 作为服务器时上传文件,并且正在将上传的文件复制到我的项目目录中。即在web-pages下的目录中。

这是我用来上传文件的servlet:

   @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片段

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

我要上传文件的项目内的图像目录:

在此处输入图像描述

4

1 回答 1

2

您无法写入 Google App Engine 中的文件系统。所有的应用程序都是沙盒的。他们只能读取与项目一起上传的文件,而不能写入文件系统。这就是您收到此错误的原因。

为了克服这个问题,您可以使用Google App Engine 虚拟文件系统。它是一个使用 GAE 数据存储和内存缓存 API 实现的库。这可能会有所帮助。

编辑:将在http://code.google.com/p/gaevfs/downloads/detail?name=gaevfs-0.3.zip&can=2&q=
下载的库文件添加到您的网络服务器。 试试这个并拍摄评论:

@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);
    }
}

在导入时添加:

import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.FileObject;
import com.newatlanta.commons.vfs.provider.gae.GaeVFS;
于 2012-08-04T15:49:05.227 回答