| 22124 |
ashik.ali |
1 |
package com.spice.profitmandi.common.util;
|
|
|
2 |
|
|
|
3 |
import java.io.File;
|
|
|
4 |
import java.io.IOException;
|
|
|
5 |
|
|
|
6 |
import org.apache.tika.Tika;
|
|
|
7 |
|
|
|
8 |
import com.spice.profitmandi.common.enumuration.ContentType;
|
|
|
9 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
10 |
|
|
|
11 |
public class FileUtil {
|
|
|
12 |
public static ContentType detectFileType(File file) throws ProfitMandiBusinessException{
|
|
|
13 |
Tika tika = new Tika();
|
|
|
14 |
|
|
|
15 |
//detecting the file type using detect method
|
|
|
16 |
String filetype;
|
|
|
17 |
try {
|
|
|
18 |
filetype = tika.detect(file);
|
|
|
19 |
} catch (IOException e) {
|
|
|
20 |
e.printStackTrace();
|
|
|
21 |
throw new ProfitMandiBusinessException("Content-Type", file.getName(), "");
|
|
|
22 |
}
|
|
|
23 |
if(filetype.contains("pdf")){
|
|
|
24 |
return ContentType.PDF;
|
|
|
25 |
}else if(filetype.contains("jpg") | filetype.contains("jpeg")){
|
|
|
26 |
return ContentType.JPEG;
|
|
|
27 |
}else if(filetype.contains("png")){
|
|
|
28 |
return ContentType.PNG;
|
|
|
29 |
}else{
|
|
|
30 |
throw new ProfitMandiBusinessException("Content-Type", file.getName(), "");
|
|
|
31 |
}
|
|
|
32 |
}
|
|
|
33 |
public static void main(String[] args){
|
|
|
34 |
//detectFileType(new File("/hsps-docs/1499163441532"));
|
|
|
35 |
//System.out.println("he");
|
|
|
36 |
}
|
|
|
37 |
}
|