Re: file name is not correct

Sebastian Sickelmann <[email protected]>
Newsgroups gmane.network.samba.java
Message-ID <[email protected]>
Am 14.08.2011 05:19, schrieb Michael B Allen:
> 2011/8/13<[email protected]>:
>> Hi, Mike,
>>
>>> Try the URL escape sequence for space which is %20 like:
>> If I create SmbFile object from file name, It is OK.
>> But I want to create SmbFile object from directory.
>> 1. SmbFile dir = SmbFile("smb://host/dir/", auth);
>> 2. SmbFile[] fileList = dir.listFiles();
>> 3. execute getName, getUncPath ant getPath for each file got from fileList.
>> 4. then, I receive file name without space.
> Hi Dora,
>
> I believe this is occurring because of the java.net.URL class which is
> used internally to manipulate URLs. So it cannot handle some special
> characters in special instances. Meaning the java.net.URL class is
> trimming leading spaces from the filename. Unfortunately unless we
> re-write JCIFS to not use the java.net.URL class, there is not a whole
> lot we can do about this.
>
> Mike
>
Hi,

Because the URL is following the recommendations in RFC2396 it tolerates 
spaces in urls and removes them. If i understand the RFC correct it is 
to make it easier for people to type in urls by hand.
Prefixing "URL:" solves it in at least for the jdk interpretation of 
rfc2396.

So the attached patch fix it for me.

-- Sebastian
rfc2396.patch (text/x-patch, 3.8 KB)
diff --git a/jcifs/jcifs/smb/SmbFile.java b/jcifs/jcifs/smb/SmbFile.java
index 22391e9..00a1da3 100644
--- a/jcifs/jcifs/smb/SmbFile.java
+++ b/jcifs/jcifs/smb/SmbFile.java
@@ -268,6 +268,7 @@ import java.util.Date;
 
 public class SmbFile extends URLConnection implements SmbConstants {
 
+    static final String RFC_2396_DISTINGUISHING_PREFIX = "URL:";
     static final int O_RDONLY = 0x01;
     static final int O_WRONLY = 0x02;
     static final int O_RDWR   = 0x03;
@@ -465,7 +466,7 @@ public class SmbFile extends URLConnection implements SmbConstants {
                 throws MalformedURLException, UnknownHostException {
         this( context.isWorkgroup0() ?
             new URL( null, "smb://" + name, Handler.SMB_HANDLER ) :
-            new URL( context.url, name, Handler.SMB_HANDLER ), context.auth );
+            new URL( context.url, RFC_2396_DISTINGUISHING_PREFIX+name, Handler.SMB_HANDLER ), context.auth );
     }
 
 /**
@@ -483,7 +484,7 @@ public class SmbFile extends URLConnection implements SmbConstants {
 
     public SmbFile( String context, String name ) throws MalformedURLException {
         this( new URL( new URL( null, context, Handler.SMB_HANDLER ),
-                name, Handler.SMB_HANDLER ));
+        		RFC_2396_DISTINGUISHING_PREFIX+name, Handler.SMB_HANDLER ));
     }
 
 /**
@@ -536,7 +537,7 @@ public class SmbFile extends URLConnection implements SmbConstants {
  */
     public SmbFile( String context, String name, NtlmPasswordAuthentication auth )
                     throws MalformedURLException {
-        this( new URL( new URL( null, context, Handler.SMB_HANDLER ), name, Handler.SMB_HANDLER ), auth );
+        this( new URL( new URL( null, context, Handler.SMB_HANDLER ), RFC_2396_DISTINGUISHING_PREFIX+name, Handler.SMB_HANDLER ), auth );
     }
 /**
  * Constructs an SmbFile representing a resource on an SMB network such
@@ -559,7 +560,7 @@ public class SmbFile extends URLConnection implements SmbConstants {
  */
     public SmbFile( String context, String name, NtlmPasswordAuthentication auth, int shareAccess )
                     throws MalformedURLException {
-        this( new URL( new URL( null, context, Handler.SMB_HANDLER ), name, Handler.SMB_HANDLER ), auth );
+        this( new URL( new URL( null, context, Handler.SMB_HANDLER ), RFC_2396_DISTINGUISHING_PREFIX + name, Handler.SMB_HANDLER ), auth );
         if ((shareAccess & ~(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)) != 0) {
             throw new RuntimeException( "Illegal shareAccess parameter" );
         }
@@ -587,7 +588,7 @@ public class SmbFile extends URLConnection implements SmbConstants {
                     throws MalformedURLException, UnknownHostException {
         this( context.isWorkgroup0() ?
             new URL( null, "smb://" + name, Handler.SMB_HANDLER ) :
-            new URL( context.url, name, Handler.SMB_HANDLER ), context.auth );
+            new URL( context.url, RFC_2396_DISTINGUISHING_PREFIX + name, Handler.SMB_HANDLER ), context.auth );
         if ((shareAccess & ~(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)) != 0) {
             throw new RuntimeException( "Illegal shareAccess parameter" );
         }
@@ -621,7 +622,7 @@ public class SmbFile extends URLConnection implements SmbConstants {
                 throws MalformedURLException, UnknownHostException {
         this( context.isWorkgroup0() ?
             new URL( null, "smb://" + name + "/", Handler.SMB_HANDLER ) :
-            new URL( context.url, name + (( attributes & ATTR_DIRECTORY ) > 0 ? "/" : "" )));
+            new URL( context.url, RFC_2396_DISTINGUISHING_PREFIX +name + (( attributes & ATTR_DIRECTORY ) > 0 ? "/" : "" )));
 
         /* why was this removed before? DFS? copyTo? Am I going around in circles? */
         auth = context.auth;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.