2

I am trying to set up file upload example using JAX RS. I could set up the project when the client is a JSP page and there are no issues. But i would like to have the client as a java class which sends post request to the service. My server side code is as shown below.

import java.io.File;
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.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;

@Path("/file")
public class UploadFileService {

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

    String uploadedFileLocation = "d://uploaded/"
            + fileDetail.getFileName();


    writeToFile(uploadedInputStream, uploadedFileLocation);

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

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

}


private void writeToFile(InputStream uploadedInputStream,
        String uploadedFileLocation) {

    try {
        OutputStream 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();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

}

The client as a JSP page is as shown below.

<html>
<body>
<h1>File Upload with Jersey</h1>

<form action="http://localhost:9090/RESTfulExample/rest/file/upload" method="post" enctype="multipart/form-data">

   <p>
    Select a file : <input type="file" name="file" size="45" />
   </p>

   <input type="submit" value="Upload It" />
</form>

</body>
</html>

Can someone please help me with a class file as a client?

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
user732362
  • 395
  • 4
  • 10
  • 19

1 Answers1

1

You can use Apache's httpclient and httpmime to accomplish this:

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class Uploader {

  public void uploadFile(String endpoint, File file) throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(endpoint);
    FileBody uploadFilePart = new FileBody(file);
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("file", uploadFilePart);
    httpPost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httpPost);
    //Check response for success
  }

}

Which works with your sample server code. See this question, as well.

Community
  • 1
  • 1
condit
  • 10,852
  • 2
  • 41
  • 60