commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Schalk" <>
Subject RE: [FileUpload] File Separator question
Date Thu, 24 Jul 2003 06:34:32 GMT


I use the bit of code below to determine the actual filename of the file
being uploaded:


StringTokenizer tokenizer = new StringTokenizer(fileName, "\\, :, /");

            int amount = tokenizer.countTokens();             

            for (int i = 0; i < amount -1; i++) {



            String currentFile = tokenizer.nextToken();


I then store the folder to which I want the file  to be written as a public
String at the top of my servlet i.e.

public String serverFolder =


Then write the file to disk as follows:


File diskFile = new File(serverFolder, currentFile);






Hope this helps you out.


Kind Regards

Schalk Neethling



Tel: +27125468436

Fax: +27125468436



-----Original Message-----
From: Rhino [] 
Sent: Thursday, July 24, 2003 1:36 AM
To: commons-user
Subject: [FileUpload] File Separator question


I need some help to get my FileUpload servlet to work "cross-platform".


It works okay when uploading a file from a Windows machine to a Windows
server but it misbehaves when I try to upload a file from a Windows machine
to a Linux server; it isn't setting the file name properly.


In a Windows->Windows scenario, the file I am uploading is something like
C:\original-dir\original-subdir\my.file. My servlet uses
lastIndexOf(File.separator) to find the file name proper, e.g. my.file, and
then concatenates it to the end of the path where I want the file stored in
the server, e.g. C:\stored-dir\stored-subdir\my.file. The servlet saves the
file successfully and when I look at the C:\stored-dir\stored-subdir
directory in my file system, the file name is my.file, exactly as I want.


In a Windows->Linux scenario (i.e. uploading a Windows file to a Linux
server), the servlet stores the file in the desired directory but it has its
full original name, e.g. C:\original-dir\original-subdir\my.file, not just
my.file. That means that the full file name of the uploaded file is
something along the lines of
original-subdir\\my.file. Clearly, that is not desireable; I want the
uploaded file name to be


It seems clear to me that the file separator is the problem here. When the
servlet is running in Linux, it determines the value of file.separator to be
'/' while the file being uploaded is in Windows which has a file.separator
of '\'. The lastIndexOf(File.separator) therefore never finds the file name
proper and saves the entire path in the name, not just the last bit.


I'm not so sure what to do about it. Do I need to have my code determine the
OS from which it is getting the file which is being uploaded or is there
some other technique I need to use? How would I determine the OS from which
I'm uploading? I know that I can get the OS of the server from the Java
system property "" but how do I get the OS which is the source of the
file upload?


I am running Tomcat 4.1.24 on both the Windows XP and Linux boxes. The Linux
box is running Mandrake 9.1.


rhino1 AT sympatico DOT ca
"If you want the best seat in the house, you'll have to move the cat."

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message