Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
674 views
in Technique[技术] by (71.8m points)

java - Photo don't uploaded correctly GCS App Engine

I'm working on my upload.java servlet to store .jpg to GCS.

I use GCS client library instead of Google Cloud Storage API. But I dont't find any sample to upload photo (doPost Method) so my code doesn't work...

Sending is a success (I can see my photo on GCS with a good size 465.24KB), but when I try to view this one, i can't:

enter image description here

Here is my code:

upload.java servlet public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

            //Read the file contents from the input stream
            GcsService gcsService = GcsServiceFactory.createGcsService();

            GcsFilename filename = new GcsFilename(BUCKETNAME, FILENAME);


            GcsFileOptions options = new GcsFileOptions.Builder()
                .mimeType("image/jpeg")
                .acl("public-read")
                .addUserMetadata("myfield1", "my field value")
                .build();

            GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options);
            InputStream is = request.getInputStream();

            try {
                copy(is, Channels.newOutputStream(writeChannel));
            } finally {
                writeChannel.close();
                is.close();
            }          

        }

      private void copy(InputStream input, OutputStream output) throws IOException {
            byte[] buffer = new byte[256];
            int bytesRead = input.read(buffer);
            while (bytesRead != -1) {
                output.write(buffer, 0, bytesRead);
                bytesRead = input.read(buffer);
            }
        }

android code (To send photo)

HttpPost httppost = new HttpPost("adress.appengine.com/upload");        


                //On créé un fichier File qui contient la photo que l'on vient de prendre (Accessible via son Path) 
                File f = new File(AccueilActivity.fileUri.getPath());       

                //On créé un FileBody (Contenu de notre requête http POST) contenant notre photo
                FileBody fileBody = new FileBody(f);

                //Permet de créer une requête avec plusieurs partie
                MultipartEntity reqEntity = new MultipartEntity();

                //On ajoute notre photo avec le mot clé file.
                reqEntity.addPart("file", fileBody);

                //On set notre requête HTTP
                httppost.setEntity(reqEntity);

                //On execute notre requete HTTP Post, et on appele du coup automatiquement le servlet "uploaded",
                //et on met la réponse dans response.
                HttpResponse response = httpClient.execute(httppost);                   

                //Ici on récupère l'entête de notre réponse HTTP (Tout sauf le code réponse)
                HttpEntity urlEntity = response.getEntity();

Thanks in advance !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...