public static void writeTo(String from, File to) throws IOException { InputStream in = null; OutputStream out = null; try { in = new ByteArrayInputStream(from.getBytes("UTF-8")); out = new FileOutputStream(to, true); while (true) { synchronized (buffer) { int amountRead = in.read(buffer); if (amountRead == -1) { break; } out.write(buffer, 0, amountRead); } } } catch (FileNotFoundException e) { Application.error(e); } catch (UnsupportedEncodingException e) { Application.error(e); } catch (IOException e) { Application.error(e); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } }
/** upload a file from an url and return a HashMap with key : the path, value : the file */ public static Map<String, File> uploadFileFromURL(String url, int userId) { Application.debug("uploadFileFromURL : " + url, userId); URL u = null; try { u = new URL(url); } catch (MalformedURLException e) { Application.error(e, userId); return null; } URLConnection connection = null; try { connection = u.openConnection(); // Application.debug("-->length of content's connection : "+connection.getContentLength(), // userId); } catch (IOException e) { Application.error(e, userId); return null; } InputStream input = null; try { input = connection.getInputStream(); } catch (IOException e) { Application.error(e, userId); return null; } String fileName = findNameOfFileInURL(u); FileOutputStream writeFile = null; UUID uuid = UUID.randomUUID(); File dir = new File(Configuration.getTmp_dir() + "/" + uuid); if (!dir.mkdir()) { Application.error( "cannot create directory uploadFileFromURL :" + Configuration.getTmp_dir() + "/" + uuid); return null; } else { String name = fileName; try { writeFile = new FileOutputStream(Configuration.getTmp_dir() + "/" + uuid + "/" + name); } catch (FileNotFoundException e) { Application.error(e, userId); return null; } byte[] buffer = new byte[2048]; int read; // Application.debug("before reading", userId); try { copy(input, writeFile); } catch (IOException e1) { Application.error(e1, userId); } Map<String, File> tmpDir = new HashMap<String, File>(); File file = new File(Configuration.getTmp_dir() + "/" + uuid + "/" + name); tmpDir.put(Configuration.getTmp_dir() + "/" + uuid, file); Application.debug("file : " + file.getAbsolutePath() + " uploaded", userId); return tmpDir; } }
public static InputStream getInputStreamFromURL(String url) { URL u = null; try { u = new URL(url); } catch (MalformedURLException e) { Application.error(e); } URLConnection connection = null; try { connection = u.openConnection(); } catch (IOException e) { Application.error(e); } InputStream input = null; try { input = connection.getInputStream(); } catch (IOException e) { Application.error(e); } return input; }
public static Map<String, File> uploadFileFromUploadField(FileUpload fileUpload, int userId) { Application.debug("uploadFileFromUploadField", userId); Map<String, File> tmpDir = new HashMap<String, File>(); String fileName = fileUpload.getClientFileName(); UUID uuid = UUID.randomUUID(); File dir = new File(Configuration.getTmp_dir() + "/" + uuid); if (!dir.mkdir()) { Application.error("cannot create directory uploadFileFromUploadField :" + uuid); return null; } else { String name = fileName; File tmpFile = new File(Configuration.getTmp_dir() + "/" + uuid + "/" + name); try { fileUpload.writeTo(tmpFile); } catch (IOException e) { Application.error(e); return null; } tmpDir.put(Configuration.getTmp_dir() + "/" + uuid, tmpFile); return tmpDir; } }
public static boolean deleteDirectory(File directory) { Application.debug("deleting : " + directory.getAbsolutePath()); if (directory.exists()) { File[] files = directory.listFiles(); if (null != files) { for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { deleteDirectory(files[i]); } else { files[i].delete(); } } } } return (directory.delete()); }