problems getting just the file name using an upload component

Hi everyone, I'm using an upload component on my page and I need to get just the name of the image/file in order to store it in a database, I used the code of the tutorial and It works fine on mozilla but the problem comes when I use Internet Explorer because I get the whole path of the image/file and not just the name of it. I believe the browser is the cause of the problem, I already checked the http://jakarta.apache.org/commons/fileupload/

, but I don't understand how to use anything, and not even the code of the tutorial works eventhough it is specified in comments:

// some browsers return complete path name, some don't

// make sure we only have the file name

String justFileName = uploadedFileName.substring

( uploadedFileName.lastIndexOf(File.separatorChar) + 1 );

If anybody could helpme solve this problem I'd appreciate it so much

Regards

Yes

Message was edited by:

Yesenia

[1056 byte] By [Yesenia] at [2007-11-26 8:40:33]
# 1

Hi Yesenia,

So, what problem have you got with tutorial code?

UploadedFile uploadedFile = fileUpload1.getUploadedFile();

String uploadedFileName = uploadedFile.getOriginalName();

// some browsers return complete path name, some don't

// make sure we only have the file name

String justFileName = uploadedFileName.substring

( uploadedFileName.lastIndexOf(File.separatorChar) + 1 );

After this, justFileName variable definitely contains name of file without path, and it's browser-independent.

Thanks, Misha

(Creator team)

Mikhail_Matveev at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 2

> but I don't understand how to use anything, and not

> even the code of the tutorial works eventhough it is

> specified in comments:

In addition to what Mikhael says...

Can you explain further what you mean by "not even the code of the tutorial works"

Can you tell us what the value of justFileName is after this statement:

String justFileName = uploadedFileName.substring

( uploadedFileName.lastIndexOf(File.separatorChar) + 1 );

How is the code not working?

jetsons at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 3

Hi Misha and Jetson

I followed the tutorial: "Using the File Upload Component" I'm working on solaris using mozilla as web broser, and the application works just fine but when I tried it on another machine with windows xp and internet explorer 6, the sentence

String justFileName = uploadedFileName.substring

( uploadedFileName.lastIndexOf(File.separatorChar) + 1 );

this.fileNameStaticText.setValue(justFileName);

returns the complete path name for example: C:\Documents and Settings\All Users\Documentos\Mis im谩genes\Im谩genes de muestra\Puesta de sol.jpg

Due to the requirements of the application, I only need to get the name of the image but I don't know for what reason it doesn't work for internet explorer, I probably need to add something else when working with ie...

thank you so much for your help

yesenia

Yesenia at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 4

Let's walk through the code...

File.separatorChar is the system-dependent default hame-separator charac tor. This field is initialized to contain the first character of the system property file.separator. On UNIX systems, the value of this field is '/'; on Microsoft Windows systems it is '\\'.

String justFileName =

// get the substring of uploadedFileName

// that starts after the last separator (\ or /)

uploadedFileName.substring

(

uploadedFileName.lastIndexOf(File.separatorChar) + 1

);

You say that this statement is returning the complete path name, for example:

C:\Documents and Settings\All

Users\Documentos\Mis im谩genes\Im谩genes de

muestra\Puesta de sol.jpg

So, we can make an assumption that the File.separatorChar must not be \, or else the statement would return Puesta de sol.jpg.

We know that it works for you on Solaris/Mozilla.

We also know that Misha has tested the statement in IE and it works for him.

(And I know that this tutorial was tested by a software engineer and a quality engineer, and it worked for both of them, as well as the tutorial author and a peer).

So, what is different here?

Can you print out File.separatorChar and see if it is '\\' (I think the first \ is an escape char, that is \\ (escape-escape) is the equivalent of \ (backslash)) or '\'?

When you run it on another machine, how are you deploying it on the other machine? Is it deployed to Solaris and running on Windows?

Have you tested to see if it works on a Windows machine using Mozilla?

Thanks in advance for any light you can shed on the problem.

jetsons at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 5

Hi Yesenia,

This is surely not a best way, but your problem can be solved this way:

String justFileName = uploadedFileName.substring

( uploadedFileName.lastIndexOf(File.separatorChar) + 1 );

justFileName = justFileName.substring

( justFileName.lastIndexOf("\\") + 1 );

this.fileNameStaticText.setValue(justFileName);

Thanks, Misha

(Creator team)

Mikhail_Matveev at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 6
Hi Jetsons, Yesenia,I just printed separatorChar on WinXP. It's '\'.Also I tested Yesenia's code on WinXP. It works correctly, justFileName value is "Puesta de sol.jpg" without path.Thanks, Misha(Creator team)
Mikhail_Matveev at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 7

Hi jetsons and Misha

