Showing posts with label web services. Show all posts
Showing posts with label web services. Show all posts

Sunday, March 2

Jersey web service + File Upload + Maven + Tomcat +Java + Rest Web service









------------------------------------------------------------------------------------------

package com.gs.gsam.lo.du;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

// Plain old Java Object it does not extend as class or implements
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation.
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML.

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/upload")
public class DocumentUploader {

    /*
     * // @Produces(MediaType.TEXT_HTML)
     *
     * @GET public String sayHtmlHello() { return " " + " " + "Hello Jerse y  " + "" + "

"
     * + "Hello Jersey" + "

" + " "; }
     */
    @POST
    @Path("/pdf")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(@FormDataParam("file") File file) {
        InputStream IS = null;;
        String uploadedFileLocation = "d://" + "Test.zip";

        try {
            IS = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

        }

        // save it
        writeToFile(IS, uploadedFileLocation);

        String output = "The PDF File uploaded to : " + uploadedFileLocation;

        return Response.status(200).entity(output).build();

    }

    // save uploaded file to new location
    private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
        OutputStream out = null;
        try {
            out = new FileOutputStream(new File(uploadedFileLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
            uploadedInputStream.close();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();

                }
                if (uploadedInputStream != null) {
                    uploadedInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

-------------------------------------------------------------------------

package com.gs.gsam.lo.client;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.core.MediaType;

public class ClientJ {

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

      InputStream is=null;
  try {

    Client client = Client.create();

    WebResource webResource = client
       .resource("http://localhost:8080/FileUpload-0.0.1-SNAPSHOT/rest/upload/pdf");

    InputStream IS = null;;

    File file =new File("D:/file/file.zip");
   
    String uploadedFileLocation = "d://" + "Test.zip";

    try {
        IS = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

    }

    // save it
    writeToFile(IS, uploadedFileLocation);

    String output = "The PDF File uploaded to : " + uploadedFileLocation;
   
    // is=new FileInputStream(file);
   /* ClientResponse response = webResource.type(MediaType.MULTIPART_FORM_DATA)
       .post(ClientResponse.class,file );

    if (response.getStatus() != 200) {
      throw new RuntimeException("Failed : HTTP error code : "
           + response.getStatus());
    }

    System.out.println("Output from Server .... \n");
    String output = response.getEntity(String.class);
    System.out.println(output);

 */   System.exit(0);
    } catch (Exception e) {

    e.printStackTrace();

    }finally{
        if(null!=is){
            is.close();
        }
    }

  }
 
//save uploaded file to new location
  private static  void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
      OutputStream out = null;
      try {
          out = new FileOutputStream(new File(uploadedFileLocation));
          int read = 0;
          byte[] bytes = new byte[1024];

          out = new FileOutputStream(new File(uploadedFileLocation));
          while ((read = uploadedInputStream.read(bytes)) != -1) {
              out.write(bytes, 0, read);
          }
          out.flush();
          out.close();
          uploadedInputStream.close();
      } catch (IOException e) {

          e.printStackTrace();
      } finally {
          try {
              if (out != null) {
                  out.close();

              }
              if (uploadedInputStream != null) {
                  uploadedInputStream.close();
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }

  }

}

Friday, September 13

Java Today - NewsLetter





Java SE 8 JDK Preview Available Now iProgrammer
Oracle has made available a preview of the development kit for Java SE (Standard ... environment by adding closures and related features to the Java language.
See all stories on this topic »  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Java SE 7 Update 40 Released About - News & Issues
Java 7 Update 40 has been released and the JDK is available to download from Oracle's Java download page. The release notes show there are a number of ...
See all stories on this topic »  
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------







Gradle: do we need another build tool? | Java Code Geeks Keyhole Software
About Keyhole Software. Keyhole is a midwest-based consulting firm with a tight-knit technical team. We work primarily with Java, JavaScript and .
Java Code Geeks 
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Java 7 Update Includes Security Features | isssource.com gHale
Oracle released the Java standard edition version 7 update 40 (7u40), which includes bug fixes and some new features. The most notable security patch ...
isssource.com 
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------









Adding the Power of Search to Your Hibernate App the Easy Way ... timkitch
I'm currently working on a software project with a data layer that is built using Hibernate – an Object-Relational Mapping (ORM) framework that takes a lot...
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------
TDIing out loud: JSON and XML tutorial - Part 1 Eddie Hartman
If you're working with cloud services, or probably any kind of services, you're most likely working with JSON, XML (e.g. SOAP web services) or both. Although ...
TDIing out loud
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------





Blogspot XML generator - HTML, CSS and Javascript - Codecall
Blogspot XML generator - posted in HTML, CSS and Javascript: Hi, I know that its possible to edit a blog in blogspot using the export and import feature in XML.
Codecall RSS

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------
JVM Blog: Thinking about faith, technology and the future The Westby Times (blog)
Wednesday morning I found myself talking about how important my relationship with God is with a complete stranger. What I came to understand is that I don't ...
See all stories on this topic »

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Spring Framework Reference Documentation
Code equivalents for Spring's XML namespaces · 3.1.6. Support for Hibernate 4.x · 3.1.7. TestContext framework support for @Configuration classes and bean ...
docs.spring.io/spring/docs/3.1.x/spring-framework.../html/