-1

要将文件上传到我在 google appengine 上的项目目录中,我正在尝试使用apache 流 APIGoogle App Engine Virtual File System。这是我到目前为止能够做的事情:

    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 upload = new ServletFileUpload();
           FileItemIterator iter = upload.getItemIterator(request);
           List list = null;
           while(iter.hasNext()) {
               FileItemStream item = iter.next();
               String name  = item.getFieldName();
               String fileName = item.getName();
               InputStream stream = item.openStream();
               if(item.isFormField()) {
                   // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
               } else {
                   GaeVFS.setRootPath( getServletContext().getRealPath("/") );
                   FileSystemManager fsManager = GaeVFS.getManager();
                   //....NOW WHAT....

               }
           }
       }

我被困在 else 块中。现在如何进行?我必须将文件写入项目中名为uploads的目录。

4

1 回答 1

2

GAE 文件系统是只读的。没有通过 API 进行的写访问。更改文件系统内容的唯一方法是通过appcfg.

如果您需要上传数据并存储它,请使用BlobstoreGoogle Cloud Storage

Upload to blobstore:

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 upload = new ServletFileUpload();
       FileItemIterator iter = upload.getItemIterator(request);
       List list = null;
       while(iter.hasNext()) {
           FileItemStream item = iter.next();
           String name  = item.getFieldName();
           String fileName = item.getName();
           InputStream stream = item.openStream();
           if(item.isFormField()) {
               // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
           } else {

            // Get a file service
            FileService fileService = FileServiceFactory.getFileService();

            // Create a new Blob file with mime-type "text/plain"
            AppEngineFile file = fileService.createNewBlobFile(mimeType, filename);

            // Open a channel to write to it
            boolean lock = true;
            FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);

            // copy byte stream from request to channel
            byte[] buffer = new byte[10000];
            int len;
            while ((len = stream.read(buffer)) > 0) {
                writeChannel.write(ByteBuffer.wrap(buffer, 0, len));
            }

            writeChannel.closeFinally();

            // here your data is saved to blobstore
            // you should now save a blobstore key somewhere (=to a datastore)
            // so that you can find it next time
            String blobKey = fileService.getBlobKey(file).getKeyString();

           }
       }
   }

Serving a blob:

public class BlobServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String blobParameter = request.getParameter("blob-key");
    if (blobParameter == null) {
        response.sendError(404, "Missing 'blob-key' parameter.");
    }

    BlobKey blobKey = new BlobKey(blobParameter);

    response.setHeader("Cache-Control", "max-age=" + (15 * 60));   // 15 min
    BlobstoreServiceFactory.getBlobstoreService().serve(blobKey, response);
}
}

Registering your servlet:

<servlet>
    <servlet-name>BlobServlet</servlet-name>
    <servlet-class>com.yourpackage.BlobServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>BlobServlet</servlet-name>
    <url-pattern>/blobservet</url-pattern>
</servlet-mapping> 

Referencing blob in your JSP:

<a src="/blobserve?<%=blobKey%>">Link to blob</a>
于 2012-08-10T16:57:19.453 回答