Thank you so much for your help I tried Misha's code and it worked because now it only returns the name of the image and not the complete path. So this is my final code:

String justFileName = uploadedFileName.substring

( uploadedFileName.lastIndexOf(File.separatorChar) + 1 );

justFileName = justFileName.substring

(justFileName.lastIndexOf("\\") + 1 );

this.fileNameStaticText.setValue(justFileName);

Now it does work on mozilla, explorer and I tried it on safari too

Thank you both for taking the time to help me =)

yesenia.

Message was edited by:

Yesenia

Message was edited by:

Yesenia

null

Yesenia at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 8

Yesenia,

Can you tell me if your fix works if the user is browsing from a UNIX or Solaris box?

I asked some questions earlier. I am reposting below. We would like to make the tutorial work for everyone. Your answers can help us fix the tutorial.

Can you print out File.separatorChar and see if it is '\\' (I think the first \ is an escape char, that is \\ (escape-escape) is the equivalent of \ (backslash)) or '\'?

When you run it on another machine, how are you deploying it on the other machine? Is it deployed to Solaris and running on Windows?

Have you tested the original code to see if it works on a Windows machine using Mozilla?

jetsons at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 9

Hi Jetsons!

I'm developing the application on solaris and using mozilla as web browser and it works fine, then I run it on windows using internet explorer and mac using safari both deployed on solaris and they work perfectly too.

The initial problem was that I was not getting JUST the name of the image on explorer(windows), and Misha's code helped me achieve that.

Now If I print the File.separatorChar as you said, it prints ":" on three browsers, thoug I'm not sure If I did get crearly what is it that you wanted me to try, but I just printed it the following way:

this.separatorStaticText.setValue(File.pathSeparator);

sorry 'cause I took too long to answer, I hope you understand my answers and just in case you have another questions or my answers are not clear, let me know =)

Yesenia

Yesenia at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 10
Hi Yesenia, Chris,Actually we speak about File.separatorChar, not File.pathSeparator. And its value is '\', both for Internet Explorer and Mozilla Firefox.Thanks, Misha(Creator team)
Mikhail_Matveev at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 11

Hello,

Yesenia. Just so you know, Misha and I have 2 different goals. Misha is a Sun support person who is helping to solve your problem, which he has nicely done.

I am one of the tutorial writers who is concerned that we need to add some notes to the tutorial to make it work for everyone. So, I am asking you to help me find out why the instructions, which are supposed to work for everyone are not working for you.

I know that for Misha, the program is working as intended, so I do not need Misha to run the experiments, just you. Your experiment was good, but as Misha says, you need to do this:

this.separatorStaticText.setValue(File.separatorChar);

When you say that you run it on windows, are you running the app server AND browser on Windows, or are you running the app server on Solaris and the Browser on windows.

If you are running the application in an app server on windows, how did you deploy the app to the app server? Did you build a WAR file and deploy that WAR file?

jetsons at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 12

Hi

Yes, you guys are right, I did a mistake, I posted it incorrectly in the forum, the file.separatorchar was "/" using this code: this.separatorStaticText.setValue(File.separatorChar);

Jetsons, I have installed creator and app server on solaris and using mozilla as web browser. I still haven't created any war file for this application so I access the application through the intranet using two machines (for example: http://193.10.6.20:29080/WebApplication) one with windows xp and internet explorer and the other one with mac os and safari as web browser, the app server is always running on solaris.

If you have any other question let me know

Thank you

Yesenia =)

Message was edited by:

Yesenia

Yesenia at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 13

Hi Yesenia,

Thanks for taking the time to answer my questions. You have provided the information that I need to try and hunt down an answer. The problem that you are having is that your browser is on Windows, and your server is on Solaris. The app is running on Solaris which reports that the file separator is /. However, your browser is on Windows and is providing a path that uses / as the separator.

We need to come up with better code in the tutorial that will work for this situation. Hopefully, we will find a solution.

Thanks again!

Chris

jetsons at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...
# 14

Thought you might want to know that we will change the code in the tutorial to the following:

UploadedFile uploadedFile = fileUpload1.getUploadedFile();

String uploadedFileName = uploadedFile.getOriginalName();

// some browsers return complete path name, some don't

// make sure we only have the file name

// Try forward slash

int index = uploadedFileName.lastIndexOf('/');

if ( index >= 0) {

justFileName = uploadedFileName.substring( index + 1 );

} else {

// Try backslash

index = uploadedFileName.lastIndexOf('\\');

if (index >= 0) {

justFileName = uploadedFileName.substring( index + 1 );

} else {

// No forward or back slashes

justFileName = uploadedFileName;

}

jetsons at 2007-7-6 22:17:22 > top of Java-index,Development Tools,Java Tools...