Index: media.php
===================================================================
--- media.php	(revision 16973)
+++ media.php	(working copy)
@@ -145,7 +145,7 @@
 
 	// try for a new style intermediate size
 	if ( $intermediate = image_get_intermediate_size($id, $size) ) {
-		$img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
+		$img_url = str_replace($img_url_basename, urlencode( $intermediate['file'] ), $img_url);
 		$width = $intermediate['width'];
 		$height = $intermediate['height'];
 		$is_intermediate = true;
@@ -153,7 +153,7 @@
 	elseif ( $size == 'thumbnail' ) {
 		// fall back to the old thumbnail
 		if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) {
-			$img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url);
+			$img_url = str_replace($img_url_basename, urlencode( wp_basename($thumb_file) ), $img_url);
 			$width = $info[0];
 			$height = $info[1];
 			$is_intermediate = true;
Index: post.php
===================================================================
--- post.php	(revision 16973)
+++ post.php	(working copy)
@@ -3809,18 +3809,17 @@
 		return false;
 
 	$url = '';
-	if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { //Get attached file
-		if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory
-			if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
-				$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
-			elseif ( false !== strpos($file, 'wp-content/uploads') )
-				$url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
-			else
-				$url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir.
+	if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true ) ) { // Get attached file
+		if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
+			if ( 0 === strpos( $file, $uploads['basedir'] ) ) // Check that the upload base exists in the file location
+				$file = substr( $file, strlen( $uploads['basedir'] ) + 1 );
+			elseif ( false !== strpos( $file, 'wp-content/uploads' ) )
+				$file = substr( $file, strpos($file, 'wp-content/uploads' ) + 19 );
+			$url = $uploads['baseurl'] . '/' . implode( '/', urlencode_deep( explode( '/', $file ) ) );
 		}
 	}
 
-	if ( empty($url) ) //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recomended to rely upon this.
+	if ( empty($url) ) // If any of the above options failed, Fallback on the GUID as used pre-2.7, not recomended to rely upon this.
 		$url = get_the_guid( $post->ID );
 
 	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
