/*
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
*/
|
package com.megatim.apifdxweb.impl.tools;
|
|
import com.megatim.apifdxweb.tools.AppContext;
|
import com.megatim.apifdxweb.tools.exceptions.ApplicationServerException;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.List;
|
import javax.ws.rs.core.MultivaluedMap;
|
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
|
|
/**
|
*
|
* @author ASUS
|
*/
|
public class Utilities {
|
|
// public static boolean arePropertiesOk() {
|
// boolean dirOk = AppContext.INTEGRATION_STANDALONE_DESTINATION_DIR != null && !AppContext.INTEGRATION_STANDALONE_DESTINATION_DIR.isEmpty()
|
// && AppContext.INTEGRATION_STANDALONE_TEMP_DIR != null && !AppContext.INTEGRATION_STANDALONE_TEMP_DIR.isEmpty()
|
// && Files.exists(Paths.get(AppContext.INTEGRATION_STANDALONE_TEMP_DIR));
|
// System.out.println("dirOk = " + dirOk);
|
//
|
// if (!AppContext.INTEGRATION_STANDALONE_IS_REMOTE) {
|
// return dirOk;
|
// } else {
|
// boolean paramsOk = AppContext.INTEGRATION_STANDALONE_HOSTNAME != null && !AppContext.INTEGRATION_STANDALONE_HOSTNAME.isEmpty()
|
// && AppContext.INTEGRATION_STANDALONE_PASSWORD != null && !AppContext.INTEGRATION_STANDALONE_PASSWORD.isEmpty()
|
// && AppContext.INTEGRATION_STANDALONE_USERNAME != null && !AppContext.INTEGRATION_STANDALONE_USERNAME.isEmpty();
|
//
|
// System.out.println("paramsOk = " + paramsOk);
|
// return dirOk && paramsOk;
|
// }
|
// }
|
|
public static File saveFileToDisk(List<InputPart> inputParts) throws ApplicationServerException {
|
|
int bufferSize = 1024;
|
File customDir = new File(AppContext.UPLOAD_DIR);
|
|
//Si le repertoire n'existe pas, on crée
|
if (!customDir.exists()) {
|
//On crée le repertoire
|
customDir.mkdir();
|
}
|
|
if (inputParts != null) {
|
for (InputPart inputPart : inputParts) {
|
MultivaluedMap<String, String> header = inputPart.getHeaders();
|
try {
|
String fileName = getFileName(header);
|
InputStream inputStream = inputPart.getBody(InputStream.class, null);
|
fileName = customDir + File.separator + fileName;
|
File file = writeFile(inputStream, fileName, bufferSize);
|
return file;
|
} catch (IOException ex) {
|
ex.printStackTrace();
|
throw new ApplicationServerException();
|
}
|
}
|
}
|
return null;
|
}
|
|
private static String getFileName(MultivaluedMap<String, String> header) {
|
String[] contentDisposition = header.getFirst("Content-Disposition").split(";");
|
for (String filename : contentDisposition) {
|
if ((filename.trim().startsWith("filename"))) {
|
String[] name = filename.split("=");
|
String finalFileName = name[1].trim().replaceAll("\"", "");
|
return finalFileName;
|
}
|
}
|
return null;
|
}
|
|
private static File writeFile(InputStream input, String filename, int bufferSize) throws IOException {
|
|
File file = new File(filename);
|
if (!file.exists()) {
|
file.createNewFile();
|
}
|
|
try ( FileOutputStream fop = new FileOutputStream(file)) {
|
byte[] buffer = new byte[bufferSize];
|
int n = 0;
|
while (-1 != (n = input.read(buffer))) {
|
fop.write(buffer, 0, n);
|
}
|
fop.flush();
|
}
|
|
return file;
|
}
|
}
|