Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Identify text vs. binary content in InputStream  RSS feed

 
Brian W Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a webapp where I receive a file upload. Utilizing O'Reilly multipart tools to grab the file as follows:



The idea is that the file is a text file, and the code goes on to manipulate that text. But, I would like to make sure it is a text file before I do any other work. What's the easiest way to verify if it is text or non-text?

Thanks.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try using Commons FileUpload instead.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian W Johnson wrote:What's the easiest way to verify if it is text or non-text?


Probably the easiest thing to do is to send your binary files to one context and your text files to another. I don't think there's a reliable way to differentiate between the two because 'text' files are binary files that conform to a particular charset.
 
Brian W Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Ess wrote:Probably the easiest thing to do is to send your binary files to one context and your text files to another.


Can you expand on this?

Joe Ess wrote:I don't think there's a reliable way to differentiate between the two because 'text' files are binary files that conform to a particular charset.


I had considered grabbing just the first 10 characters or so of the file and checking if they were within an acceptable range of characters. Wouldn't weed out all non-text files, but I thought it might catch most... which is better than I'm doing now. Course, I wasn't sure what the best function to accomplish this would be.

Thanks!
 
Brian W Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You could try using Commons FileUpload instead.


I just took a quick glance at that link... is there something in particular in that package that would be helpful for my situation?

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!