how to upload file into googlecloud through java -
i want upload file bucket in google cloud storage api. when run servelet class deployed , shows output in browser " see here file content, have uploaded on storage.. file uploading done" .
but problem servelet class not establish connection google cloud storage.and file not uploaded bucket.once check
the code , give suggestion how connect bucket source code.
public class testcloudstorageservlet extends httpservlet{ private static final long serialversionuid = 1l; private storageservice storage = new storageservice(); private static final int buffer_size = 1024 * 1024; private static final logger log = logger.getlogger(testcloudstorageservlet.class.getname()); @override public void dopost(httpservletrequest req, httpservletresponse resp) throws ioexception { log.info(this.getservletinfo()+" servlets called...."); resp.setcontenttype("text/plain"); resp.getwriter().println("now see here file content, have uploaded on storage.."); servletfileupload upload = new servletfileupload(); system.out.println(upload); fileitemiterator iter; try { iter = upload.getitemiterator(req); while (iter.hasnext()) { fileitemstream item = iter.next(); string filename = item.getname(); string mime = item.getcontenttype(); storage.init(filename, mime); inputstream = item.openstream(); byte[] b = new byte[buffer_size]; int readbytes = is.read(b, 0, buffer_size); while (readbytes != -1) { storage.storefile(b, readbytes); readbytes = is.read(b, 0, readbytes); } is.close(); storage.destroy(); resp.getwriter().println("file uploading done"); //resp.getwriter().println("read:" + storage.readtextfileonly(filename)); log.info(this.getservletname()+" ended...."); } } catch (fileuploadexception e) { system.out.println("fileuploadexception::"+e.getmessage()); log.severe(this.getservletname()+":fileuploadexception::"+e.getmessage()); e.printstacktrace(); } catch (exception e) { log.severe(this.getservletname()+":exception::"+e.getmessage()); system.out.println("exception::"+e.getmessage()); e.printstacktrace(); } }
my storageservice class uploading file.
public class storageservice { public static final string bucket_name = "mybucketname"; private filewritechannel writechannel = null; fileservice fileservice = fileservicefactory.getfileservice(); private outputstream os = null; private static final logger log = logger.getlogger(storageservice.class.getname()); public void init(string filename, string mime) throws exception { system.out.println("storage service:init() method: file name:"+filename+" , mime:"+mime); log.info("storage service:init() method: file name:"+filename+" , mime:"+mime); log.info("test.."); gsfileoptionsbuilder builder = new gsfileoptionsbuilder() .setacl("public_read") .setbucket(bucket_name) .setkey(filename) .setmimetype(mime); log.info("test.."); appenginefile writablefile = fileservice.createnewgsfile(builder.build()); boolean lock = true; writechannel = fileservice.openwritechannel(writablefile, lock); os = channels.newoutputstream(writechannel); } public void storefile(byte[] b, int readsize) throws exception { os.write(b, 0, readsize); os.flush(); } public void destroy() throws exception { log.info("storage service: destroy() method"); os.close(); writechannel.closefinally(); } }
Comments
Post a Comment