• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encoding file name with java  RSS feed

 
Darvesh Niz
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
i have some basic problem with encoding filename i have a file with space in its name. the file name is test this.txt
here isthe code which i am using to encode the file name, when i use encode url it does not save at all any idea why


The issue is that when i run the sample program its not creating the file with the
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "fileUrl" contain after you run the encode command?
 
Darvesh Niz
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I i print fileURL in the console it gets printed as

C%3A%5Cnfs%5Ccommon%5Ctemp%5Ctest+this.txt

which is encoded characters but somehow when i use ecode url it doesnot create the file, wehn i dont encode the url it works

Any advice
Rashid
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked for a file named "C%3A%5Cnfs%5Ccommon%5Ctemp%5Ctest+this.txt" in your current working directory? I expect you are still looking in the C:/nfs/common/temp directory and not finding anything. This would be normal, because the file name you produced isn't a path any more.

I'm curious why you thought URL-encoding a file path would be a good idea.
 
Darvesh Niz
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
i tried the below code where i only encode the file name and it works, the whole issue I am trying to encode file names is because we are having users who are uploading file with some special characters like +, & and spaces in the file names. Dont know whats the best way to encode this in file names since we dont want to have any issues when storing them to file system.
Let me know if this is the correct approach.
public void test(){
try{
String fileDir = "C:\\nfs\\common\\temp";
String fileName= "temp test.txt";
fileName = URLEncoder.encode(fileName,"UTF-8");
File file = new File(fileDir,fileName);
file.createNewFile();
System.out.println("crated");
}catch(Exception e){
e.printStackTrace();
}


Thanks
Rashid
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you would certainly need to URL-encode the file name if you are planning to pass it as the value of an HTTP parameter. But I don't see the point of trying to create a new file whose name is the URL-encoded form of the original file's name. Why not just let the users create files with whatever names they like, then just URL-encode the name where it's necessary?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!