2016-05-02 9 views

Antwort

0

Sie können Spring RestTemplate verwenden, um die Datei

RestTemplate templ = new RestTemplate(); 
byte[] downloadedBytes = templ.getForObject(url, byte[].class); 
zum Download

Extrahieren Sie den Inhalt mit Standard-Java oder einer Drittanbieter-Bibliothek.

Beispiel Dienstprogramm, von hier angepasst: http://www.codejava.net/java-se/file-io/programmatically-extract-a-zip-file-using-java

package com.test; 

import java.io.BufferedOutputStream; 
import java.io.ByteArrayInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

public class ZipHelper { 

    private static final int BUFFER_SIZE = 4096; 

    public static void unzip(byte[] data, String dirName) throws IOException { 
     File destDir = new File(dirName); 
     if (!destDir.exists()) { 
      destDir.mkdir(); 
     } 
     ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(data)); 
     ZipEntry entry = zipIn.getNextEntry(); 

     while (entry != null) { 
      String filePath = dirName + File.separator + entry.getName(); 
      if (!entry.isDirectory()) { 
       // if the entry is a file, extracts it 
       extractFile(zipIn, filePath); 
      } else { 
       // if the entry is a directory, make the directory 
       File dir = new File(filePath); 
       dir.mkdir(); 
      } 
      zipIn.closeEntry(); 
      entry = zipIn.getNextEntry(); 
     } 
     zipIn.close(); 
    } 

    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException { 
     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); 
     byte[] bytesIn = new byte[BUFFER_SIZE]; 
     int read = 0; 
     while ((read = zipIn.read(bytesIn)) != -1) { 
      bos.write(bytesIn, 0, read); 
     } 
     bos.close(); 
    } 
} 

Dann verwenden Sie dieses Dienstprogramm wie folgt:

ZipHelper.unzip(downloadedBytes, "/path/to/directory"); 
+0

Danke, ich muss Antwort entschlüsseln? Glaubst du, die Bibliothek benutzt? – KPN24

+0

Sorry, was meinst du mit "r denkt"? –

+0

Entschuldigung, 'oder', so etwas ?: Inflatter decompressor = new Inflater(); \t \t \t decappressor.setInput (Datei); \t \t \t ByteArrayOutputStream bos = neuer ByteArrayOutputStream (file.length); \t \t \t byte [] buf = neues Byte [1024]; \t \t \t während { \t \t \t int count = decompressor.inflate (buf) (decompressor.finished()!); \t \t \t bos.write (buf, 0, zählen); \t \t \t} \t \t \t bos.close(); \t \t byte [] dekomprimierteDaten = bos.toByteArray(); – KPN24

0

Der requesdt Körper so sein sollte:

@RequestMapping(value = "/push/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE, produces=MediaType.APPLICATION_JSON_VALUE) 
@ResponseStatus(HttpStatus.OK) 
public String pushTransactions(@PathVariable("id") String id, @RequestBody byte[] str) throws MessagingException, JsonParseException, JsonMappingException, IOException { 

und in der Steuerung, können Sie dies tun:

GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str)); 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     int len; 
     while ((len = gis.read(buffer)) > 0) { 
      out.write(buffer, 0, len); 
     } 

     gis.close(); 
     out.close(); 
+0

@RequestBody byte [] srt löst eine Ausnahme: Die Anmerkung für diesen Ort nicht zulässig ist . – KPN24

+0

@FrancescoPerfetti überprüfen Sie meine Probe erneut. Ich habe bearbeitet – Neron

+0

Gelöst danke trotzdem für Ihr Interesse. – KPN24

Verwandte Themen