我正在尝试将HashMap上传到GCS。我要做的第一件事是将HashMap保存到内部存储器中:
HashMap<Integer, Integer> picViews = new HashMap<Integer, Integer>();
//Write the local file with total picture views
private void writeViewFile(String filename) {
try {
FileOutputStream f = openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(picViews);
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
使用以下代码,我试图在我的存储区“ isthiscute-rankings”中创建一个名为“ myViewsTest”的对象:
public void uploadViews() throws Exception {
// Authorization.
if (googleCredential == null) {
googleCredential = authorize();
}
// Set up global Storage instance.
client = new Storage.Builder(httpTransport, JSON_FACTORY, googleCredential).setApplicationName(APPLICATION_NAME).build();
String fileName = "myViews";
StorageObject object = new StorageObject();
object.setBucket(BUCKET_NAME_RANKINGS);
//Read stored HashMap as an ObjectInputStream
FileInputStream fis = openFileInput(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
HashMap<Integer, Integer> testHash;
try {
//This correctly reads the HashMap from file
testHash = (HashMap<Integer, Integer>) ois.readObject();
String contentType = URLConnection.guessContentTypeFromStream(ois);
InputStreamContent content = new InputStreamContent(contentType, ois);
//This does not add a HashMap object to my Storage Object
Storage.Objects.Insert insert = client.objects().insert(BUCKET_NAME_RANKINGS, object, content);
insert.setName("myViewsTest");
//This does add a HashMap to my Storage Object --> Fails to upload
insert.set("object", testHash);
insert.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
fis.close();
ois.close();
}
}
最后,我尝试下载该对象。这是我正在尝试的:
public void downloadViews() throws Exception {
// Authorization.
if (googleCredential == null) {
googleCredential = authorize();
}
// Set up global Storage instance.
client = new Storage.Builder(httpTransport, JSON_FACTORY, googleCredential).setApplicationName(APPLICATION_NAME).build();
String fileName = "myViewsTest";
String destinationDirectory = this.getCacheDir().toString();
File file = new File(destinationDirectory + "/" + fileName);
Storage.Objects.Get get = client.objects().get(BUCKET_NAME_RANKINGS, fileName);
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
HashMap<Integer, Integer> testHash = null;
try {
get.executeAndDownloadTo(oos);
} catch (Exception e) {
e.printStackTrace();
} finally {
fos.close();
oos.close();
}
}
我不确定如何将原始数据从ObjectOutputStream输入回我的原始HashMap“ testHash”。从我在GCS存储桶中看到的内容,我什至不认为正确的数据甚至都保存到了存储桶中。它是0个字节,当我下载对象并在Notepad ++中查看时,它是不可读的。
任何帮助将不胜感激!!
珍妮
更新:
Brandon Yarbrough的建议允许我阅读为HashMap创建的内部文件(“ myViews”)。尝试将HashMap对象上传到我的GCS存储桶时,出现了我的新问题。据我所知,包含我的HashMap(“ ois”)的InputStream没有附加到我的存储对象上。
最佳答案
您正在使用ObjectOutputStream将可序列化的Java对象(在这种情况下为HashMap)转换为流数据。将该数据流重构为一个对象称为“反序列化”。在这种情况下,相应的流是ObjectInputStream
。您可以这样使用它:
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
HashMap<Integer, Integer> testHash;
testHash = (HashMap<Integer, Integer>) ois.readObject();
ois.close();
在此示例中,我保留了文件作为操作的一部分,但请记住,没有必要这样做。您几乎可以轻松地将下载的内容直接流式传输到ObjectInputStream中。
您可能还需要查阅ObjectInputStreams上的文档。