Download DOC File Using CXF REST / JAX-RS

Run Client

You can test this service in two different ways

1) Using Java Program

package com.client;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

public class DownloadFileClient {

  
public static void main(String[] args) {
     
try {
        
URL url = new URL("http://localhost:8080/CXFRestUpload/services/rest/downloadFile");
         HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
         conn.setDoOutput
(true);
         conn.setRequestMethod
("GET");
         conn.setRequestProperty
("Content-Type", "application/json");
        
if (conn.getResponseCode() == 200) {
           
InputStream inputStream = conn.getInputStream();
            OutputStream output =
new FileOutputStream("C:/downloads/copyOfTest.doc");
           
byte[] buffer = new byte[1024];
           
int bytesRead;
           
while ((bytesRead = inputStream.read(buffer)) != -1) {
              
output.write(buffer, 0, bytesRead);
           
}
           
output.close();
        
} else {
           
Scanner scanner = new Scanner(conn.getErrorStream());
            System.out.println
(scanner.next());
            scanner.close
();
        
}
        
conn.disconnect();
     
} catch (MalformedURLException e) {
        
e.printStackTrace();
     
} catch (IOException e) {
        
e.printStackTrace();
     
}
   }
}
Output

Download Doc File Using CXF REST / JAX-RS

2) Using Browser
Download Doc File Using CXF REST / JAX-RS





Previous 1 2








Your email address will not be published. Required fields are marked *