Download PDF File Using CXF REST / JAX-RS

Download PDF File Using CXF REST / JAX-RS explains about downloading PDF file using CXF REST / JAX-RS API

@Produces("application/pdf")

On the above annotation, we are mapping the response as PDF mime type, you can change mime type according to different file formats

response.header("Content-Disposition", "attachment; filename=test.pdf");

We are also setting header as "Content-Disposition" in order to open file as an attachment

This tutorial is for downloading PDF file, but you can see different mime types(formats) below

Download PDF File Using CXF REST / JAX-RS
Download DOC File Using CXF REST / JAX-RS
Download Excel File Using CXF REST / JAX-RS
Download Image File Using CXF REST / JAX-RS
Download Text File Using CXF REST / JAX-RS

Download PDF File Using CXF REST / JAX-RS

I am going to re-use CXF REST File Upload

We are making changes on UploadServiceImpl class, just adding new method downloadFile() in order to download pdf file

package com.student;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.activation.DataHandler;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;

public class UploadServiceImpl {
  
private static final String FILE_PATH = "c:\\downloads\\test.pdf";

  
@GET
   @Path
("/downloadFile")
  
@Produces("application/pdf")
  
public Response downloadFile() {
     
File file = new File(FILE_PATH);
      ResponseBuilder response = Response.ok
((Object) file);
      response.header
("Content-Disposition", "attachment; filename=test.pdf");
     
return response.build();
  
}

  
@POST
   @Path
("/uploadFile")
  
@Consumes(MediaType.MULTIPART_FORM_DATA)
  
public Response uploadFile(List<Attachment> attachments, @Context HttpServletRequest request) {
     
for (Attachment attachment : attachments) {
        
DataHandler handler = attachment.getDataHandler();
        
try {
           
InputStream stream = handler.getInputStream();
            MultivaluedMap<String, String> map = attachment.getHeaders
();
            System.out.println
("fileName Here" + getFileName(map));
            OutputStream out =
new FileOutputStream(new File("C:/uploads/" + getFileName(map)));

           
int read = 0;
           
byte[] bytes = new byte[1024];
           
while ((read = stream.read(bytes)) != -1) {
              
out.write(bytes, 0, read);
           
}
           
stream.close();
            out.flush
();
            out.close
();
        
} catch (Exception e) {
           
e.printStackTrace();
        
}
      }

     
return Response.ok("file uploaded").build();
  
}

  
private String getFileName(MultivaluedMap<String, String> header) {
     
String[] contentDisposition = header.getFirst("Content-Disposition").split(";");
     
for (String filename : contentDisposition) {
        
if ((filename.trim().startsWith("filename"))) {
           
String[] name = filename.split("=");
            String exactFileName = name
[1].trim().replaceAll("\"", "");
           
return exactFileName;
        
}
      }
     
return "unknown";
  
}
}

1 2 Next





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