栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

照片未正确上传GCS App Engine

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

照片未正确上传GCS App Engine

我终于(自己)找到了解决我问题的方法!我为那= D感到骄傲

The pre for Android:

//Actions à réaliser en fond        protected String doInBackground(String... params) { //Création d'une requête HTTP HttpClient httpClient = new DefaultHttpClient(); //On construit notre adresse Post grâce à ce que l'on vient de récupérer HttpPost httppost = new HttpPost("http://your_id.appspot.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()); //Et là on essaie ! try {      MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();      entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);       entityBuilder.addBinaryBody("Photo_a_uploader", f);        entityBuilder.addTextBody("Type", "Type que l'utilisateur a choisi");      httppost.setEntity(entityBuilder.build());      HttpResponse response = httpClient.execute(httppost);        } catch (ClientProtocolException e) { // Si on tombe dans le catch, on set notre variable à 2.         e.printStackTrace();        } catch (IOException e) { // Si on tombe dans le catch, on set notre variable à 2.  e.printStackTrace();        }

The pre App Engine

 @Override          public void doPost(HttpServletRequest req, HttpServletResponse res)   throws ServletException, 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/jpg")         .acl("public-read")         .addUsermetadata("myfield1", "my field value")         .build(); GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options); ServletFileUpload upload = new ServletFileUpload(); res.setContentType("text/plain"); try {     FileItemIterator iterator = upload.getItemIterator(req);         while (iterator.hasNext()) {  FileItemStream item = iterator.next();  InputStream stream = item.openStream();  if (item.isFormField()) {    log.warning("Champs texte avec id: " + item.getFieldName()+", et nom: "+Streams.asString(stream));  } else {    log.warning("Nous avons un fichier à uploader : " + item.getFieldName() +     ", appelé = " + item.getName());    // You now have the filename (item.getName() and the    // contents (which you can read from stream). Here we just    // print them back out to the servlet output stream, but you    // will probably want to do something more interesting (for    // example, wrap them in a Blob and commit them to the    // datastore).    // Open a channel to write to it    byte[] bytes = ByteStreams.toByteArray(stream);    try {          writeChannel.write(ByteBuffer.wrap(bytes));    } finally {          writeChannel.close();          stream.close();    }          }    }     } catch (FileUploadException e) {         // TODO Auto-generated catch block         e.printStackTrace();     }         }

我希望这些示例可以帮助您使用App Engine和Google云存储!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372736.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号