How do I know if a File type is PDF?
up vote
4
down vote
favorite
This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not
Using complete library for this use looks like overkill
- Are there any ways to know that a Java File is of type PDF?
java
add a comment |
up vote
4
down vote
favorite
This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not
Using complete library for this use looks like overkill
- Are there any ways to know that a Java File is of type PDF?
java
3
Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
– peshkira
Nov 8 '12 at 20:14
Related/duplicate: stackoverflow.com/questions/1915317/…
– Tomasz Nurkiewicz
Nov 8 '12 at 20:14
Try having a look at stackoverflow.com/questions/51438/…
– MadProgrammer
Nov 8 '12 at 20:18
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not
Using complete library for this use looks like overkill
- Are there any ways to know that a Java File is of type PDF?
java
This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not
Using complete library for this use looks like overkill
- Are there any ways to know that a Java File is of type PDF?
java
java
edited May 23 '17 at 10:34
Community♦
11
11
asked Nov 8 '12 at 20:09
daydreamer
30.2k124336562
30.2k124336562
3
Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
– peshkira
Nov 8 '12 at 20:14
Related/duplicate: stackoverflow.com/questions/1915317/…
– Tomasz Nurkiewicz
Nov 8 '12 at 20:14
Try having a look at stackoverflow.com/questions/51438/…
– MadProgrammer
Nov 8 '12 at 20:18
add a comment |
3
Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
– peshkira
Nov 8 '12 at 20:14
Related/duplicate: stackoverflow.com/questions/1915317/…
– Tomasz Nurkiewicz
Nov 8 '12 at 20:14
Try having a look at stackoverflow.com/questions/51438/…
– MadProgrammer
Nov 8 '12 at 20:18
3
3
Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
– peshkira
Nov 8 '12 at 20:14
Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
– peshkira
Nov 8 '12 at 20:14
Related/duplicate: stackoverflow.com/questions/1915317/…
– Tomasz Nurkiewicz
Nov 8 '12 at 20:14
Related/duplicate: stackoverflow.com/questions/1915317/…
– Tomasz Nurkiewicz
Nov 8 '12 at 20:14
Try having a look at stackoverflow.com/questions/51438/…
– MadProgrammer
Nov 8 '12 at 20:18
Try having a look at stackoverflow.com/questions/51438/…
– MadProgrammer
Nov 8 '12 at 20:18
add a comment |
6 Answers
6
active
oldest
votes
up vote
11
down vote
accepted
Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.
Just opened a PDF in notepad++, and it indeed does. +1
– Sam I am
Nov 8 '12 at 20:16
Yeah, I had a use case similar and Wikipedia was very helpful
– ElderMael
Nov 8 '12 at 20:17
but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
– Sam I am
Nov 8 '12 at 20:17
2
@SamIam - Sounds like another argument in favor of using a library.
– jahroy
Nov 8 '12 at 20:21
2
Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
– peshkira
Nov 8 '12 at 20:22
add a comment |
up vote
2
down vote
Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:
import javax.activation.*;
public class ShowMimeType
{
public static void main(String args) {
FileDataSource ds = new FileDataSource(args[0]);
String contentType = ds.getContentType();
System.out.println("The MIME type of the file " + args[0] + " is: " + contentType);
}
}
add a comment |
up vote
2
down vote
SimpleMagic is a Java library for resolving content types:
<!-- pom.xml -->
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.8</version>
</dependency>
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.j256.simplemagic.ContentType;
// ...
public class SimpleMagicSmokeTest {
private final static Logger log = LoggerFactory.getLogger(SimpleMagicSmokeTest.class);
@Test
public void smokeTestSimpleMagic() throws IOException {
ContentInfoUtil util = new ContentInfoUtil();
File possiblePdfFile = new File("/path/to/possiblePdfFile.pdf");
ContentInfo info = util.findMatch(possiblePdfFile);
log.info( info.toString() );
assertEquals( ContentType.PDF, info.getContentType() );
}
add a comment |
up vote
1
down vote
If checking the file extension is not satisfactory, you coudl try checking the files magic number by reading a few bytes of the file
PDF files start with "%PDF" (hex 25 50 44 46).
add a comment |
up vote
0
down vote
This might sound a little bit too obvious, but check the extension on the filename.
If it's good enough for explorer, it should be good enough for you
2
an extension does not say anything about a format.
– peshkira
Nov 8 '12 at 20:11
@peshkira well, it's supposed to. Only rarely you can't trust it.
– John Dvorak
Nov 8 '12 at 20:14
1
on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
– peshkira
Nov 8 '12 at 20:17
2
I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
– jahroy
Nov 8 '12 at 20:19
add a comment |
up vote
0
down vote
Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier AutoDetectParser.
if(currentImageType ==null){
ByteArrayInputStream is = new ByteArrayInputStream(image);
String mimeType = URLConnection.guessContentTypeFromStream(is);
if(mimeType == null){
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
mimeType = detector.detect(is,md).toString();
if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
}
if(mimeType.contains("png")){
mimeType ="png";
}
else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
mimeType = "jpg";
}
else if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
currentImageType = ImageType.fromValue(mimeType);
}
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.
Just opened a PDF in notepad++, and it indeed does. +1
– Sam I am
Nov 8 '12 at 20:16
Yeah, I had a use case similar and Wikipedia was very helpful
– ElderMael
Nov 8 '12 at 20:17
but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
– Sam I am
Nov 8 '12 at 20:17
2
@SamIam - Sounds like another argument in favor of using a library.
– jahroy
Nov 8 '12 at 20:21
2
Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
– peshkira
Nov 8 '12 at 20:22
add a comment |
up vote
11
down vote
accepted
Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.
Just opened a PDF in notepad++, and it indeed does. +1
– Sam I am
Nov 8 '12 at 20:16
Yeah, I had a use case similar and Wikipedia was very helpful
– ElderMael
Nov 8 '12 at 20:17
but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
– Sam I am
Nov 8 '12 at 20:17
2
@SamIam - Sounds like another argument in favor of using a library.
– jahroy
Nov 8 '12 at 20:21
2
Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
– peshkira
Nov 8 '12 at 20:22
add a comment |
up vote
11
down vote
accepted
up vote
11
down vote
accepted
Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.
Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.
answered Nov 8 '12 at 20:13
ElderMael
5,47942450
5,47942450
Just opened a PDF in notepad++, and it indeed does. +1
– Sam I am
Nov 8 '12 at 20:16
Yeah, I had a use case similar and Wikipedia was very helpful
– ElderMael
Nov 8 '12 at 20:17
but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
– Sam I am
Nov 8 '12 at 20:17
2
@SamIam - Sounds like another argument in favor of using a library.
– jahroy
Nov 8 '12 at 20:21
2
Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
– peshkira
Nov 8 '12 at 20:22
add a comment |
Just opened a PDF in notepad++, and it indeed does. +1
– Sam I am
Nov 8 '12 at 20:16
Yeah, I had a use case similar and Wikipedia was very helpful
– ElderMael
Nov 8 '12 at 20:17
but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
– Sam I am
Nov 8 '12 at 20:17
2
@SamIam - Sounds like another argument in favor of using a library.
– jahroy
Nov 8 '12 at 20:21
2
Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
– peshkira
Nov 8 '12 at 20:22
Just opened a PDF in notepad++, and it indeed does. +1
– Sam I am
Nov 8 '12 at 20:16
Just opened a PDF in notepad++, and it indeed does. +1
– Sam I am
Nov 8 '12 at 20:16
Yeah, I had a use case similar and Wikipedia was very helpful
– ElderMael
Nov 8 '12 at 20:17
Yeah, I had a use case similar and Wikipedia was very helpful
– ElderMael
Nov 8 '12 at 20:17
but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
– Sam I am
Nov 8 '12 at 20:17
but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
– Sam I am
Nov 8 '12 at 20:17
2
2
@SamIam - Sounds like another argument in favor of using a library.
– jahroy
Nov 8 '12 at 20:21
@SamIam - Sounds like another argument in favor of using a library.
– jahroy
Nov 8 '12 at 20:21
2
2
Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
– peshkira
Nov 8 '12 at 20:22
Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
– peshkira
Nov 8 '12 at 20:22
add a comment |
up vote
2
down vote
Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:
import javax.activation.*;
public class ShowMimeType
{
public static void main(String args) {
FileDataSource ds = new FileDataSource(args[0]);
String contentType = ds.getContentType();
System.out.println("The MIME type of the file " + args[0] + " is: " + contentType);
}
}
add a comment |
up vote
2
down vote
Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:
import javax.activation.*;
public class ShowMimeType
{
public static void main(String args) {
FileDataSource ds = new FileDataSource(args[0]);
String contentType = ds.getContentType();
System.out.println("The MIME type of the file " + args[0] + " is: " + contentType);
}
}
add a comment |
up vote
2
down vote
up vote
2
down vote
Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:
import javax.activation.*;
public class ShowMimeType
{
public static void main(String args) {
FileDataSource ds = new FileDataSource(args[0]);
String contentType = ds.getContentType();
System.out.println("The MIME type of the file " + args[0] + " is: " + contentType);
}
}
Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:
import javax.activation.*;
public class ShowMimeType
{
public static void main(String args) {
FileDataSource ds = new FileDataSource(args[0]);
String contentType = ds.getContentType();
System.out.println("The MIME type of the file " + args[0] + " is: " + contentType);
}
}
answered Nov 8 '12 at 20:11
awolfe91
1,5071710
1,5071710
add a comment |
add a comment |
up vote
2
down vote
SimpleMagic is a Java library for resolving content types:
<!-- pom.xml -->
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.8</version>
</dependency>
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.j256.simplemagic.ContentType;
// ...
public class SimpleMagicSmokeTest {
private final static Logger log = LoggerFactory.getLogger(SimpleMagicSmokeTest.class);
@Test
public void smokeTestSimpleMagic() throws IOException {
ContentInfoUtil util = new ContentInfoUtil();
File possiblePdfFile = new File("/path/to/possiblePdfFile.pdf");
ContentInfo info = util.findMatch(possiblePdfFile);
log.info( info.toString() );
assertEquals( ContentType.PDF, info.getContentType() );
}
add a comment |
up vote
2
down vote
SimpleMagic is a Java library for resolving content types:
<!-- pom.xml -->
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.8</version>
</dependency>
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.j256.simplemagic.ContentType;
// ...
public class SimpleMagicSmokeTest {
private final static Logger log = LoggerFactory.getLogger(SimpleMagicSmokeTest.class);
@Test
public void smokeTestSimpleMagic() throws IOException {
ContentInfoUtil util = new ContentInfoUtil();
File possiblePdfFile = new File("/path/to/possiblePdfFile.pdf");
ContentInfo info = util.findMatch(possiblePdfFile);
log.info( info.toString() );
assertEquals( ContentType.PDF, info.getContentType() );
}
add a comment |
up vote
2
down vote
up vote
2
down vote
SimpleMagic is a Java library for resolving content types:
<!-- pom.xml -->
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.8</version>
</dependency>
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.j256.simplemagic.ContentType;
// ...
public class SimpleMagicSmokeTest {
private final static Logger log = LoggerFactory.getLogger(SimpleMagicSmokeTest.class);
@Test
public void smokeTestSimpleMagic() throws IOException {
ContentInfoUtil util = new ContentInfoUtil();
File possiblePdfFile = new File("/path/to/possiblePdfFile.pdf");
ContentInfo info = util.findMatch(possiblePdfFile);
log.info( info.toString() );
assertEquals( ContentType.PDF, info.getContentType() );
}
SimpleMagic is a Java library for resolving content types:
<!-- pom.xml -->
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.8</version>
</dependency>
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.j256.simplemagic.ContentType;
// ...
public class SimpleMagicSmokeTest {
private final static Logger log = LoggerFactory.getLogger(SimpleMagicSmokeTest.class);
@Test
public void smokeTestSimpleMagic() throws IOException {
ContentInfoUtil util = new ContentInfoUtil();
File possiblePdfFile = new File("/path/to/possiblePdfFile.pdf");
ContentInfo info = util.findMatch(possiblePdfFile);
log.info( info.toString() );
assertEquals( ContentType.PDF, info.getContentType() );
}
answered Sep 28 '16 at 16:55
Abdull
14.3k1486135
14.3k1486135
add a comment |
add a comment |
up vote
1
down vote
If checking the file extension is not satisfactory, you coudl try checking the files magic number by reading a few bytes of the file
PDF files start with "%PDF" (hex 25 50 44 46).
add a comment |
up vote
1
down vote
If checking the file extension is not satisfactory, you coudl try checking the files magic number by reading a few bytes of the file
PDF files start with "%PDF" (hex 25 50 44 46).
add a comment |
up vote
1
down vote
up vote
1
down vote
If checking the file extension is not satisfactory, you coudl try checking the files magic number by reading a few bytes of the file
PDF files start with "%PDF" (hex 25 50 44 46).
If checking the file extension is not satisfactory, you coudl try checking the files magic number by reading a few bytes of the file
PDF files start with "%PDF" (hex 25 50 44 46).
answered Nov 8 '12 at 20:14
case1352
1,10511221
1,10511221
add a comment |
add a comment |
up vote
0
down vote
This might sound a little bit too obvious, but check the extension on the filename.
If it's good enough for explorer, it should be good enough for you
2
an extension does not say anything about a format.
– peshkira
Nov 8 '12 at 20:11
@peshkira well, it's supposed to. Only rarely you can't trust it.
– John Dvorak
Nov 8 '12 at 20:14
1
on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
– peshkira
Nov 8 '12 at 20:17
2
I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
– jahroy
Nov 8 '12 at 20:19
add a comment |
up vote
0
down vote
This might sound a little bit too obvious, but check the extension on the filename.
If it's good enough for explorer, it should be good enough for you
2
an extension does not say anything about a format.
– peshkira
Nov 8 '12 at 20:11
@peshkira well, it's supposed to. Only rarely you can't trust it.
– John Dvorak
Nov 8 '12 at 20:14
1
on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
– peshkira
Nov 8 '12 at 20:17
2
I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
– jahroy
Nov 8 '12 at 20:19
add a comment |
up vote
0
down vote
up vote
0
down vote
This might sound a little bit too obvious, but check the extension on the filename.
If it's good enough for explorer, it should be good enough for you
This might sound a little bit too obvious, but check the extension on the filename.
If it's good enough for explorer, it should be good enough for you
edited Nov 8 '12 at 20:19
Jacob Schoen
9,833156695
9,833156695
answered Nov 8 '12 at 20:10
Sam I am
26.6k105791
26.6k105791
2
an extension does not say anything about a format.
– peshkira
Nov 8 '12 at 20:11
@peshkira well, it's supposed to. Only rarely you can't trust it.
– John Dvorak
Nov 8 '12 at 20:14
1
on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
– peshkira
Nov 8 '12 at 20:17
2
I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
– jahroy
Nov 8 '12 at 20:19
add a comment |
2
an extension does not say anything about a format.
– peshkira
Nov 8 '12 at 20:11
@peshkira well, it's supposed to. Only rarely you can't trust it.
– John Dvorak
Nov 8 '12 at 20:14
1
on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
– peshkira
Nov 8 '12 at 20:17
2
I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
– jahroy
Nov 8 '12 at 20:19
2
2
an extension does not say anything about a format.
– peshkira
Nov 8 '12 at 20:11
an extension does not say anything about a format.
– peshkira
Nov 8 '12 at 20:11
@peshkira well, it's supposed to. Only rarely you can't trust it.
– John Dvorak
Nov 8 '12 at 20:14
@peshkira well, it's supposed to. Only rarely you can't trust it.
– John Dvorak
Nov 8 '12 at 20:14
1
1
on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
– peshkira
Nov 8 '12 at 20:17
on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
– peshkira
Nov 8 '12 at 20:17
2
2
I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
– jahroy
Nov 8 '12 at 20:19
I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
– jahroy
Nov 8 '12 at 20:19
add a comment |
up vote
0
down vote
Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier AutoDetectParser.
if(currentImageType ==null){
ByteArrayInputStream is = new ByteArrayInputStream(image);
String mimeType = URLConnection.guessContentTypeFromStream(is);
if(mimeType == null){
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
mimeType = detector.detect(is,md).toString();
if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
}
if(mimeType.contains("png")){
mimeType ="png";
}
else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
mimeType = "jpg";
}
else if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
currentImageType = ImageType.fromValue(mimeType);
}
add a comment |
up vote
0
down vote
Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier AutoDetectParser.
if(currentImageType ==null){
ByteArrayInputStream is = new ByteArrayInputStream(image);
String mimeType = URLConnection.guessContentTypeFromStream(is);
if(mimeType == null){
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
mimeType = detector.detect(is,md).toString();
if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
}
if(mimeType.contains("png")){
mimeType ="png";
}
else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
mimeType = "jpg";
}
else if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
currentImageType = ImageType.fromValue(mimeType);
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier AutoDetectParser.
if(currentImageType ==null){
ByteArrayInputStream is = new ByteArrayInputStream(image);
String mimeType = URLConnection.guessContentTypeFromStream(is);
if(mimeType == null){
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
mimeType = detector.detect(is,md).toString();
if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
}
if(mimeType.contains("png")){
mimeType ="png";
}
else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
mimeType = "jpg";
}
else if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
currentImageType = ImageType.fromValue(mimeType);
}
Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier AutoDetectParser.
if(currentImageType ==null){
ByteArrayInputStream is = new ByteArrayInputStream(image);
String mimeType = URLConnection.guessContentTypeFromStream(is);
if(mimeType == null){
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
mimeType = detector.detect(is,md).toString();
if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
}
if(mimeType.contains("png")){
mimeType ="png";
}
else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
mimeType = "jpg";
}
else if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
currentImageType = ImageType.fromValue(mimeType);
}
answered Mar 21 '16 at 20:28
Akin Okegbile
457720
457720
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13296939%2fhow-do-i-know-if-a-file-type-is-pdf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
– peshkira
Nov 8 '12 at 20:14
Related/duplicate: stackoverflow.com/questions/1915317/…
– Tomasz Nurkiewicz
Nov 8 '12 at 20:14
Try having a look at stackoverflow.com/questions/51438/…
– MadProgrammer
Nov 8 '12 at 20:18