我正在开发一个 Java - JSF 网络应用程序,我在其中显示了一个弹出窗口,以便让用户从 Google 云端硬盘中选择一个文档进行下载。
为此,我有一些 js 代码:
<script src="filepicker.js"></script>
<script>
function initPicker() {
var picker = new FilePicker({
apiKey: 'MY_API_KEY',
clientId: my_client_id,
buttonEl: document.getElementById('pick'),
onSelect : function(file) {
if (file.id) {
sendLinkToBean(file.id);
} else {
alert('Unable to download file.');
}
}
});
}
</script>
<a4j:jsFunction name="sendLinkToBean" action="#{gPPersonFormBean.downloadFile()}">
<a4j:param name="param1" assignTo="#{gPPersonFormBean.fileId}"/>
</a4j:jsFunction>
file.id 到达 Bean,我尝试获取它,如 G.Drive 的 API 所示:
public void downloadFile(){
try {
Drive driveService = getDriveService();
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId) .executeMediaAndDownloadTo(outputStream);
} catch (IOException e) {
String mess = "downloadFile(): "
+ (e.getMessage()!=null?". "+e.getMessage():"")
+ (e.getCause()!=null?". "+e.getCause():"");
logger.error(mess);
}
}
但是我得到 FileNotFoundException:
com.google.api.client.http.HttpResponseException: 404 Not Found
{
"error": {"theID"
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "File not found: 1tvnGWDCse4TFQwIvqEDTlvv2cebfs0C2JBtjTP5A42I.",
"locationType": "parameter",
"location": "fileId"
}
],
"code": 404,
"message": "File not found: theID."
}
}
有谁知道为什么会这样? 提前致谢。
编辑:我刚刚将收到的 ID 与 Google 提供的带有文件链接的 ID 进行了比较,它们完全相同。
编辑 2:如果我执行 driveService.files().list();
,它会返回一个空列表。
最佳答案
如果您想下载 Google 文档,请使用 files.export。能反射(reflect)下面的脚本试试看吗?
String fileId = "## file ID ##";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, "application/pdf")
.executeMediaAndDownloadTo(outputStream);
- 这个示例脚本和详细信息是here .
如果我误解了你的问题,我很抱歉。
关于javascript - 从刚刚检索到的 id 获取 Google Doc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239841/