I created a photo downloader that works great with javax.ws.rs. Here is the signature and the main meaning:
@POST @Path("/upload/photo") @Consumes("multipart/form-data") @Produces("application/json") public String uploadPhoto(InputStream stream){ try { int read = 0; FileOutputStream fos = new FileOutputStream(file); CountingOutputStream out = new CountingOutputStream(fos); byte[] bytes = new byte[MAX_UPLOAD_SIZE]; while ((read = stream.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (IOException e) {
I can verify this using the apache.commons.httpClient library as follows:
@Test public void testUpload() { int statusCode = 0; String methodResult = null; String endpoint = SERVICE_HOST + "/upload/photo"; PostMethod post = new PostMethod(endpoint); File file = new File("/home/me/Desktop/someFolder/image.jpg"); FileRequestEntity entity = new FileRequestEntity(file, "multipart/form-data"); post.setRequestEntity(entity); try { httpClient.executeMethod(post); methodResult = post.getResponseBodyAsString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } statusCode = post.getStatusCode(); post.releaseConnection();
It works great! The problem is that the rest of the application is written using Spring MVC. When I use the Spring Mock MVC test framework, the program just freezes (shown in the code snippet below this). Here is the SpringMVC code for the bootloader:
@ResponseBody @RequestMapping( produces="application/json", consumes="multipart/form-data", method=RequestMethod.POST, value="/photo") public String uploadPhoto(@RequestPart("file") MultipartFile multipartFile){ try { int read = 0; FileOutputStream fos = new FileOutputStream(file); CountingOutputStream out = new CountingOutputStream(fos); byte[] bytes = new byte[MAX_UPLOAD_SIZE]; while ((read = multipartFile.getInputStream().read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (IOException e) {
And below is what I used for testing using Spring Mock MVC. I think the problem is with using fileUpload (...). Is there a way to check using a regular post (..) instead, as I can with apache? I would prefer to use InputStream as an argument and not use MultipartFile.
@Test public void testUpload() throws Exception { String endpoint = BASE_URL + "/upload/photo"; FileInputStream fis = new FileInputStream("/home/me/Desktop/someFolder/image.jpg"); MockMultipartFile multipartFile = new MockMultipartFile("file", fis); mockMvc.perform(fileUpload(endpoint) .file(multipartFile) .contentType(MediaType.MULTIPART_FORM_DATA)) .andExpect(status().isOk()); }
Ideally, I would like to use Spring MVC and Spring Mock MVC framework, but the code I provided just hangs with the while statement. Is what I'm doing right using the fileUpload method in a Spring test? Any advice is appreciated.