FileUploadUtil { public static File copyUploadedFile( HttpServletRequest request, String key ) throws IOException { MultipartHttpServletRequest multipartRequest = ( MultipartHttpServletRequest ) request; MultipartFile file = multipartRequest.getFile( key ); if ( file == null ) { throw new IllegalArgumentException( "File with key " + key + " was not in the request" ); } return copyUploadedFile( file, request ); }  static File copyUploadedFile( HttpServletRequest request, String key ); static File copyUploadedFile( MultipartFile multipartFile, HttpServletRequest request ); static File copyUploadedInputStream( InputStream is ); static String getContextUploadPath(); static String getUploadPath();  }
@Test public void testCopyUploadedFileStreamClosed() throws FileNotFoundException, IOException { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); request.setMethod( "POST" ); String text_contents = "test"; String key = "file1"; String filename = "test_upload.txt"; MockMultipartFile sourceFile = new MockMultipartFile( key, filename, "text/plain", text_contents.getBytes() ); long expectedSize = sourceFile.getSize(); request.setContent( text_contents.getBytes() ); request.addFile( sourceFile ); File copiedFile = FileUploadUtil.copyUploadedFile( request, key ); assert ( copiedFile.exists() ); assertEquals( expectedSize, copiedFile.length() ); copiedFile.delete(); }