Folder names with special characters do not work session.execute IMAP - java

Here i'm trying to pass the retrieved folder to the session as string folderName and get the data of particular user. This all works for normal folder names which does not have any special characters. But if the folder name as any special characters like $,&:) etc it gives response as folder not found.
IMAPResponse[] response = session.execute(new SelectFolderCommand(folderName)).get().getResponseLines();
I need help to know are there any functions or any way we can process and select folder which as special characters

Related

Map invalid character to valid character while creating a file and back to original name when read filename

I need to map invalid characters to some other characters like "/" to "_" (forward slash to underscore) while creating a file because file name do not allowed to put slashes, question, double quotes etc.
Suppose I have
String name = "Message Test - 22/10/2016";
Now I want to write a file by using above string but it gives error because of slashes.
So I want to map slash like all the invalid characters to any other characters while writing a file. After writing, I need to read all the names of the files & show on the page.
SOMEHOW I MAP THE CHARACTERS, SO FILE NAME WOULD BE
Message_Test_-_22-10-2016
When I show it on web I need to return file name as the original name like
Message Test - 22/10/2016
I am using java. Can anyone help me out of this how can I start writing this approach or Is there any api for it or Is there any other approach.
I don't want to use database to co-related alias file name with original file name
I need to map invalid characters to some other characters like "/" to "_"
It is not enough robust since it supposes that you never use the _ character in the filename.
If you use it, how to know if a file stored as my_file should be displayed as my_file or my/file in your application.
I think that a more reliable way would be to have a file (JSON or XML for example) that stores the two properties for each file :
the stored filename
the visual name representing it in your application
It demands an additional file but it makes things really clearer.
You can use a map to store the mappings:
E.g.
Map<Character,Character> map = new HashMap<Character,Character>();
map.put('/','_');
And then replace the characters in 1 traversal:
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if( map.containsKey(c) )
str.replace(c,map.get(c));
}

How to stop URI from being encoding the reserved characters when executing?

I'm Trying to do an android app which contains an URI of a JSON file . The JSON File URI contains some reserved characters in the link and hence when executing, the link is being automatically replaced with the encoded form of those characters. therefore Im not being able to retrieve the JSON file from the link.
The link when entered in Browser shows output but through program it does not show.The link after encoding when entered in a browser show nothing but a blank page.
Sorry I Cant post the Link here...( Characters which are shown in the URI are {?,_ etc..} present in the link of the file.
How shall I COUNTER this problem?
(How to use the Escape characters to resolve this problem?)

What could be wrong with my file download method?

I have an app which allows users to upload a file. User is sent to a preview page where they can download the file they just uploaded to sorta verify that things are correct. But for some reason the filename is not correct when it reaches the servlet, what could cause this?
$('a[id^=dl_link_]').click(function(e) {
e.preventDefault();
$('#dl_form input[name=file_name]').val($(this).text());
$('#dl_form input[name=uid]').val(upload.tempId);
$('#dl_form').submit();
});
When I add logs, I see that the file name is correct; ie "this is a test file.docx".
But when this data gets to the backend I get the following:
java.io.FileNotFoundException: /Users/yao/__TEMP__/upload_temp/1111/0gGNMY8PcAWEs3M/this�is�a�test�file.docx (No such file or directory)
The file path is constructed by combining parts together. The servlet receives the uid and the filename, everything else is from some other backend methods.
What could be the cause of this?
Maybe you need to call
encodeURIComponent()
on the file name. It'll convert the space chars to %20 and then be converted properly on the back end.
As I see the problem is related with the space character. This question might help you: accessing files with spaces in filename from java

Merging two relative file URLs

Let' say I have two paths, first can look like folder/ and second like /anotherFolder/image.png. I would like to merge those two paths in some automated fashion and with option for user to omit the last slash in first string and first slash in second string. So all of these
folder/ + /anotherFolder/image.png
folder + anotherFolder/image.png
folder + /anotherFolder/image.png
should give me folder/anotherFolder/image.png
I need to merge two properties in one of my projects and I want it as dummy as possible:)So is there some trick with URL class or do I have to play around with Strings?
You can do this with java.io.File, by using the constructor which takes a File and a String as arguments, will interpret the String as a relative path to the File.
Or with java.net.URL, you can send an URL and a String to the constructur, which will interpret the URL as a context for the String parameter.
I actually used FileUtils.getFile() from Apache Commons IO but Rolf's solution was working too.

Java : How to accommodate special characters in the filename while uploading and downloading the file?

Background:
I have a file which I upload, during this process the link of the file is stored in the database and not the actual file, acutal file is stored in the File System, currently am storing it in my local machine.
Goal:
My goal is to upload a file and download a file properly which has special characters in it - #,$,%,# etc.
Issue:
I am able to upload the file with special character but am not able to download file with special characters. Also I cannot do any changes in the Download Servlet as it is part of the Framework, so all I can work with is the Upload Servlet, so my focus is to upload file with special characters in such a way so that I can download them.
I have tried creating an alias for the filename where in am replacing the special characters with '_' symbol, this approach works fine and am able to download the file but actual name of file is not maintained in here, all special characters in the filename are replaced by '_' symbol and this is not acceptable as user should actual name of the file.
Any suggestions or approach:
Code:
public ModelAndView save(HttpServletRequest request, HttpServletResponse response, Object command,
ModelAndView modelView, BindException errors) throws Exception {
String newFileName = checkForSpecialCharsAndGetNewFileName(file.getOriginalFilename());
System.out.println("alias filename="+ newFileName);
String url = "f" + (String.valueOf(System.currentTimeMillis())) + "_" + newFileName;
String fileName = file.getOriginalFilename();
System.out.println("FileName "+ fileName);
}
//Code to replace all special characters in the incoming file with '_' symbol.
private String checkForSpecialCharsAndGetNewFileName (String originalFileName) {
final String[] splChars = {"#", "+", "$"};
String newString = originalFileName;
for (int i=0; i<splChars.length; i++)
newString = StringUtils.replace(newString, splChars[i], "_");
return newString;
}
Hope am making some sense here.
Thanks.
If I am understanding you correctly, you want to encode the filename such that when you upload it, and later download it, you want to be able to find the same file from the file name.
To do this, you can use URLEncoder and URLDecoder classes.
You can do this doing something like the following:
String fileName;
fileName = URLEncoder.encode("My ! String #", "UTF-8");
That will encode it. To get the original file name:
String originalFileName = URLDecoder.decode(fileName, "UTF-8");
You can use the encoded file name to download the file from the service. You can then decode the file name to store it appropriately.
Hope that helps.
I have a problem where the java.io.File class has started auto encoding tildes that are contained in the filename thats passed in the constructor.
So for example if you instantiate a file using "~filename" it will internally interpret it as "%7Efilename" so that if you need to read or write to a file named "~filename" there is no way to do it.
The issue was introduced when I endorsed a newer set of Xalan / Xerces jars (the full set of 5) on the tomcat server. If you remove the endorsed jars the issue immediately goes away (go figure).
If that is similar to your issue, you may need to look to see if your server is using any endorsed XML parsing jars and consider removing them. I haven't figured out a way to make the newer xerces jars play well with java.io.File or even understand why there is an impact here to begin with.

Categories