{"id":670,"date":"2022-05-25T19:21:31","date_gmt":"2022-05-25T17:21:31","guid":{"rendered":"https:\/\/www.valeriofinazzo.it\/wordpress\/?p=670"},"modified":"2022-05-25T19:21:32","modified_gmt":"2022-05-25T17:21:32","slug":"resttemplate-gestire-contenuto-multipart-mixed","status":"publish","type":"post","link":"https:\/\/www.valeriofinazzo.it\/wordpress\/2022\/05\/resttemplate-gestire-contenuto-multipart-mixed\/","title":{"rendered":"RestTemplate &#8211; Gestire contenuto Multipart-mixed"},"content":{"rendered":"\n<p>In questi giorni mi sono trovato a dover gestire il contenuto di servizio rest che a seguito  di una chiamata GET restituiva un content di tipo multipart-mixed. Nel progetto precedente usavo jersey che gestiva in mod del tutto trasparente questo tipo di chiamata, in questo progetto avevo una dipendenza da Spring ed ero vincolato ad usare la classe RestTemplate, peccato che per questa template la lettura di questo tipo di contenuto non fosse cos\u00ec agevole.<\/p>\n\n\n\n<p>Per risolvere ho letto la risposta come byte[]  e poi ho usato la classe org.apache.commons.fileupload.MultipartStream presente nel in <a href=\"https:\/\/commons.apache.org\/proper\/commons-fileupload\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Commons FileUpload\">Commons FileUpload<\/a> per leggere il contenuto da un byte[], scartare gli header e leggere solo il file di mio interesse<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RestTemplate restTemplate = new RestTemplate();\nbyte[] bytes = restTemplate.getForObject(fileUlr, byte[].class);\nString txt = new String(bytes);\nString boundary = StringUtils.substringBefore(txt, \"\\r\\n\").substring(2);\nMultipartStream multipartStream = new MultipartStream(new ByteArrayInputStream(bytes), \n\t\tboundary.getBytes(), 1024, null);\nboolean nextPart = multipartStream.skipPreamble();\nwhile (nextPart) {\n     \tmultipartStream.readHeaders();\n        ByteArrayOutputStream bos = new ByteArrayOutputStream();\n        multipartStream.readBodyData(bos);\n        return bos.toByteArray();\n}\nreturn bytes;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In questi giorni mi sono trovato a dover gestire il contenuto di servizio rest che a seguito di una chiamata GET restituiva un content di tipo multipart-mixed. Nel progetto precedente usavo jersey che gestiva in mod del tutto trasparente questo tipo di chiamata, in questo progetto avevo una dipendenza da Spring ed ero vincolato ad &hellip; <a href=\"https:\/\/www.valeriofinazzo.it\/wordpress\/2022\/05\/resttemplate-gestire-contenuto-multipart-mixed\/\" class=\"more-link\">Continua la lettura di <span class=\"screen-reader-text\">RestTemplate &#8211; Gestire contenuto Multipart-mixed<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[97],"tags":[152,21],"_links":{"self":[{"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/posts\/670"}],"collection":[{"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/comments?post=670"}],"version-history":[{"count":3,"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/posts\/670\/revisions"}],"predecessor-version":[{"id":674,"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/posts\/670\/revisions\/674"}],"wp:attachment":[{"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/media?parent=670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/categories?post=670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.valeriofinazzo.it\/wordpress\/wp-json\/wp\/v2\/tags?post=670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}