CXF REST File Upload

Create a Service Interface

Here we are implement the logic, this will defines which methods of restful service, to be invoked by the client

We are using annotations @Consumes(MediaType.MULTIPART_FORM_DATA) in order to upload the CXF Multipart file attachments

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.POST;
import javax.ws.rs.Path;
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 org.apache.cxf.jaxrs.ext.multipart.Attachment;

public class UploadServiceImpl {

  
@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";
  
}
}

Here we are using one example showing with POST method

GET---> Calling this method will not result any changes to the server

POST---> Calling this method will result changes to the server, This have more secure than GET method

@Consumes annotation specifies, the request is coming from the client

you can specify the Mime type as @Consumes("application/xml"), if the request is in xml format

@Produces annotation specifies, the response is going to the client

you can specify the Mime type as @Produces ("application/xml"), if the response need to be in xml format

Create a cxf.xml

CXF is using Spring internally, Finding classes by spring we need to add service implementation beans are added on "jaxrs:serviceBeans".

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <jaxrs:server id="base" address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="UploadService" />
        </jaxrs:serviceBeans>
    </jaxrs:server>
    <bean id="UploadService" class="com.student.UploadServiceImpl" />
</beans>









Previous 1 2 3 Next





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