Reading in a file from google cloud storage using

2019-07-10 11:02发布

I am new to google cloud storage api, as well as using servers. I'm trying to write a web application in Java using Eclipse's IDE to read in a file that is stored in google's cloud storage. I have the code to read in the file on the server side, and am not sure how to modify the sample code on the client side so that it supports an httpServlet instead of a RemoteServiceServlet. Any help or suggestions would be greatly appreciated!

Below is my code on the server side.

package com.google.gwt.sample.interfacecloud.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.Channels;
import java.util.ArrayList;

import javax.servlet.http.*;
import com.google.gwt.sample.interfacecloud.client.GreetingService;

import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileReadChannel;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.files.GSFileOptions.GSFileOptionsBuilder;

@SuppressWarnings("serial")
public class CloudInteraction extends HttpServlet implements GreetingService{
public static final String BUCKETNAME = "obd_data";
public static final String FILENAME = "data.txt";

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
    resp.setContentType("text/plain");

    String filename = "/gs/" + BUCKETNAME + "/" + FILENAME;
    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile readableFile = new AppEngineFile(filename);
    FileReadChannel readChannel =
            fileService.openReadChannel(readableFile, false);
    BufferedReader reader = 
            new BufferedReader(Channels.newReader(readChannel, "UTF8"));
    String line = reader.readLine();
    resp.getWriter().println("READ:"+line);
    System.out.println(line);
    readChannel.close();
}

@Override
public String greetServer(String name) throws IllegalArgumentException {
    // TODO Auto-generated method stub
    return null;
}

}

1条回答
手持菜刀,她持情操
2楼-- · 2019-07-10 11:22

You are mixing and matching RPC with plain Servlets. You should not be doing that. Do away with RPC interfaces for such interactions if you intend to you plain Servlets. You would be better served with RequestBuilder in this scenario. Note - it is not very clear what your are requirements are?

查看更多
登录 后发表回答