Import Spread Sheet failed through

53 views
Skip to first unread message

Ramesh

unread,
Apr 25, 2011, 11:40:41 AM4/25/11
to gdata-java-client
Hi All,
When i am trying to import spread sheet i am getting this error.

Exception in thread "main"
com.google.gdata.util.InvalidEntryException: Bad Request
Invalid request URI

at
com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:
594)
at
com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:
563)
at
com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:
552)
at
com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:
530)
at
com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:
535)
at
com.gulfinfotech.atss.server.spreadsheet.SpreadSheetHelper.convertXlsToSpreadSheet(SpreadSheetHelper.java:
115)
at
com.gulfinfotech.atss.server.spreadsheet.SpreadSheetHelper.main(SpreadSheetHelper.java:
139)


Here is the code i am using


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import com.google.gdata.client.Service;
import com.google.gdata.client.Service.GDataRequest;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.media.MediaSource;
import com.google.gdata.data.media.MediaStreamSource;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;

public class SpreadSheetHelper {

private static final Logger log =
Logger.getLogger(SpreadSheetHelper.class
.getName());

private static final String META_FEED_URL = "https://
spreadsheets.google.com/feeds/spreadsheets/private/full";

/*
*
*/
private String userEmail;

/*
*
*/
private String password;

/*
*
*/
private String appId;

public SpreadSheetHelper(String userEmail, String password, String
appId) {
this.userEmail = userEmail;
this.password = password;
this.appId = appId;
}

public SpreadsheetService createSpreadSheetService()
throws AuthenticationException {
SpreadsheetService spreadSheetService = new
SpreadsheetService(appId);
spreadSheetService.setUserCredentials(userEmail, password);
log.info("spreadSheetService:" +
spreadSheetService.getServiceVersion());
return spreadSheetService;
}

public DocsService createDocumentService() throws
AuthenticationException {
DocsService docsService = new DocsService(appId);
docsService.setUserCredentials(userEmail, password);
return docsService;
}

public SpreadsheetEntry loadSpreadSheet(
SpreadsheetService spreadSheetService, String spreadSheetName) {
log.info("loadSpreadSheet:" + spreadSheetName);
SpreadsheetEntry spreadSheetEntry = null;
try {
URL metafeedUrl = new URL(META_FEED_URL);
SpreadsheetFeed feed = spreadSheetService.getFeed(metafeedUrl,
SpreadsheetFeed.class);

List<SpreadsheetEntry> spreadsheets = feed.getEntries();
for (int i = 0; i < spreadsheets.size(); i++) {
SpreadsheetEntry entry = spreadsheets.get(i);
log.info(i + ":entry:" + entry.getTitle().getPlainText());
log.info(""
+ entry.getTitle().getPlainText()
.equals(spreadSheetName));
if (entry.getTitle().getPlainText().equals(spreadSheetName)) {
log.info(i + ":selected entry:"
+ entry.getTitle().getPlainText());
spreadSheetEntry = entry;
break;
}
}

} catch (Exception e) {
log.info("Loading Worksheet failed:" + e.getMessage());
}

return spreadSheetEntry;
}

public boolean convertXlsToSpreadSheet(InputStream excelInputStream,
SpreadsheetService service,
SpreadsheetEntry spreadsheetEntry, String attachmentMimeType)
throws IOException,
InvalidFormatException, ServiceException {

MediaStreamSource mediaStreamSource=new
MediaStreamSource(excelInputStream, attachmentMimeType);
log.info("mediaStreamSource:"+mediaStreamSource.getContentType());

Service.GDataRequest request =
service.createRequest(GDataRequest.RequestType.INSERT,
new URL("http://spreadsheets.google.com/feeds/default/private/
full"), new ContentType(attachmentMimeType));

log.info("request:"+request);

request.setHeader("Slug",
spreadsheetEntry.getTitle().getPlainText());

MediaSource.Output.writeTo(mediaStreamSource,
request.getRequestStream());

// Send the request (HTTP POST)
request.execute();

// Save the response
InputStream responseStream = request.getResponseStream();
try {
byte[] buffer = new byte[1024];
int l;
while ( (l=responseStream.read(buffer)) > 0 ) {
System.out.write(buffer, 0, l);
}
} finally {
responseStream.close();
}

return false;
}

public static void main(String[] args) throws Exception{
SpreadSheetHelper spreadSheetHelper=new SpreadSheetHelper("emailId",
"password", "test");
SpreadsheetService
spreadSheetService=spreadSheetHelper.createSpreadSheetService();
SpreadsheetEntry
spreadSheetEntry=spreadSheetHelper.loadSpreadSheet(spreadSheetService,
"existingSpreadSheetName");

File f=new File("C:\\LocalFileExcel.xlsx");
FileInputStream excelInputStream=new FileInputStream(f);

spreadSheetHelper.convertXlsToSpreadSheet(excelInputStream,spreadSheetService,spreadSheetEntry,"application/
vnd.ms-excel");
}
}


Thanks for your responses

Ramesh

unread,
Apr 28, 2011, 4:17:47 AM4/28/11
to gdata-java-client
I Found an other way for importing spread sheets

package com.esuave;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import sun.font.CreatedFontTracker;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.media.MediaSource;
import com.google.gdata.data.media.MediaStreamSource;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class UploadTest {
private static final String META_FEED_URL = "https://docs.google.com/
feeds/default/private/full";

public DocumentListEntry uploadFile(String filepath, String title)
throws IOException, ServiceException {
DocsService client = new DocsService("yourCo-yourAppName-v1");
client.setUserCredentials("email", "password");

File file = new File(filepath);
String mimeType =
DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();

System.out.println("mimeType:"+mimeType);

DocumentListEntry newDocument = new DocumentListEntry();

newDocument.setFile(file, mimeType);
newDocument.setTitle(new PlainTextConstruct(title));

// Prevent collaborators from sharing the document with others?
// newDocument.setWritersCanInvite(false);

return client.insert(new URL("https://docs.google.com/feeds/default/
private/full/"), newDocument);
}

public DocumentListEntry uploadStream(InputStream in, String title,
DocsService service, SpreadsheetEntry entry)
throws IOException, ServiceException {
String etag = entry.getEtag();
System.out.println("etag:"+etag);
String mimeType =
DocumentListEntry.MediaType.fromFileName("SSS-
Problems.xlsx").getMimeType();
MediaStreamSource ms = new MediaStreamSource(in,mimeType);
ms.setEtag(etag);
service.getRequestFactory().setHeader("If-Match", etag);
try
{
String url = entry.getMediaEditLink().getHref()+"?
convert=false";
System.out.println("url:"+url);
service.updateMedia(new URL(url), SpreadsheetEntry.class,
ms);
}
finally
{
service.getRequestFactory().setHeader("If-Match", null);
}
return null;
}

public static DocsService createDocsService()
throws AuthenticationException {
DocsService spreadSheetService = new DocsService("test");
spreadSheetService.setUserCredentials("apps...@gulfinfotech.com",
"google123");
return spreadSheetService;
}

public void printDocuments(DocumentListFeed feed) {
for (DocumentListEntry entry : feed.getEntries()) {
String resourceId = entry.getResourceId();
System.out.println(" -- Document(" + resourceId + "/" +
entry.getTitle().getPlainText() + ")");
}
}

public static SpreadsheetEntry loadSpreadSheet(
DocsService spreadSheetService, String spreadSheetName) {
SpreadsheetEntry spreadSheetEntry = null;
try {
URL metafeedUrl = new URL(META_FEED_URL);
SpreadsheetFeed feed = spreadSheetService.getFeed(metafeedUrl,
SpreadsheetFeed.class);

List<SpreadsheetEntry> spreadsheets = feed.getEntries();
for (int i = 0; i < spreadsheets.size(); i++) {
SpreadsheetEntry entry = spreadsheets.get(i);
System.out.println("entry:"+entry.getTitle().getPlainText());
if (entry.getTitle().getPlainText().equals(spreadSheetName)) {
spreadSheetEntry = entry;
break;
}
}

} catch (Exception e) {
e.printStackTrace();
}

return spreadSheetEntry;
}

public static void main(String[] args) throws Exception{
UploadTest test=new UploadTest();

FileInputStream stream=new FileInputStream("C:\\SS-Problems.xlsx");
DocsService service=createDocsService();
SpreadsheetEntry entry=loadSpreadSheet(service, "sss-problems");
System.out.println("selected entry:"+entry);
test.uploadStream(stream, "sss-problems", service, entry);
Reply all
Reply to author
Forward
0 new messages