How to get file seperator in gwt ?

722 views
Skip to first unread message

Rinku

unread,
Jan 29, 2009, 12:34:39 PM1/29/09
to Google Web Toolkit

How to get file seperator in gwt to because System.getProperty
("file.separator") is not working on client side ?

Lothar Kimmeringer

unread,
Jan 29, 2009, 1:50:50 PM1/29/09
to Google-We...@googlegroups.com
Rinku schrieb:

>
> How to get file seperator in gwt to because System.getProperty
> ("file.separator") is not working on client side ?

What do you want to do with this information? Maybe you can
solve the underlying problem leading to this question
differently.


Regards, Lothar

danox

unread,
Jan 29, 2009, 5:16:39 PM1/29/09
to Google Web Toolkit
Which file separator do you want to get?

If it is the client file separator, the only way I could think would
be to run a trusted java applet in the client and use javascript to
invoke a call to System.getProperty(). This would involve the user
having to agree to let the applet run and so on.

If you wanted the file separator on the server, you could always make
an Async call to a service that called System.getProperty();

I can't help feeling, though, that the fact that you want to get the
file separator in the client would suggest that what you are trying to
do with it isn't going to work.

Rinku

unread,
Jan 30, 2009, 12:20:13 AM1/30/09
to Google Web Toolkit
Thanks for your reply.
Let me explain the scenario in which i want to get the file seperator.

I am using FileUploadWidget to upload a file.
After uploading file I am adding the uploaded filename in a ListBox to
display the uploaded filename to the user.
From server side I am returning the complete path of the file where I
created uploaded file on the server. Code is given below :-

response.setContentType("text/html");
String path = filePath+System.getProperty("file.separator")+fileName;
response.getWriter().write(path);

On client side
public void onSubmitComplete(FormSubmitCompleteEvent event)
{
//--- Get the complete path of uploaded file ---
String fileUrl= event.getResults();
int idx = fileUrl.lastIndexOf("\\");
String fileName = null;

if (idx != -1)
{
//--- Get the filename from the url. ---
fileName = fileUrl.substring(idx + 1);
//--- listAttachments is a ListBox in which I
am adding filename. ---
this.listAttachments.addItem(fileName,
fileUrl);
}

}


This code is working fine on Windows Operating System but not working
on Linux.
Because Windows using "\" as a fileseperator and Linux using "/" as
fileseperator.

So what I will use to get the file seperator in the code given below,
which will work on both Windows and Linux.
int idx = fileUrl.lastIndexOf("\\");

Is there any substitue of System.getProperty("file.separator") which I
will use on client side to get the seperator, which will work on cross
platform ?

Lothar Kimmeringer

unread,
Jan 30, 2009, 4:37:57 AM1/30/09
to Google-We...@googlegroups.com
Rinku schrieb:

> I am using FileUploadWidget to upload a file.
> After uploading file I am adding the uploaded filename in a ListBox to
> display the uploaded filename to the user.
>>From server side I am returning the complete path of the file where I
> created uploaded file on the server. Code is given below :-

If you are in control of what is returned, why don't you simply
replace the system.specific file-separator by '/'. So in your
servlet you simply do a
return localPath.replace(File.separatorChar, '/');

That way you don't have to cope with the separator at all.
java.io.File takes all kinds of separators, i.e. if you create
an instance of File with "\Users\User\Documents\whatever.txt"
this will happily work independent if that happens on a Windows
or Linux or AS/400-system. The same is valid for the use of
slashes ('/') on Windows-systems.

If you want to show the path using the separators used by the
client, you might check out the User-Agent-Header on the servlet-
side to find out what operating system the client is using. On
the other side, who cares if there are backslashes or slashes?


Regards, Lothar

Rinku

unread,
Jan 30, 2009, 9:15:14 AM1/30/09
to Google Web Toolkit
Thanks Lothar.
I will try it.
Reply all
Reply to author
Forward
0 new messages