| 21543 |
ashik.ali |
1 |
package com.spice.profitmandi.common.util;
|
|
|
2 |
|
| 26066 |
amit.gupta |
3 |
import com.google.gson.Gson;
|
| 22215 |
ashik.ali |
4 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 26079 |
amit.gupta |
5 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 30122 |
amit.gupta |
6 |
import org.apache.commons.io.FileUtils;
|
|
|
7 |
import org.apache.commons.lang3.StringEscapeUtils;
|
|
|
8 |
import org.apache.logging.log4j.LogManager;
|
|
|
9 |
import org.apache.logging.log4j.Logger;
|
|
|
10 |
import org.springframework.core.io.InputStreamSource;
|
|
|
11 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
12 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
| 21543 |
ashik.ali |
13 |
|
| 30122 |
amit.gupta |
14 |
import javax.mail.Multipart;
|
|
|
15 |
import javax.mail.internet.InternetAddress;
|
|
|
16 |
import javax.mail.internet.MimeBodyPart;
|
|
|
17 |
import javax.mail.internet.MimeMessage;
|
|
|
18 |
import javax.mail.internet.MimeMultipart;
|
| 35204 |
amit |
19 |
import java.io.*;
|
| 32955 |
amit.gupta |
20 |
import java.time.*;
|
| 30122 |
amit.gupta |
21 |
import java.util.*;
|
|
|
22 |
import java.util.concurrent.ConcurrentHashMap;
|
|
|
23 |
import java.util.function.Function;
|
|
|
24 |
import java.util.function.Predicate;
|
|
|
25 |
import java.util.regex.Matcher;
|
|
|
26 |
import java.util.regex.Pattern;
|
|
|
27 |
|
| 21543 |
ashik.ali |
28 |
public class Utils {
|
| 31177 |
tejbeer |
29 |
|
| 35102 |
amit |
30 |
public static final float FLOAT_EPSILON = 0.001f;
|
| 21986 |
kshitij.so |
31 |
|
| 35102 |
amit |
32 |
public static final double DOUBLE_EPSILON = 0.001d;
|
| 31177 |
tejbeer |
33 |
|
| 33045 |
amit.gupta |
34 |
|
| 35102 |
amit |
35 |
public static String[] getAlphaNumericParts(String alphaNumericString) {
|
|
|
36 |
String[] parts = alphaNumericString.split("(?<=\\D)(?=\\d)");
|
|
|
37 |
return parts;
|
|
|
38 |
}
|
| 33045 |
amit.gupta |
39 |
|
| 35102 |
amit |
40 |
private static final Logger logger = LogManager.getLogger(Utils.class);
|
|
|
41 |
public static final String EXPORT_ENTITIES_PATH = getExportPath();
|
|
|
42 |
public static final String PRODUCT_PROPERTIES_SNIPPET = "ProductPropertiesSnippet.html";
|
|
|
43 |
public static final String DOCUMENT_STORE = "/profitmandi/documents/";
|
|
|
44 |
private Gson gson = new Gson();
|
|
|
45 |
private static final Map<Integer, String> helpMap = new HashMap<>(6);
|
|
|
46 |
private static final Map<Integer, String> dthIdAliasMap = new HashMap<>(7);
|
|
|
47 |
private static Map<Long, String> mobileProvidersMap;
|
|
|
48 |
private static Map<Long, String> dthProvidersMap;
|
|
|
49 |
private static Map<Long, String> allProviders;
|
|
|
50 |
public static final String SYSTEM_PARTNER = "testpxps@gmail.com";
|
|
|
51 |
public static final int SYSTEM_PARTNER_ID = 175120474;
|
|
|
52 |
private static final RestClient rc = new RestClient();
|
|
|
53 |
private static final String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
|
|
|
54 |
public static final LocalTime MAX_TIME = LocalTime.of(23, 59, 59);
|
|
|
55 |
// Compile regular expression to get the pattern
|
|
|
56 |
private static final Pattern pattern = Pattern.compile(regex);
|
| 24440 |
amit.gupta |
57 |
|
| 35102 |
amit |
58 |
public static boolean validateEmail(String email) {
|
|
|
59 |
if (email == null) {
|
|
|
60 |
return false;
|
|
|
61 |
}
|
|
|
62 |
Matcher matcher = pattern.matcher(email);
|
|
|
63 |
return matcher.matches();
|
|
|
64 |
}
|
| 31177 |
tejbeer |
65 |
|
| 35102 |
amit |
66 |
@SuppressWarnings("serial")
|
|
|
67 |
public static final Map<String, String> MIME_TYPE = Collections.unmodifiableMap(new HashMap<String, String>() {
|
|
|
68 |
{
|
|
|
69 |
put("image/png", "png");
|
|
|
70 |
put("image/jpeg", "jpeg");
|
|
|
71 |
put("image/pjpeg", "jpeg");
|
|
|
72 |
put("application/pdf", "pdf");
|
|
|
73 |
put("application/msword", "doc");
|
|
|
74 |
put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
|
|
|
75 |
put("application/vnd.ms-excel", "xls");
|
|
|
76 |
put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
|
|
|
77 |
}
|
|
|
78 |
});
|
| 21543 |
ashik.ali |
79 |
|
| 35102 |
amit |
80 |
private static String getExportPath() {
|
|
|
81 |
String exportPath = "/var/lib/tomcat7/webapps/export/html/entities/";
|
|
|
82 |
/*
|
|
|
83 |
* String exportPath = null;
|
|
|
84 |
*
|
|
|
85 |
* try { ConfigClient client = ConfigClient.getClient(); exportPath =
|
|
|
86 |
* client.get("export_entities_path"); } catch (Exception ce) {
|
|
|
87 |
* logger.error("Unable to read export path from the config client: ", ce);
|
|
|
88 |
* logger.warn("Setting the default export path"); exportPath =
|
|
|
89 |
* "/var/lib/tomcat7/webapps/export/html/entities/"; }
|
|
|
90 |
*/
|
|
|
91 |
return exportPath;
|
|
|
92 |
}
|
| 21986 |
kshitij.so |
93 |
|
| 35102 |
amit |
94 |
public static LocalDateTime toLocalDateTime(long epochTimeInMillis) {
|
|
|
95 |
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochTimeInMillis), ZoneId.systemDefault());
|
|
|
96 |
}
|
| 32724 |
amit.gupta |
97 |
|
| 35102 |
amit |
98 |
public static boolean copyDocument(String documentPath) {
|
|
|
99 |
File source = new File(documentPath);
|
|
|
100 |
File dest = new File(DOCUMENT_STORE + source.getName());
|
|
|
101 |
try {
|
|
|
102 |
FileUtils.copyFile(source, dest);
|
|
|
103 |
} catch (IOException e) {
|
|
|
104 |
e.printStackTrace();
|
|
|
105 |
return false;
|
|
|
106 |
}
|
|
|
107 |
return true;
|
|
|
108 |
}
|
| 21543 |
ashik.ali |
109 |
|
| 35102 |
amit |
110 |
public static Map<Long, String> getMobileProvidersMap() {
|
|
|
111 |
return mobileProvidersMap;
|
|
|
112 |
}
|
| 21543 |
ashik.ali |
113 |
|
| 35102 |
amit |
114 |
public static Map<Long, String> getDthProvidersMap() {
|
|
|
115 |
return dthProvidersMap;
|
|
|
116 |
}
|
| 21986 |
kshitij.so |
117 |
|
| 35102 |
amit |
118 |
public static Map<Long, String> getAllProviders() {
|
|
|
119 |
return allProviders;
|
|
|
120 |
}
|
| 21986 |
kshitij.so |
121 |
|
| 35102 |
amit |
122 |
public static String getProvider(long operatorId) {
|
|
|
123 |
return allProviders.get(operatorId);
|
|
|
124 |
}
|
| 21543 |
ashik.ali |
125 |
|
| 35102 |
amit |
126 |
public static void sendSms(String text, String mobileNumber) throws Exception {
|
|
|
127 |
Map<String, String> paramsMap = new HashMap<>();
|
|
|
128 |
paramsMap.put("username", "SmartDukaanT");
|
|
|
129 |
paramsMap.put("password", "Smart@91");
|
|
|
130 |
paramsMap.put("senderID", "SMTDKN");
|
|
|
131 |
paramsMap.put("message", text);
|
|
|
132 |
paramsMap.put("mobile", mobileNumber);
|
|
|
133 |
paramsMap.put("messageType", "Text");
|
|
|
134 |
// rc.getResponse(SMS_GATEWAY, paramsMap, null);
|
|
|
135 |
}
|
| 24440 |
amit.gupta |
136 |
|
| 35102 |
amit |
137 |
public static void sendMailWithAttachments(JavaMailSender mailSender, String emailTo, String[] cc, String subject,
|
|
|
138 |
String body, List<File> attachments) throws Exception {
|
|
|
139 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
140 |
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
|
141 |
helper.setSubject(subject);
|
|
|
142 |
helper.setText(body);
|
|
|
143 |
if (cc != null) {
|
|
|
144 |
helper.setCc(cc);
|
|
|
145 |
}
|
|
|
146 |
helper.setTo(emailTo);
|
|
|
147 |
// helper.setTo("amit.gupta@smartdukaan.com");
|
|
|
148 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
|
|
|
149 |
helper.setFrom(senderAddress);
|
|
|
150 |
if (attachments != null) {
|
|
|
151 |
for (File file : attachments) {
|
|
|
152 |
helper.addAttachment(file.getName(), file);
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
mailSender.send(message);
|
|
|
156 |
}
|
| 25764 |
amit.gupta |
157 |
|
| 35102 |
amit |
158 |
public static class Attachment {
|
|
|
159 |
public Attachment(String fileName, InputStreamSource inputStreamSource) {
|
|
|
160 |
this.fileName = fileName;
|
|
|
161 |
this.inputStreamSource = inputStreamSource;
|
|
|
162 |
}
|
| 25764 |
amit.gupta |
163 |
|
| 35102 |
amit |
164 |
private String fileName;
|
|
|
165 |
private InputStreamSource inputStreamSource;
|
| 25764 |
amit.gupta |
166 |
|
| 35102 |
amit |
167 |
public String getFileName() {
|
|
|
168 |
return fileName;
|
|
|
169 |
}
|
| 25764 |
amit.gupta |
170 |
|
| 35102 |
amit |
171 |
public void setFileName(String fileName) {
|
|
|
172 |
this.fileName = fileName;
|
|
|
173 |
}
|
| 25764 |
amit.gupta |
174 |
|
| 35102 |
amit |
175 |
public InputStreamSource getInputStreamSource() {
|
|
|
176 |
return inputStreamSource;
|
|
|
177 |
}
|
| 25764 |
amit.gupta |
178 |
|
| 35102 |
amit |
179 |
public void setInputStreamSource(InputStreamSource inputStreamSource) {
|
|
|
180 |
this.inputStreamSource = inputStreamSource;
|
|
|
181 |
}
|
| 25764 |
amit.gupta |
182 |
|
| 35102 |
amit |
183 |
@Override
|
|
|
184 |
public String toString() {
|
|
|
185 |
return "Attachment [fileName=" + fileName + ", inputStreamSource=" + inputStreamSource + "]";
|
|
|
186 |
}
|
|
|
187 |
}
|
| 25764 |
amit.gupta |
188 |
|
| 35102 |
amit |
189 |
public static void sendMailWithAttachment(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
|
|
|
190 |
String body, String fileName, InputStreamSource inputStreamSource) throws Exception {
|
|
|
191 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
192 |
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
|
193 |
helper.setSubject(subject);
|
|
|
194 |
helper.setText(body);
|
|
|
195 |
if (cc != null) {
|
|
|
196 |
helper.setCc(cc);
|
|
|
197 |
}
|
|
|
198 |
helper.setTo(emailTo);
|
|
|
199 |
helper.addAttachment(fileName, inputStreamSource);
|
|
|
200 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
|
|
|
201 |
helper.setFrom(senderAddress);
|
|
|
202 |
mailSender.send(message);
|
|
|
203 |
}
|
| 25764 |
amit.gupta |
204 |
|
| 35102 |
amit |
205 |
public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String[] bcc,
|
|
|
206 |
String subject, String body, Attachment... attachments) throws Exception {
|
|
|
207 |
sendMailWithAttachments(mailSender, emailTo, cc, bcc,
|
|
|
208 |
subject, body, false, attachments);
|
|
|
209 |
}
|
| 25764 |
amit.gupta |
210 |
|
| 35102 |
amit |
211 |
public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String[] bcc,
|
|
|
212 |
String subject, String body, boolean html, Attachment... attachments) throws Exception {
|
|
|
213 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
214 |
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
|
215 |
helper.setSubject(subject);
|
|
|
216 |
helper.setText(body, html);
|
|
|
217 |
if (cc != null) {
|
|
|
218 |
helper.setCc(cc);
|
|
|
219 |
}
|
|
|
220 |
if (bcc != null) {
|
|
|
221 |
helper.setBcc(bcc);
|
|
|
222 |
}
|
|
|
223 |
helper.setTo(emailTo);
|
|
|
224 |
for (Attachment attachment : attachments) {
|
| 35204 |
amit |
225 |
//saveInputStreamSourceToHome(attachment.getInputStreamSource(), attachment.getFileName());
|
| 35102 |
amit |
226 |
helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
|
|
|
227 |
}
|
|
|
228 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
|
|
|
229 |
helper.setFrom(senderAddress);
|
|
|
230 |
mailSender.send(message);
|
|
|
231 |
}
|
| 24440 |
amit.gupta |
232 |
|
| 35102 |
amit |
233 |
public static void sendHtmlMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc,
|
|
|
234 |
String subject, String body, Attachment... attachments) throws Exception {
|
|
|
235 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
236 |
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
|
237 |
helper.setSubject(subject);
|
|
|
238 |
helper.setText(body, true);
|
|
|
239 |
// helper.setText(body, true);
|
|
|
240 |
if (cc != null) {
|
|
|
241 |
helper.setCc(cc);
|
|
|
242 |
}
|
|
|
243 |
helper.setTo(emailTo);
|
|
|
244 |
for (Attachment attachment : attachments) {
|
|
|
245 |
if (attachment == null)
|
|
|
246 |
break;
|
|
|
247 |
helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
|
|
|
248 |
}
|
|
|
249 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
|
|
|
250 |
helper.setFrom(senderAddress);
|
|
|
251 |
mailSender.send(message);
|
|
|
252 |
}
|
| 21986 |
kshitij.so |
253 |
|
| 35102 |
amit |
254 |
public static String getIconUrl(int entityId, String host, int port, String webapp) {
|
|
|
255 |
return "";
|
|
|
256 |
}
|
| 23509 |
amit.gupta |
257 |
|
| 35102 |
amit |
258 |
public static void main(String[] args) throws Exception {
|
|
|
259 |
Utils.sendSms("Hello", "9990381569");
|
|
|
260 |
}
|
| 27285 |
tejbeer |
261 |
|
| 35102 |
amit |
262 |
public static void sendEmbeddedHtmlMail(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
|
|
|
263 |
String body, Map<? extends Serializable, File> map) throws Exception {
|
|
|
264 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
265 |
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
|
266 |
helper.setSubject(subject);
|
|
|
267 |
Multipart mp = new MimeMultipart();
|
| 27285 |
tejbeer |
268 |
|
| 35102 |
amit |
269 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
|
|
270 |
messageBodyPart.setContent(body, "text/html");
|
|
|
271 |
mp.addBodyPart(messageBodyPart);
|
| 25998 |
amit.gupta |
272 |
|
| 35102 |
amit |
273 |
// helper.setText(body, true);
|
|
|
274 |
if (cc != null) {
|
|
|
275 |
helper.setCc(cc);
|
|
|
276 |
}
|
|
|
277 |
helper.setTo(emailTo);
|
|
|
278 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
|
|
|
279 |
helper.setFrom(senderAddress);
|
|
|
280 |
for (Map.Entry<? extends Serializable, File> entry : map.entrySet()) {
|
|
|
281 |
entry.getKey();
|
|
|
282 |
entry.getValue();
|
|
|
283 |
MimeBodyPart imagePart = new MimeBodyPart();
|
|
|
284 |
imagePart.setHeader("Content-ID", entry.getKey() + "");
|
|
|
285 |
imagePart.setDisposition(MimeBodyPart.INLINE);
|
|
|
286 |
imagePart.attachFile(entry.getValue());
|
|
|
287 |
mp.addBodyPart(imagePart);
|
| 25764 |
amit.gupta |
288 |
|
| 35102 |
amit |
289 |
}
|
|
|
290 |
message.setContent(mp);
|
|
|
291 |
mailSender.send(message);
|
| 27285 |
tejbeer |
292 |
|
| 35102 |
amit |
293 |
}
|
| 32199 |
amit.gupta |
294 |
|
| 35102 |
amit |
295 |
public String html(String string) {
|
|
|
296 |
return org.apache.commons.lang.StringEscapeUtils.escapeHtml(String.valueOf(string));
|
|
|
297 |
}
|
| 25764 |
amit.gupta |
298 |
|
| 35102 |
amit |
299 |
public String htmlJson(Object object) {
|
|
|
300 |
return StringEscapeUtils.escapeHtml4(gson.toJson(object));
|
|
|
301 |
}
|
| 27285 |
tejbeer |
302 |
|
| 35102 |
amit |
303 |
public static String getHyphenatedString(String s) {
|
|
|
304 |
s = s.trim().replaceAll("\\s+", " ");
|
|
|
305 |
s = s.replaceAll("\\s", "-");
|
|
|
306 |
return s.toLowerCase();
|
|
|
307 |
}
|
| 30122 |
amit.gupta |
308 |
|
| 35102 |
amit |
309 |
public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
|
|
|
310 |
String body, Attachment... attachments) throws Exception {
|
|
|
311 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
312 |
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
|
313 |
helper.setSubject(subject);
|
|
|
314 |
helper.setText(body, true);
|
|
|
315 |
if (cc != null) {
|
|
|
316 |
helper.setCc(cc);
|
|
|
317 |
}
|
|
|
318 |
helper.setTo(emailTo);
|
|
|
319 |
for (Attachment attachment : attachments) {
|
|
|
320 |
helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
|
| 35204 |
amit |
321 |
//saveInputStreamSourceToHome(attachment.getInputStreamSource(), attachment.getFileName());
|
| 35102 |
amit |
322 |
}
|
|
|
323 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
|
|
|
324 |
helper.setFrom(senderAddress);
|
|
|
325 |
mailSender.send(message);
|
|
|
326 |
}
|
| 33354 |
amit.gupta |
327 |
|
| 35102 |
amit |
328 |
public static int compareFloat(float f1, float f2) {
|
|
|
329 |
if (Math.abs(f1 - f2) < DOUBLE_EPSILON) {
|
|
|
330 |
return 0;
|
|
|
331 |
}
|
| 26534 |
amit.gupta |
332 |
|
| 35102 |
amit |
333 |
return Float.compare(f1, f2);
|
|
|
334 |
}
|
| 31177 |
tejbeer |
335 |
|
| 35102 |
amit |
336 |
public static int compareDouble(double d1, double d2) {
|
|
|
337 |
if (Math.abs(d1 - d2) < DOUBLE_EPSILON) {
|
|
|
338 |
return 0;
|
|
|
339 |
}
|
|
|
340 |
return Double.compare(d1, d2);
|
|
|
341 |
}
|
| 30122 |
amit.gupta |
342 |
|
| 35102 |
amit |
343 |
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
|
|
|
344 |
Set<Object> seen = ConcurrentHashMap.newKeySet();
|
|
|
345 |
return t -> seen.add(keyExtractor.apply(t));
|
|
|
346 |
}
|
| 31903 |
amit.gupta |
347 |
|
| 35102 |
amit |
348 |
public static <T> Predicate<T> smallestByKey(Function<? super T, ?> keyExtractor) {
|
|
|
349 |
Set<Object> seen = ConcurrentHashMap.newKeySet();
|
|
|
350 |
return t -> seen.add(keyExtractor.apply(t));
|
|
|
351 |
}
|
| 32955 |
amit.gupta |
352 |
|
| 35102 |
amit |
353 |
public static LocalDate convertToLocalDate(Date dateToConvert) {
|
|
|
354 |
return Instant.ofEpochMilli(dateToConvert.getTime())
|
|
|
355 |
.atZone(ZoneId.systemDefault())
|
|
|
356 |
.toLocalDate();
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
public static LocalDateTime convertToLocalDateTime(Date dateToConvert) {
|
|
|
360 |
return Instant.ofEpochMilli(dateToConvert.getTime())
|
|
|
361 |
.atZone(ZoneId.systemDefault())
|
|
|
362 |
.toLocalDateTime();
|
|
|
363 |
}
|
|
|
364 |
|
| 35204 |
amit |
365 |
/**
|
|
|
366 |
* Saves the given InputStream to the specified File.
|
|
|
367 |
* Automatically creates parent directories if they don't exist.
|
|
|
368 |
*
|
|
|
369 |
* @param inputStream the source InputStream
|
|
|
370 |
* @param file the destination file
|
|
|
371 |
* @throws IOException if an I/O error occurs
|
|
|
372 |
*/
|
|
|
373 |
public static void saveStreamToFile(InputStream inputStream, File file) throws IOException {
|
|
|
374 |
// Ensure parent directories exist
|
|
|
375 |
if (file.getParentFile() != null && !file.getParentFile().exists()) {
|
|
|
376 |
file.getParentFile().mkdirs();
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
try (InputStream in = inputStream;
|
|
|
380 |
OutputStream out = new FileOutputStream(file)) {
|
|
|
381 |
byte[] buffer = new byte[8192]; // 8 KB buffer
|
|
|
382 |
int bytesRead;
|
|
|
383 |
while ((bytesRead = in.read(buffer)) != -1) {
|
|
|
384 |
out.write(buffer, bytesRead == 0 ? 0 : 0, bytesRead);
|
|
|
385 |
}
|
|
|
386 |
}
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
/**
|
|
|
390 |
* Saves a Spring InputStreamSource to the specified file.
|
|
|
391 |
* Works on all OS (Windows, macOS, Linux) and Java 8+.
|
|
|
392 |
*/
|
|
|
393 |
/**
|
|
|
394 |
* Saves a Spring InputStreamSource to a file inside the user's home directory.
|
|
|
395 |
* Works on Java 8+ and all operating systems.
|
|
|
396 |
*
|
|
|
397 |
* @param source the InputStreamSource (e.g., MultipartFile, ByteArrayResource)
|
|
|
398 |
* @param fileName the file name (without path)
|
|
|
399 |
* @return the created File reference
|
|
|
400 |
* @throws IOException if an error occurs during write
|
|
|
401 |
*/
|
|
|
402 |
public static File saveInputStreamSourceToHome(InputStreamSource source, String fileName) throws IOException {
|
|
|
403 |
// Get user home directory
|
|
|
404 |
String userHome = System.getProperty("user.home");
|
|
|
405 |
|
|
|
406 |
// Build the file path (home + filename)
|
|
|
407 |
File targetFile = new File(userHome, fileName);
|
|
|
408 |
|
|
|
409 |
// Ensure parent directories exist (in case filename contains subfolders)
|
|
|
410 |
if (targetFile.getParentFile() != null && !targetFile.getParentFile().exists()) {
|
|
|
411 |
targetFile.getParentFile().mkdirs();
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
// Copy the stream contents
|
|
|
415 |
try (InputStream in = source.getInputStream();
|
|
|
416 |
OutputStream out = new FileOutputStream(targetFile)) {
|
|
|
417 |
|
|
|
418 |
byte[] buffer = new byte[8192];
|
|
|
419 |
int bytesRead;
|
|
|
420 |
while ((bytesRead = in.read(buffer)) != -1) {
|
|
|
421 |
out.write(buffer, 0, bytesRead);
|
|
|
422 |
}
|
|
|
423 |
}
|
|
|
424 |
logger.info("Saving InputStreamSource to home directory: {}", targetFile.getAbsolutePath());
|
|
|
425 |
|
|
|
426 |
return targetFile;
|
|
|
427 |
}
|
|
|
428 |
|
| 26084 |
amit.gupta |
429 |
}
|