3

It seems like there are several posts such as here asking how to use Apache Commons HTTPClient libraries in Java to do a POST to a Servlet. However, it seems like I'm having some problems doing the same thing with a annotated Spring controller method. I've tried a few things but gotten HTTP 401 Bad Request responses from the server. Any examples of doing this would be greatly appreciated.

EDIT: Code I am trying to use:

//Server Side (Java)
@RequestMapping(value = "/create", method = RequestMethod.POST)
public void createDocument(@RequestParam("userId") String userId,
                           @RequestParam("file") MultipartFile file, HttpServletResponse response) {
    // Do some stuff                            
}

//Client Side (Groovy)
    void processJob(InputStream stream, String remoteAddress) {
    HttpClient httpclient = new DefaultHttpClient()
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1)
    HttpPost httppost = new HttpPost("http://someurl/rest/create")

    MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
    InputStreamBody uploadFilePart = new InputStreamBody(stream, 'application/octet-stream', 'test.file')
    mpEntity.addPart('file', uploadFilePart)
    mpEntity.addPart('userId', new StringBody('testUser'))
    httppost.setEntity(mpEntity)

    HttpResponse response = httpclient.execute(httppost);
    println(response.statusLine)
}

Still getting 400 Bad Request in the response from the server.

Community
  • 1
  • 1
Greymeister
  • 312
  • 4
  • 12

2 Answers2

4

I hate to answer my own question when it shows incompetence, but it turns out the code was fine, this particular controller did not have a CommonsMultipartResolver defined in its servlet-context.xml file (multiple DispatcherServlets...long story :()

Here's what I added to make it work:

<!-- ========================= Resolver DEFINITIONS ========================= -->
<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="50000000"/>
</bean>
Greymeister
  • 312
  • 4
  • 12
2

Here is an example from the Spring Reference:

@Controller
public class FileUpoadController {

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
           return "redirect:uploadSuccess";
       } else {
           return "redirect:uploadFailure";
       }
    }

}
earldouglas
  • 13,265
  • 5
  • 41
  • 50
  • James, that is a working example from the Spring controller side, I'm trying to figure out the client side using HTTPClient, any ideas? – Greymeister Feb 04 '11 at 16:11
  • On the client side, you can use Spring's [RestTemplate](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#rest-client-access) to consume RESTful Web services. I'm still trying to dig up a more general HTTP client example. – earldouglas Feb 04 '11 at 17:26
  • 1
    [Here](http://www.earldouglas.com/a-secure-restful-web-service) I made a *very* rudimentary HTTP client in the `EmployeeControllerTest.get` method. Hopefully it can point you in the right direction. – earldouglas Feb 04 '11 at 17:28