------------------------------------------------------------------------------------------
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();
}
}
}
}