|
|
@@ -1,180 +0,0 @@
|
|
|
-package commons.utility;
|
|
|
-
|
|
|
-import java.awt.Graphics2D;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.nio.channels.FileChannel;
|
|
|
-import java.util.Date;
|
|
|
-
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-
|
|
|
-import commons.utility.database.NullValue;
|
|
|
-
|
|
|
-
|
|
|
-public class FileUtility {
|
|
|
-
|
|
|
- public static byte[] fromFileToByte(String pathFile) throws IOException
|
|
|
- {
|
|
|
- File file = new File(pathFile);
|
|
|
- InputStream is = new FileInputStream(file);
|
|
|
- long length = file.length();
|
|
|
- byte[] bytes = new byte[(int)length];
|
|
|
- // Read in the bytes
|
|
|
- int offset = 0;
|
|
|
- int numRead = 0;
|
|
|
- while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
|
|
|
- offset += numRead;
|
|
|
- }
|
|
|
- // Ensure all the bytes have been read in
|
|
|
- if (offset < bytes.length) {
|
|
|
- is.close();
|
|
|
- throw new IOException("Impossibile leggere il file " + file.getName());
|
|
|
- }
|
|
|
- // Close the input stream and return bytes
|
|
|
- is.close();
|
|
|
- return bytes;
|
|
|
- }
|
|
|
-
|
|
|
- public static void fromByteToFile(byte[] bytes,
|
|
|
- File file)
|
|
|
- {
|
|
|
- BufferedOutputStream bos = null;
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- //create an object of FileOutputStream
|
|
|
- FileOutputStream fos = new FileOutputStream(file);
|
|
|
- //create an object of BufferedOutputStream
|
|
|
- bos = new BufferedOutputStream(fos);
|
|
|
-
|
|
|
- /*
|
|
|
- * To write byte array to file use,
|
|
|
- * public void write(byte[] b) method of BufferedOutputStream
|
|
|
- * class.
|
|
|
- */
|
|
|
- bos.write(bytes);
|
|
|
- }
|
|
|
- catch (FileNotFoundException fnfe)
|
|
|
- {
|
|
|
- fnfe.printStackTrace();
|
|
|
- }
|
|
|
- catch (IOException ioe)
|
|
|
- {
|
|
|
- ioe.printStackTrace();
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- if (bos != null)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- //flush the BufferedOutputStream
|
|
|
- bos.flush();
|
|
|
- //close the BufferedOutputStream
|
|
|
- bos.close();
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //Elimina una cartella e le sue sottocartelle compresi i file
|
|
|
- public static boolean deleteDir(File dir) {
|
|
|
- if (dir.isDirectory()) {
|
|
|
- String[] children = dir.list();
|
|
|
- for (int i=0; i<children.length; i++) {
|
|
|
- boolean success = deleteDir(new File(dir, children[i]));
|
|
|
- if (!success) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // The directory is now empty so delete it
|
|
|
- return dir.delete();
|
|
|
- }
|
|
|
-
|
|
|
- //Copia file
|
|
|
- public static void copy(File source, File dest) throws IOException {
|
|
|
- FileChannel in = null, out = null;
|
|
|
- try {
|
|
|
- in = new FileInputStream(source).getChannel();
|
|
|
- out = new FileOutputStream(dest).getChannel();
|
|
|
-
|
|
|
- in.transferTo( 0, in.size(), out);
|
|
|
- } finally {
|
|
|
- if (in != null)
|
|
|
- in.close();
|
|
|
- if (out != null)
|
|
|
- out.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static String getFileNameFromPath(String path)
|
|
|
- {
|
|
|
- File file = new File(path);
|
|
|
- return file.getName();
|
|
|
- }
|
|
|
-
|
|
|
- public static String getExtension(String fileName)
|
|
|
- {
|
|
|
- String ext = NullValue.STRING;
|
|
|
- if(!StringUtility.empty(fileName, true))
|
|
|
- ext = fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
- return ext;
|
|
|
- }
|
|
|
-
|
|
|
- public static String getContentType(String tipoFile)
|
|
|
- {
|
|
|
- if(tipoFile.equals("pdf"))
|
|
|
- return "application/pdf";
|
|
|
- else if(tipoFile.equals("zip"))
|
|
|
- return "application/zip";
|
|
|
- else if(tipoFile.equals("rar"))
|
|
|
- return "application/rar";
|
|
|
- else if(tipoFile.equals("doc"))
|
|
|
- return "application/msword";
|
|
|
- else if(tipoFile.equals("jpg"))
|
|
|
- return "image/jpg";
|
|
|
- else if(tipoFile.equals("jpeg"))
|
|
|
- return "image/jpeg";
|
|
|
- else if(tipoFile.equals("bmp"))
|
|
|
- return "imgae/bmp";
|
|
|
- else if(tipoFile.equals("tif"))
|
|
|
- return "image/tif";
|
|
|
- else if(tipoFile.equals("tiff"))
|
|
|
- return "imgae/tiff";
|
|
|
- else if(tipoFile.equals("gif"))
|
|
|
- return "image/gif";
|
|
|
- else if(tipoFile.equals("png"))
|
|
|
- return "imgae/png";
|
|
|
- else
|
|
|
- return "application/octet-stream";
|
|
|
- }
|
|
|
-
|
|
|
- public static byte[] resizeImage(File fileUploaded) throws IOException
|
|
|
- {
|
|
|
- final int IMG_WIDTH = 130;
|
|
|
- final int IMG_HEIGHT = 130;
|
|
|
- BufferedImage originalImage = ImageIO.read(fileUploaded);
|
|
|
- int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
|
|
|
-
|
|
|
- BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
|
|
|
- Graphics2D g = resizedImage.createGraphics();
|
|
|
- g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
|
|
|
- g.dispose();
|
|
|
-
|
|
|
- File file = new File(new Date().getTime() + "");
|
|
|
- ImageIO.write(resizedImage, "jpg", file);
|
|
|
-
|
|
|
- return fromFileToByte(file.getAbsolutePath());
|
|
|
- }
|
|
|
-}
|