| 22124 |
ashik.ali |
1 |
package com.spice.profitmandi.common.util;
|
|
|
2 |
|
| 30728 |
amit.gupta |
3 |
import com.spice.profitmandi.common.enumuration.ContentType;
|
|
|
4 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 23951 |
amit.gupta |
5 |
import org.apache.commons.csv.CSVFormat;
|
| 29598 |
tejbeer |
6 |
import org.apache.commons.csv.CSVParser;
|
| 23951 |
amit.gupta |
7 |
import org.apache.commons.csv.CSVPrinter;
|
| 29598 |
tejbeer |
8 |
import org.apache.commons.csv.CSVRecord;
|
| 23951 |
amit.gupta |
9 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
|
|
10 |
import org.apache.logging.log4j.LogManager;
|
|
|
11 |
import org.apache.logging.log4j.Logger;
|
| 22124 |
ashik.ali |
12 |
import org.apache.tika.Tika;
|
| 29598 |
tejbeer |
13 |
import org.springframework.web.multipart.MultipartFile;
|
| 22124 |
ashik.ali |
14 |
|
| 30728 |
amit.gupta |
15 |
import java.io.*;
|
|
|
16 |
import java.util.List;
|
| 22124 |
ashik.ali |
17 |
|
|
|
18 |
public class FileUtil {
|
| 23951 |
amit.gupta |
19 |
|
|
|
20 |
private static final Logger LOGGER = LogManager.getLogger(FileUtil.class);
|
|
|
21 |
|
|
|
22 |
public static ContentType detectFileType(File file) throws ProfitMandiBusinessException {
|
| 22124 |
ashik.ali |
23 |
Tika tika = new Tika();
|
| 23951 |
amit.gupta |
24 |
|
|
|
25 |
// detecting the file type using detect method
|
|
|
26 |
String filetype;
|
| 22124 |
ashik.ali |
27 |
try {
|
|
|
28 |
filetype = tika.detect(file);
|
|
|
29 |
} catch (IOException e) {
|
|
|
30 |
e.printStackTrace();
|
| 23780 |
ashik.ali |
31 |
throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1001");
|
| 22124 |
ashik.ali |
32 |
}
|
| 23951 |
amit.gupta |
33 |
if (filetype.contains("pdf")) {
|
|
|
34 |
return ContentType.PDF;
|
|
|
35 |
} else if (filetype.contains("jpg") | filetype.contains("jpeg")) {
|
|
|
36 |
return ContentType.JPEG;
|
|
|
37 |
} else if (filetype.contains("png")) {
|
|
|
38 |
return ContentType.PNG;
|
|
|
39 |
} else {
|
|
|
40 |
throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1002");
|
|
|
41 |
}
|
| 22124 |
ashik.ali |
42 |
}
|
| 23951 |
amit.gupta |
43 |
|
|
|
44 |
public static void main(String[] args) {
|
|
|
45 |
// detectFileType(new File("/hsps-docs/1499163441532"));
|
|
|
46 |
// System.out.println("he");
|
| 22124 |
ashik.ali |
47 |
}
|
| 23951 |
amit.gupta |
48 |
|
| 25840 |
amit.gupta |
49 |
public static ByteArrayOutputStream getCSVByteStream(List<String> headers, List<List<?>> rows) throws Exception {
|
| 23951 |
amit.gupta |
50 |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
51 |
PrintStream writer = new PrintStream(baos);
|
|
|
52 |
CSVPrinter csvPrinter = new CSVPrinter(writer,
|
|
|
53 |
CSVFormat.EXCEL.withTrim().withHeader(headers.toArray(new String[] {})));) {
|
| 25840 |
amit.gupta |
54 |
for (List<?> row : rows) {
|
| 23951 |
amit.gupta |
55 |
csvPrinter.printRecord(row);
|
|
|
56 |
}
|
|
|
57 |
return baos;
|
|
|
58 |
}
|
|
|
59 |
}
|
| 29598 |
tejbeer |
60 |
|
| 34619 |
ranu |
61 |
public static ByteArrayOutputStream getCSVByteStreamWithMultiHeaders(List<List<String>> headers, List<List<?>> rows) throws Exception {
|
|
|
62 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
63 |
try (PrintStream writer = new PrintStream(baos);
|
|
|
64 |
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL.withTrim())) {
|
|
|
65 |
|
|
|
66 |
// Print all header rows manually
|
|
|
67 |
for (List<String> headerRow : headers) {
|
|
|
68 |
csvPrinter.printRecord(headerRow);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// Print all data rows
|
|
|
72 |
for (List<?> row : rows) {
|
|
|
73 |
csvPrinter.printRecord(row);
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
return baos;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
|
| 29598 |
tejbeer |
80 |
public static List<CSVRecord> readFile(MultipartFile file) throws IOException, ProfitMandiBusinessException {
|
|
|
81 |
|
|
|
82 |
CSVParser parser = new CSVParser(new InputStreamReader(file.getInputStream()), CSVFormat.DEFAULT);
|
|
|
83 |
List<CSVRecord> records = parser.getRecords();
|
|
|
84 |
LOGGER.info("records" + records);
|
|
|
85 |
LOGGER.info("parser" + parser);
|
|
|
86 |
if (records.size() < 2) {
|
|
|
87 |
parser.close();
|
|
|
88 |
throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
|
|
|
89 |
}
|
|
|
90 |
// Remove header
|
|
|
91 |
records.remove(0);
|
|
|
92 |
parser.close();
|
|
|
93 |
return records;
|
|
|
94 |
|
|
|
95 |
}
|
| 30728 |
amit.gupta |
96 |
|
|
|
97 |
public static List<CSVRecord> readFile(File file) throws IOException, ProfitMandiBusinessException {
|
|
|
98 |
FileReader filereader = new FileReader(file);
|
|
|
99 |
CSVParser parser = new CSVParser(filereader, CSVFormat.DEFAULT);
|
|
|
100 |
List<CSVRecord> records = parser.getRecords();
|
|
|
101 |
LOGGER.info("records" + records);
|
|
|
102 |
LOGGER.info("parser" + parser);
|
|
|
103 |
if (records.size() < 2) {
|
|
|
104 |
parser.close();
|
|
|
105 |
throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
|
|
|
106 |
}
|
|
|
107 |
// Remove header
|
|
|
108 |
records.remove(0);
|
|
|
109 |
parser.close();
|
|
|
110 |
return records;
|
|
|
111 |
|
|
|
112 |
}
|
| 22124 |
ashik.ali |
113 |
}
|