List of usage examples for org.apache.commons.io FileUtils moveFile
public static void moveFile(File srcFile, File destFile) throws IOException
From source file:org.fao.geonet.services.logo.Add.java
public Element exec(Element params, ServiceContext context) throws Exception { synchronized (this) { if (logoDirectory == null) { logoDirectory = Resources.locateHarvesterLogosDir(context); }/*from w ww . ja v a2 s.c o m*/ } String file = Util.getParam(params, Params.FNAME); if (file.contains("..")) { throw new BadParameterEx("Invalid character found in resource name.", file); } if ("".equals(file)) { throw new Exception("Logo name is not defined."); } File inFile = new File(context.getUploadDir(), file); File outFile = new File(logoDirectory, file); try { FileUtils.moveFile(inFile, outFile); } catch (Exception e) { inFile.delete(); throw new Exception( "Unable to move uploaded thumbnail to destination: " + outFile + ". Error: " + e.getMessage()); } Element response = new Element("response"); response.addContent(new Element("status").setText("Logo added.")); return response; }
From source file:org.fao.geonet.services.metadata.format.Register.java
private void handleRawXsl(File uploadedFile, File dir) throws IOException { FileUtils.moveFile(uploadedFile, new File(dir, VIEW_XSL_FILENAME)); }
From source file:org.fao.geonet.services.metadata.format.Register.java
private void handleZipFile(ZipFile zipFile, File dir) throws IOException { ZipUtil.extract(zipFile, dir);// ww w . j a v a 2 s . c om removeTopDir(dir); Collection<File> xslFiles = FileUtils.listFiles(dir, new String[] { "xsl" }, false); File toRename = null; for (File file : xslFiles) { if (file.getName().equals(VIEW_XSL_FILENAME)) return; toRename = file; } if (xslFiles.size() > 1 || toRename == null) { throw new IllegalStateException( "Uploaded zip file must have a view.xsl file or only a single xsl file"); } FileUtils.moveFile(toRename, new File(dir, VIEW_XSL_FILENAME)); }
From source file:org.fao.geonet.services.resources.handlers.DefaultResourceUploadHandler.java
private static void moveFile(ServiceContext context, String sourceDir, String filename, File targetDir, String overwrite) throws Exception { // move uploaded file to destination directory // note: uploadDir and rootDir must be in the same volume IO.mkdirs(targetDir, "directory to move a file to"); // get ready to move uploaded file to destination directory File oldFile = new File(sourceDir, filename); File newFile = new File(targetDir, filename); context.info("Source : " + oldFile.getAbsolutePath()); context.info("Destin : " + newFile.getAbsolutePath()); if (!oldFile.exists()) { throw new Exception("File upload unsuccessful " + oldFile.getAbsolutePath() + " does not exist"); }//ww w .ja va 2 s . c o m // check if file already exists and do whatever overwrite wants if (newFile.exists() && overwrite.equals("no")) { throw new Exception("File upload unsuccessful because " + newFile.getName() + " already exists and overwrite was not permitted"); } // move uploaded file to destination directory - have two goes try { FileUtils.moveFile(oldFile, newFile); } catch (Exception e) { context.warning("Cannot move uploaded file"); context.warning(" (C) Source : " + oldFile.getAbsolutePath()); context.warning(" (C) Destin : " + newFile.getAbsolutePath()); IO.delete(oldFile, false, context); throw new Exception("Unable to move uploaded file to destination directory"); } }
From source file:org.fao.geonet.services.resources.Upload.java
protected static void moveFile(ServiceContext context, String sourceDir, String filename, File targetDir, String overwrite) throws Exception { // move uploaded file to destination directory // note: uploadDir and rootDir must be in the same volume targetDir.mkdirs();/*from w ww.j a v a 2 s . c o m*/ // get ready to move uploaded file to destination directory File oldFile = new File(sourceDir, filename); File newFile = new File(targetDir, filename); context.info("Source : " + oldFile.getAbsolutePath()); context.info("Destin : " + newFile.getAbsolutePath()); if (!oldFile.exists()) { throw new Exception("File upload unsuccessful " + oldFile.getAbsolutePath() + " does not exist"); } // check if file already exists and do whatever overwrite wants if (newFile.exists() && overwrite.equals("no")) { throw new Exception("File upload unsuccessful because " + newFile.getName() + " already exists and overwrite was not permitted"); } // move uploaded file to destination directory - have two goes try { FileUtils.moveFile(oldFile, newFile); } catch (Exception e) { oldFile.delete(); context.warning("Cannot move uploaded file"); context.warning(" (C) Source : " + oldFile.getAbsolutePath()); context.warning(" (C) Destin : " + newFile.getAbsolutePath()); oldFile.delete(); throw new Exception("Unable to move uploaded file to destination directory"); } }
From source file:org.fao.geonet.services.thumbnail.Generate.java
public Element serviceSpecificExec(Element params, ServiceContext context) throws Exception { String id = Util.getParam(params, Params.ID); String version = Util.getParam(params, Params.VERSION); String file = Util.getParam(params, Params.NAME, "thumbnail.png"); String jsonConfig = Util.getParam(params, "config"); String rotation = Util.getParam(params, "rotation", null); Integer rotationAngle = null; try {/*w w w. j a v a2 s . c o m*/ rotationAngle = Integer.valueOf(rotation); } catch (NumberFormatException e) { e.printStackTrace(); } Lib.resource.checkEditPrivilege(context, id); GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); DataManager dataMan = gc.getBean(DataManager.class); ThumbnailMaker thumbnailMaker = gc.getBean(ThumbnailMaker.class); //--- check if the metadata has been modified from last time if (version != null && !dataMan.getVersion(id).equals(version)) { throw new ConcurrentUpdateEx(id); } File thumbnailFile = thumbnailMaker.generateThumbnail(jsonConfig, rotationAngle); //--- create destination directory String dataDir = Lib.resource.getDir(context, Params.Access.PUBLIC, id); IO.mkdirs(new File(dataDir), "Metadata data directory"); File outFile = new File(dataDir, file); if (outFile.exists() && !outFile.delete()) { throw new Exception("Unable to overwrite existing file: " + outFile); } try { FileUtils.moveFile(thumbnailFile, outFile); } catch (Exception e) { IO.delete(thumbnailFile, false, context); throw new Exception( "Unable to move uploaded thumbnail to destination: " + outFile + ". Error: " + e.getMessage()); } dataMan.setThumbnail(context, id, false, file, false); dataMan.indexMetadata(id, false); Element response = new Element("a"); response.addContent(new Element("id").setText(id)); response.addContent(new Element("version").setText(dataMan.getNewVersion(id))); return response; }
From source file:org.fao.geonet.services.thumbnail.Set.java
public Element serviceSpecificExec(Element params, ServiceContext context) throws Exception { String id = Util.getParam(params, Params.ID); String type = Util.getParam(params, Params.TYPE); String version = Util.getParam(params, Params.VERSION); String file = Util.getParam(params, Params.FNAME); String scalingDir = Util.getParam(params, Params.SCALING_DIR); boolean scaling = Util.getParam(params, Params.SCALING, false); int scalingFactor = Util.getParamAsInt(params, Params.SCALING_FACTOR); boolean createSmall = Util.getParam(params, Params.CREATE_SMALL, false); String smallScalingDir = Util.getParam(params, Params.SMALL_SCALING_DIR, ""); int smallScalingFactor = Util.getParam(params, Params.SMALL_SCALING_FACTOR, 0); Lib.resource.checkEditPrivilege(context, id); //----------------------------------------------------------------------- //--- environment vars GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); DataManager dataMan = gc.getDataManager(); Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB); //--- check if the metadata has been modified from last time if (version != null && !dataMan.getVersion(id).equals(version)) throw new ConcurrentUpdateEx(id); //----------------------------------------------------------------------- //--- create destination directory String dataDir = Lib.resource.getDir(context, Params.Access.PUBLIC, id); new File(dataDir).mkdirs(); //----------------------------------------------------------------------- //--- create the small thumbnail, removing the old one if (createSmall) { String smallFile = getFileName(file, true); String inFile = context.getUploadDir() + file; String outFile = dataDir + smallFile; removeOldThumbnail(context, dbms, id, "small", false); createThumbnail(inFile, outFile, smallScalingFactor, smallScalingDir); dataMan.setThumbnail(context, dbms, id, true, smallFile, false); }/*from www. j a v a 2 s . com*/ //----------------------------------------------------------------------- //--- create the requested thumbnail, removing the old one removeOldThumbnail(context, dbms, id, type, false); if (scaling) { String newFile = getFileName(file, type.equals("small")); String inFile = context.getUploadDir() + file; String outFile = dataDir + newFile; createThumbnail(inFile, outFile, scalingFactor, scalingDir); if (!new File(inFile).delete()) context.error("Error while deleting thumbnail : " + inFile); dataMan.setThumbnail(context, dbms, id, type.equals("small"), newFile, false); } else { //--- move uploaded file to destination directory File inFile = new File(context.getUploadDir(), file); File outFile = new File(dataDir, file); if (outFile.exists() && !outFile.delete()) { throw new Exception("Unable to overwrite existing file: " + outFile); } try { FileUtils.moveFile(inFile, outFile); } catch (Exception e) { inFile.delete(); throw new Exception("Unable to move uploaded thumbnail to destination: " + outFile + ". Error: " + e.getMessage()); } dataMan.setThumbnail(context, dbms, id, type.equals("small"), file, false); } dataMan.indexInThreadPool(context, id, dbms); //----------------------------------------------------------------------- Element response = new Element("a"); response.addContent(new Element("id").setText(id)); response.addContent(new Element("version").setText(dataMan.getNewVersion(id))); return response; }
From source file:org.fao.geonet.services.thumbnail.Set.java
/** * TODO Javadoc.//from w w w . j a v a2s . c o m * * @param scaling * @param file * @param type * @param dataDir * @param scalingDir * @param scalingFactor * @param dataMan * @param dbms * @param id * @param context * @throws Exception */ private void saveThumbnail(boolean scaling, String file, String type, String dataDir, String scalingDir, int scalingFactor, DataManager dataMan, Dbms dbms, String id, ServiceContext context) throws Exception { if (scaling) { String newFile = getFileName(file, type.equals("small")); String inFile = context.getUploadDir() + file; String outFile = dataDir + newFile; createThumbnail(inFile, outFile, scalingFactor, scalingDir); if (!new File(inFile).delete()) context.error("Error while deleting thumbnail : " + inFile); dataMan.setThumbnail(context, dbms, id, type.equals("small"), newFile, false); } else { //--- move uploaded file to destination directory File inFile = new File(context.getUploadDir(), file); File outFile = new File(dataDir, file); try { FileUtils.moveFile(inFile, outFile); } catch (Exception e) { inFile.delete(); throw new Exception("Unable to move uploaded thumbnail to destination: " + outFile + ". Error: " + e.getMessage()); } dataMan.setThumbnail(context, dbms, id, type.equals("small"), file, false); } }
From source file:org.forgerock.doc.maven.ImageDataTransformer.java
/** * Update files that match, adding them to the results. * * @param file//from w ww .ja v a 2 s .c o m * File to update * @param depth * Not used * @param results * List of files updated * @throws IOException * Something went wrong changing a file's content. */ @Override protected final void handleFile(final File file, final int depth, final Collection<File> results) throws IOException { if (file.isFile()) { try { Source xml = new StreamSource(file); File tmpFile = File.createTempFile(file.getName(), ".tmp"); transformer.transform(xml, new StreamResult(tmpFile)); FileUtils.deleteQuietly(file); FileUtils.moveFile(tmpFile, file); results.add(file); } catch (TransformerException te) { throw new IOException("Failed to transform " + file.getPath() + ": " + te.getStackTrace()); } } }
From source file:org.forgerock.doc.maven.PNGUtils.java
/** * Set the DPI on {@code image} to {@code dotsPerInch}. * * @param image PNG image file./* w w w . j av a 2 s .c o m*/ * @param dotsPerInch DPI to set in metadata. * @throws IOException Failed to save the image. */ public static void setDPI(final File image, final int dotsPerInch) throws IOException { BufferedImage in = ImageIO.read(image); File updatedImage = File.createTempFile(image.getName(), ".tmp"); saveBufferedImage(in, updatedImage, dotsPerInch); FileUtils.deleteQuietly(image); FileUtils.moveFile(updatedImage, image); }