2013-08-26 38 views
0

everyone。使用java rest客戶端獲取zip文件(restEasy)

我開始使用restEasy(jboss)java rest客戶端,並遇到了一個我似乎無法解決的問題。 到目前爲止,我可以使用它從其他服務器(字符串表單)返回json。 但是,我需要的其餘服務之一將帶回一個zip文件。我偶然發現了一個問題。 下面是代碼:

ClientRequest req = new ClientRequest("rest service url"); //the url is good 

ClientResponse<String> res = null; 

res = req.get(String.class); 

ZipInputStream zip = new ZipInputStream(new 
     ByteArrayInputStream(res.getEntity().getBytes())); 

ZipEntry zipEntry = zip.getNextEntry(); 
     System.out.println(zipEntry.getName()); 
    //here, I print the name of the first file in my archive, so, I seem to have a  
    // zip file indeed 

String jsonString = IOUtils.toString(zip); 
    //bam, this is causing a zipException : invalid block type 

谷歌告訴我,這是讀取壓縮文件的正確方法。我試圖逐字節讀取它,並在zip.read()中引發sams異常。

我做錯了什麼? 我該怎麼做才能閱讀我的文件內容?

如果對此事有任何見解,我會很樂意。 謝謝

P.S:對不起,如果我聽起來很奇怪,英語不是我的第一語言。

回答

0
URL url = new URL("http://xyz.com/download.zip"); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("GET"); 
IntpuStream in = connection.getInputStream(); 
FileOutputStream out = new FileOutputStream("download.zip"); 
copy(in, out, 1024); 
out.close(); 


    public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException { 
    byte[] buf = new byte[bufferSize]; 
    int n = input.read(buf); 
    while (n >= 0) { 
     output.write(buf, 0, n); 
     n = input.read(buf); 
    } 
    output.flush(); 
    } 
+0

這個伎倆!非常感謝 ! – DeH

0
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.util.Date; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

    public class ZipReader { 
     // Expands the zip file passed as argument 1, into the 
     // directory provided in argument 2 
     public static void main(String args[]) throws Exception 
     { 
      if(args.length != 2) 
      { 
       System.err.println("zipreader zipfile outputdir"); 
       return; 
      } 

     // create a buffer to improve copy performance later. 
     byte[] buffer = new byte[2048]; 

     // open the zip file stream 
     InputStream theFile = new FileInputStream(args[0]); 
     ZipInputStream stream = new ZipInputStream(theFile); 
     String outdir = args[1]; 

     try 
     { 

      // now iterate through each item in the stream. The get next 
      // entry call will return a ZipEntry for each file in the 
      // stream 
      ZipEntry entry; 
      while((entry = stream.getNextEntry())!=null) 
      { 
       String s = String.format("Entry: %s len %d added %TD", 
           entry.getName(), entry.getSize(), 
           new Date(entry.getTime())); 
       System.out.println(s); 

       // Once we get the entry from the stream, the stream is 
       // positioned read to read the raw data, and we keep 
       // reading until read returns 0 or less. 
       String outpath = outdir + "/" + entry.getName(); 
       FileOutputStream output = null; 
       try 
       { 
        output = new FileOutputStream(outpath); 
        int len = 0; 
        while ((len = stream.read(buffer)) > 0) 
        { 
         output.write(buffer, 0, len); 
        } 
       } 
       finally 
       { 
        // we must always close the output file 
        if(output!=null) output.close(); 
       } 
      } 
     } 
     finally 
     { 
      // we must always close the zip file. 
      stream.close(); 
     } 
    } 
} 
+0

無,問題是stream.read(buffer)引發了相同的異常。這是我在搜索時找到的一個頁面,我已經可以嘗試。我認爲這個問題會在剩下的電話中出現,但我不能把它放在手上。 – DeH

+0

嘗試新的 –

+0

優秀!這似乎是一種魅力。謝謝你的時間。 – DeH

0

比方說你的服務被定義爲以下服務器端

/** 
* 
* http://rama-local:8081/RESTfulDemoApplication/files/download/Test.pdf 
* Where Test.pdf is the document I have stored under SERVER_DOWNLOAD_LOCATION_FOLDER path. 
* 
*/ 


@GET 
@Path("/files/download/{fileName}") 
@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response getFile(@PathParam("fileName") String fileName) { 
    File file = new File(SERVER_DOWNLOAD_LOCATION_FOLDER+fileName); 
    ResponseBuilder response = Response.ok((Object) file); 
    response.header("Content-Disposition","attachment; filename="+fileName); 
    return response.build(); 

} 

您可以在客戶端定義一個接口

public interface RestEasyFileServiceRestfulClient { 


    @GET 
    @Path("/files/download/{fileName}") 
    @Produces(MediaType.APPLICATION_OCTET_STREAM) 
    public Response getFile(@PathParam("fileName") String fileName); 

} 

然後,你可以下載文件使用java rest客戶端如下

Class RestTest { 

    public static void main(String[] args) throws Exception { 

    String fileServiceUrl = "http://rama-local:8081/RESTfulDemoApplication/files"; 
    RestEasyFileServiceRestfulClient fileServiceClient =  ProxyFactory.create(RestEasyFileServiceRestfulClient.class,fileServiceUrl); 

    BaseClientResponse response = (BaseClientResponse)fileServiceClient.getFile("ASD.zip"); 
    File s = (File)response.getEntity(File.class); 
    File ff = new File("C:\\ASD.zip"); 
    s.renameTo(ff); 
    FileWriter fr = new FileWriter(s); 
    fr.flush(); 
    System.out.println("FileDownload Response = "+ response.getStatus()); 
} 
} 

這需要 - RestEasy的-JAXRS-2.3.1.GA.jar - RestEasy的多域提供商 - 2.3.1.GA.jar

更多信息以

http://docs.jboss.org/resteasy/docs/1.0.2.GA/userguide/html/RESTEasy_Client_Framework.html