google-api-java-client icon indicating copy to clipboard operation
google-api-java-client copied to clipboard

Problems uploading files to google drive: java.io.IOException: Error writing request body to server

Open lingyun-zhu opened this issue 5 years ago • 4 comments

Storage: com.google.cloud.storage.StorageException: Error writing request body to server #3410

Caused by: java.io.IOException: Error writing request body to server
    at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:3593)
    at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:3576)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:126)
    at java.util.zip.DeflaterOutputStream.deflate(DeflaterOutputStream.java:253)
    at java.util.zip.DeflaterOutputStream.write(DeflaterOutputStream.java:211)
    at java.util.zip.GZIPOutputStream.write(GZIPOutputStream.java:145)
    at com.google.api.client.util.ByteStreams.copy(ByteStreams.java:53)
    at com.google.api.client.util.IOUtils.copy(IOUtils.java:87)
    at com.google.api.client.http.AbstractInputStreamContent.writeTo(AbstractInputStreamContent.java:67)
    at com.google.api.client.http.GZipEncoding.encode(GZipEncoding.java:50)
    at com.google.api.client.http.HttpEncodingStreamingContent.writeTo(HttpEncodingStreamingContent.java:48)
    at com.google.api.client.http.javanet.NetHttpRequest$DefaultOutputWriter.write(NetHttpRequest.java:76)
    at com.google.api.client.http.javanet.NetHttpRequest.writeContentToOutputStream(NetHttpRequest.java:171)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:117)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012)
    at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithoutGZip(MediaHttpUploader.java:551)
    at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequest(MediaHttpUploader.java:568)
    at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:422)
    at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:336)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:551)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:475)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:592)
    ... 48 common frames omitted

In others places(google-cloud-java ) , google api added a retry to successfully solved this problem. So I want to know if this error could be solved by add a similar retry? thank you.

lingyun-zhu avatar May 15 '20 09:05 lingyun-zhu

at com.google.api.client.util.ByteStreams.copy(ByteStreams.java:53)

at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithoutGZip(MediaHttpUploader.java:551)

It seems that something went wrong.The problem with googleapi like this, (this time is MediaHttpUploader) Is there any solution to the problem?

lingyun-zhu avatar May 15 '20 10:05 lingyun-zhu

Can you help add a snippet of code that is causing this?

chingor13 avatar May 21 '20 22:05 chingor13

@chingor13 Thank you for your prompt reply. I tried with the following code to upload file to Google Drive by using inputStream. The result is successful upload in most cases,but occasionally failed with java.io.IOException: Error writing request body to server. it seems like this issue google-cloud-java:StorageException: Error writing request body to server #3410

Code Snippet:


public class CreateDocument {                                                                                                   
	private static final String APP_NAME = "Dummy APP";                                                                                                    
	private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();                                                                                                     
	private String folderId;                                                                                                  
	private InputStream inputStream;                                                                                                            
	private HttpTransport HTTP_TRANSPORT;                                                                                                            

	public String execute(String accessToken) throws Exception {                                                                                                       
		Drive driveService = createGDriveService(accessToken);                                                                                                 
		File fileMetadata = getFileMetadata(); //MimeType is "application/pdf"
		BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);                                                                                                    
		InputStreamContent mediaContent = new InputStreamContent(fileMetadata.getMimeType(), bufferedInputStream);                                                                                                  
		Drive.Files.Create request;                                                                                                          

		request = driveService.files().create(fileMetadata, mediaContent);                                                                                                            
		request.setFields("*");                                                                                                  
		request.setSupportsAllDrives(true);
		//[IOException: Error writing request body to server] is happened when i run execute()
		//file upload is success in most cases but occasionally failed
		File response = request.execute(); 

		return response.getId();                                                                                                
	}                                                                                                              

	public Drive createGDriveService(String accessToken) throws Exception {                                                                                                               
		SimpleHttpInitializer initializer = new SimpleHttpInitializer(accessToken); //READ_TIMEOUT is 3 minutes,CONNECT_TIMEOUT is also 3 minutes
		return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, initializer).setApplicationName(APP_NAME).build();                                                                                                        
	}                                                                                                              
}                                                                                                              

lingyun-zhu avatar May 22 '20 05:05 lingyun-zhu

Hello everyone i have same problem now how should i do to fix it? thx

bakazhou avatar Feb 24 '23 05:02 bakazhou