Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Windows Folders having Spaces

 
santosh batta
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My requirement is to transfer files (download/upload) from a remote windows shared drive. I'm using JCIFS API to achieve it. I'm able to connect & download files from there. The issue is when the folder name on the remote machine has spaces, my code is failing with "Failed to connect to server" error.
Below is the code snippet


When executed, I'm getting SMB exception. When tried with folders without spaces, its working absolutely fine. Would you please help me out?

jcifs.smb.SmbException: Failed to connect to server
java.net.UnknownHostException: \\HostIP\Trans-serv\1_NBWCPSUP\Test Files\My Folder-Cycle 1\hi.txt
at jcifs.UniAddress.getAllByName(UniAddress.java:315)
at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:862)
at jcifs.smb.SmbFile.connect(SmbFile.java:951)
at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
at jcifs.smb.SmbFile.open0(SmbFile.java:972)
at jcifs.smb.SmbFile.open(SmbFile.java:1006)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
at WinFileTransfer.main(WinFileTransfer.java:37)
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The unescaped blackslashes inside a String literal seem problematic to me, though the file name reported in the error message seems to be correct. Strange.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to put the entire path in quotes (escaped double quotes) to make sure windows keeps the path together as one term instead of splitting it at the spaces.
 
santosh batta
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,
Thanks for your response. I've figured it out. The problem is with the string "smb://". For some reason, its not accepting this as the correct URL format. I've modified it as below

"smb:"+"//"+hostIP+....

and it worked.

The other problem, I forget to address is the File transfer rate. It is taking approximately 2 min. to download 9MB file which I feel is very less. I've tried several ways with changing the buffer size, but it doesn't help significantly. Is there any way to improve the performance?
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santosh batta wrote:

Semi unrelated, but that line is wrong. 0 is a valid return value, and it does not indicate you're done. Change it into != -1, > -1 or >= 0.
 
santosh batta
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
When I change the condition to >=0 or != -1 , the performance still got worse. Previously it was taking 2 min. to download 9MB file & now its taking 5 min.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santosh batta wrote:Hi Rob,
When I change the condition to >=0 or != -1 , the performance still got worse. Previously it was taking 2 min. to download 9MB file & now its taking 5 min.


Most likely it was taking 2 min. to download part of a 9MB file, until read() returned 0. Even if you got the whole file that time, the code is wrong and isn't guaranteed to work.

If performance is a problem, you might try wrapping your InputStream in a BufferedInputStream, if you're not already doing so.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic