虚拟文件系统上传文件

虚拟文件系统上传文件

本文介绍了使用 apache 流 API 和 Google App Engine 虚拟文件系统上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要将文件上传到我在 google appengine 上的项目目录中,我正在尝试使用 apache 流 APIGoogle App Engine 虚拟文件系统.这是我到目前为止能够做的:

To upload a file into my project directory which is there on the google appengine,I am trying to use apache streaming API and Google App Engine Virtual File System. Here is what I have been able to do till now :

    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 的目录.

I am stuck there in the else block. How to proceed now ? I have to write the file to a directory named uploads in my project.

推荐答案

GAE 文件系统是只读的.没有通过 API 的无写访问.更改文件系统内容的唯一方法是通过 appcfg 更新应用程序.

GAE filesystem is read-only. There is no write access via an API. The only way to change filesystem contents is to update the app via appcfg.

如果您需要上传和存储数据,请使用 BlobstoreGoogle 云存储.

If you need to upload data and store it use Blobstore or Google Cloud Storage.

上传到 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();

           }
       }
   }

服务 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);
}
}

注册您的 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>

在 JSP 中引用 blob:

<a src="/blobserve?<%=blobKey%>">Link to blob</a>

这篇关于使用 apache 流 API 和 Google App Engine 虚拟文件系统上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:59