This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating File object problem

 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I have tried to create a java File object for an existing file in Windows system. The JVM is running on a Sun Solaris application.
I am unable to get a File object using the below code:

where filenameUpd1 is a path like 'D:\tmp\test'.

I assumed the File object was not creating because of the backslash present and used the below code
to remove them:


I now get a string like ''D:\\tmp\\test' but still the file1.isFile() keeps returning false.

I tried and it returns a string '/'.

Anyway I could resolve the issue, so as to get a 'true' value from file1.isFile()? Or maybe I have done
something wrong. Kindly provide your help.
 
Tim Moores
Bartender
Posts: 2687
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused - is this on a Windows or a Solaris system? A "D:" file designation indicates Windows, a "/" file separator indicates Solaris.

More importantly, "new File(...)" creates a File object, not a file. Unless the file existed before you will always get false from isFile() and exists(). If you want to create a new empty file, call createNewFile().
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This code is present in a jsp file. The Weblogic is running on Sun Solaris system.
I open the web application from a Windows system and thus the Windows File path.

I need to confirm whether a File exists at a given FilePath in windows.
So I use the Windows File Path, try creating a File object, if file exists at the FilePath then file1.isFile() should return true.
Hope this provides more clarity.
 
Tim Moores
Bartender
Posts: 2687
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You fundamentally misunderstand how JSP works: JSP code running on the server does not have access to the client-side file system.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I upload a file from client side to server then?
 
Tim Moores
Bartender
Posts: 2687
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using an input element with type=file in an HTML form.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my requirement:
I need to allow browsing a file only if no file exists at a given location.
Any ideas on how I can go about it?
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tim jones wrote:This is my requirement:
I need to allow browsing a file only if no file exists at a given location.
Any ideas on how I can go about it?


Surely that's a new question? Your original question was about JSPs and file uploading and so on. This can't possibly have anything to do with that. Or can it? Perhaps you can explain in more detail.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Paul,

Well this is the scenario, the user needs to upload a file. In an earlier page, the file path is already collected.
Now before uploading to server, if the given filepath is valid, upload should happen or else browse option needs to be enabled for the user
to upload another file.

I did not know about JSP restrictions to access file systems. Seems quite logical now. Thanks to Tim to point that one out.
That was why I was trying to check if file is valid by creating a File object out of the filepath and validating it (either using exists() or isFile()).

Please help with your suggestions.

 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First suggestion: get rid of that requirement. It's nonsense anyway. The way to allow a user to upload a file has already been posted in this thread. It isn't the responsibility of the server to manage the process whereby the user chooses what file to upload, nor is it even possible for the server to do that.

It's also impossible for the user to upload a file which doesn't exist. The browser takes care of that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic