This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Is indexof only way of identify in file type  RSS feed

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All
While making a fileinputstream in case we have to check for file type for enforcing validation rule. Is there any better way except doing an indexOf on File complete path.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use the endsWith() method instead, to avoid the possibility that a file extension might also occur elsewhere in the path.
 
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
I would use the endsWith() method instead, to avoid the possibility that a file extension might also occur elsewhere in the path.



I would do a getName() on file object and then endsWith()
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are wanting to check whether a given file is of the right content type, you may want to look at URLConnection.guessContentTypeFromName() and URLConnection.guessContentTypeFromStream().

Note you can make a URL from a File.
 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you may want to look at URLConnection.guessContentTypeFromName() and URLConnection.guessContentTypeFromStream().

It seems to be a good solution but is it possible to get URLConnection somehow from request object as I must be doing this check in struts kind of framework where we only have request object to get most of the input
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
URLConnection.guessContentTypeFromName() and -Stream() are static methods, so you don't need an instance. You should be able to call these anywhere.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can call them without an instance of URLConnection. My code does it: -

 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!