List of usage examples for org.apache.commons.io FilenameUtils getExtension
public static String getExtension(String filename)
From source file:com.nuvolect.securesuite.webserver.connector.FileObj.java
/** * Create and return an elFinder file object. * @param volumeId// ww w . j a v a 2 s.c o m * @param file * @param url * @return */ public static JsonObject makeObj(String volumeId, OmniFile file, String url) { JsonObject obj = new JsonObject(); /** * Look for the root path. If so there is no parent. * Assign null to the parentFile and do not include the phash parameter. */ OmniFile parentFile = null; /** * The root file path is everything up to the root slash "/". */ if (!file.isRoot()) { parentFile = file.getParentFile(); } /** * Start with the file parameters */ obj.addProperty("hash", file.getHash()); obj.addProperty("isowner", false);//TODO confirm necessary, isowner is not documented obj.addProperty("locked", file.canWrite() ? 0 : 1); if (file.isDirectory()) { obj.addProperty("volumeid", volumeId); } String name = file.getName(); obj.addProperty("name", name); boolean isImage = MimeUtil.isImage(FilenameUtils.getExtension(name).toLowerCase(Locale.US)); String mime = file.getMime(); obj.addProperty("mime", mime); if (parentFile != null) { obj.addProperty("phash", parentFile.getHash()); } obj.addProperty("read", file.canRead() ? 1 : 0); obj.addProperty("size", file.length()); long timeStamp = file.lastModified() / 1000; if (DEBUG && timeStamp < 0) { LogUtil.log(LogUtil.LogType.FILE_OBJ, "Negative timestamp: " + timeStamp + " for file: " + file.getName()); } obj.addProperty("ts", timeStamp); obj.addProperty("write", file.canWrite() ? 1 : 0); /** * Normally the client uses this URL as a base to fetch thumbnails, * a clear text filename would be added to perform a GET. * This works fine for single volume, however to support multiple volumes * the volumeId is required. We just set the url to root and use the * same volumeId+hash system to fetch thumbnails. */ obj.addProperty("tmbUrl", url + "/"); obj.add("disabled", new JsonArray()); if (isImage) { OmniFile thumbnailFile = OmniImage.makeThumbnail(file); obj.addProperty("tmb", thumbnailFile.getHash()); String dim = OmniImage.getDim(file); obj.addProperty("dim", dim); } /** * Add parameters for "directory" and "volume" */ if (file.isDirectory()) { // Check if directory has any subdirectories boolean dirs = false; OmniFile[] files = file.listFiles(); for (OmniFile afile : files) { if (afile.isDirectory()) { dirs = true; break; } } obj.addProperty("dirs", dirs ? "1" : "0"); obj.addProperty("volumeid", volumeId); if (file.isRoot()) { // Volume has uiCmdMap, directory does not obj.add("uiCmdMap", new JsonArray()); } /*TODO test - provide url for custom directory icon. Directory has custom icon, volume does not obj.put("icon", httpIpPort+"/elFinder/img/diricon.png");*/ } return obj; }
From source file:io.github.mzmine.modules.featuretableimport.FeatureTableImportModule.java
@Override public void runModule(@Nonnull MZmineProject project, @Nonnull ParameterSet parameters, @Nonnull Collection<Task<?>> tasks) { final List<File> fileNames = parameters.getParameter(FeatureTableImportParameters.fileNames).getValue(); final String removePrefix = parameters.getParameter(RawDataImportParameters.removePrefix).getValue(); final String removeSuffix = parameters.getParameter(RawDataImportParameters.removeSuffix).getValue(); if (fileNames == null) { logger.warn("Feature table import module started with no filenames"); return;// ww w .j a v a 2 s .co m } for (File fileName : fileNames) { if ((!fileName.exists()) || (!fileName.canRead())) { MZmineGUI.displayMessage("Cannot read file " + fileName); logger.warn("Cannot read file " + fileName); continue; } DataPointStore dataStore = DataPointStoreFactory.getTmpFileDataStore(); // Find file extension and initiate corresponding import method String fileExtension = FilenameUtils.getExtension(fileName.getAbsolutePath()); MSDKMethod<?> method = null; switch (fileExtension.toUpperCase()) { default: case "CSV": method = new CsvFileImportMethod(fileName, dataStore); break; case "MZTAB": method = new MzTabFileImportMethod(fileName, dataStore); break; } final MSDKMethod<?> finalMethod = method; MSDKTask newTask = new MSDKTask("Importing feature table file", fileName.getName(), finalMethod); newTask.setOnSucceeded(e -> { FeatureTable featureTable = (FeatureTable) finalMethod.getResult(); if (featureTable == null) return; // Remove common prefix if (!Strings.isNullOrEmpty(removePrefix)) { String name = featureTable.getName(); if (removePrefix != null && name.startsWith(removePrefix)) name = name.substring(removePrefix.length()); featureTable.setName(name); } // Remove common suffix if (!Strings.isNullOrEmpty(removeSuffix)) { String suffix = removeSuffix; if (suffix.equals(".*")) suffix = "." + fileExtension; String name = featureTable.getName(); if (name.endsWith(suffix)) name = name.substring(0, name.length() - suffix.length()); featureTable.setName(name); } project.addFeatureTable(featureTable); }); tasks.add(newTask); } }
From source file:com.validation.manager.core.server.core.AttachmentServer.java
public void addFile(File f, String fileName) { try {/* ww w. ja v a 2 s . c om*/ if (f != null && f.isFile() && f.exists()) { byte[] array = FileUtils.readFileToByteArray(f); setFile(array); setFileName(fileName); String ext = FilenameUtils.getExtension(getFileName()); if (ext != null) { //Set attachment type setAttachmentType(AttachmentTypeServer.getTypeForExtension(ext)); } if (getAttachmentType() == null) { //Set as undefined setAttachmentType(AttachmentTypeServer.getTypeForExtension("")); } } } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:com.jaeksoft.searchlib.crawler.file.database.FileInfo.java
public FileInfo(FileInstanceAbstract fileInstance) throws SearchLibException { init();/*from ww w . jav a2 s . com*/ setUriFileNameExtension(fileInstance.getURI()); URI uri = fileInstance.getURI(); setUri(uri.toASCIIString()); setFileName(fileInstance.getFileName()); setFileExtension(FilenameUtils.getExtension(fileName)); setFileSystemDate(fileInstance.getLastModified()); setFileSize(fileInstance.getFileSize()); setFileType(fileInstance.getFileType()); }
From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java
/** * Gets the shp file./*w w w .j a va 2 s . co m*/ * * @param tmpDestDir * the tmp dest dir * @return the shp file */ private static File getShpFile(File tmpDestDir) { File[] files = tmpDestDir.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return FilenameUtils.getExtension(name).equalsIgnoreCase("shp"); } }); return (files.length > 0) ? files[0] : null; }
From source file:com.tocsi.images.ReceivedImageController.java
public void handleFileUpload(FileUploadEvent event) { if (event.getFile() != null) { try {/*from ww w. j ava 2 s . c o m*/ UploadedFile uf = (UploadedFile) event.getFile(); File folder = new File(GlobalsBean.destOriginal); //File folderThumb = new File(destThumbnail); String filename = FilenameUtils.getBaseName(uf.getFileName()); String extension = FilenameUtils.getExtension(uf.getFileName()); File file = File.createTempFile(filename + "-", "." + extension, folder); //File fileThumb = File.createTempFile(filename + "-", "." + extension, folderThumb); //resize image here BufferedImage originalImage = ImageIO.read(uf.getInputstream()); InputStream is = uf.getInputstream(); if (originalImage.getWidth() > 700) { //resize image if image's width excess 700px BufferedImage origImage = Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH, 640, (int) ((originalImage.getHeight() / ((double) (originalImage.getWidth() / 700.0)))), Scalr.OP_ANTIALIAS); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(origImage, extension, os); is = new ByteArrayInputStream(os.toByteArray()); } //create thumbnail BufferedImage thumbnail = Scalr.resize(ImageIO.read(uf.getInputstream()), 150); ByteArrayOutputStream osThumb = new ByteArrayOutputStream(); ImageIO.write(thumbnail, extension, osThumb); InputStream isThumbnail = new ByteArrayInputStream(osThumb.toByteArray()); try (InputStream input = is) { Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING); } try (InputStream input = isThumbnail) { Files.copy(input, Paths.get(GlobalsBean.destThumbnail + GlobalsBean.separator + file.getName()), StandardCopyOption.REPLACE_EXISTING); } //File chartFile = new File(file.getAbsolutePath()); //chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/jpg"); ImageBean ib = new ImageBean(); ib.setFilename(file.getName()); ib.setThumbFile(file.getName()); ib.setImageFileLocation(GlobalsBean.destOriginal + GlobalsBean.separator + file.getName()); imageList.add(ib); } catch (IOException | IllegalArgumentException | ImagingOpException ex) { Utilities.displayError(ex.getMessage()); } } }
From source file:com.quattroresearch.antibody.plugin.PluginLoader.java
private List<File> findFilesInDirRecursively(File[] directory) { List<File> filesInDir = new LinkedList<File>(); for (File file : directory) { if (FilenameUtils.getExtension(file.getPath()).equals("jar")) { filesInDir.add(file);/* www. j a v a2s. c om*/ } if (file.isDirectory()) { filesInDir.addAll(findFilesInDirRecursively(file.listFiles())); } } return filesInDir; }
From source file:filehandling.FileUploadBean.java
public void fileUploadListener(FileUploadEvent e) { UploadedFile file = e.getFile();//from w w w . ja v a 2 s. c om if (Master.DEBUG_LEVEL > Master.LOW) System.out.println("Uploaded file is " + file.getFileName() + " (" + file.getSize() + ")"); String filename = FilenameUtils.getBaseName(file.getFileName()); String extension = FilenameUtils.getExtension(file.getFileName()); extension = extension.equals("") ? "xml" : extension; byte[] bytes = file.getContents(); List<FileData> result = FilePreprocessor.extractMDFilesFromXMLEmbedding(bytes, filename, extension); // this.addFilesToDisplayList(result); //!!!!! this.uploadedSth = true; if (fireOpenEvent) { StringBuilder sb = new StringBuilder(); for (FileData fd : result) { if (sb.length() != 0) { sb.append(','); } sb.append(fd.getDisplayname()); } sb.insert(0, "fireBasicEvent(\'MDEuploadedMD\' ,{filename:\'["); sb.append("]\'});"); String command = sb.toString(); if (Master.DEBUG_LEVEL > Master.LOW) System.out.println(command); RequestContext.getCurrentInstance().execute(command); } RequestContext.getCurrentInstance().update(GUIrefs.getClientId(GUIrefs.fileDispDlg)); GUIrefs.updateComponent(GUIrefs.filesToShow); // String command = "doFireEvent(\'file upload event\',\'" + // file.getFileName() + "\' );"; // RequestContext.getCurrentInstance().execute(command); }
From source file:com.opendoorlogistics.speedregions.excelshp.app.FileBrowserPanel.java
private static boolean matchesFilter(FileNameExtensionFilter filter, String path) { String[] exts = filter.getExtensions(); for (String ext : exts) { // check if it already has a valid extension if (TextUtils.equalsStd(FilenameUtils.getExtension(path), ext)) { return true; }/*from ww w . j a v a 2 s .c om*/ } return false; }
From source file:eu.esdihumboldt.hale.common.core.io.HaleIO.java
/** * Find the content types that match the given file name and/or input. * //from ww w. j ava 2 s . c om * NOTE: The implementation should try to restrict the result to one content * type and only use the input supplier if absolutely needed. * * @param types the types to match * @param in the input supplier to use for testing, may be <code>null</code> * if the file name is not <code>null</code> * @param filename the file name, may be <code>null</code> if the input * supplier is not <code>null</code> * @return the matched content types */ public static List<IContentType> findContentTypesFor(Collection<IContentType> types, InputSupplier<? extends InputStream> in, String filename) { Preconditions.checkArgument(filename != null || in != null, "At least one of input supplier and file name must not be null"); List<IContentType> results = new ArrayList<IContentType>(); if (filename != null) { // test file extension for (IContentType type : types) { String ext = FilenameUtils.getExtension(filename); if (ext != null && !ext.isEmpty()) { String[] extensions = type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); boolean match = false; for (int i = 0; i < extensions.length && !match; i++) { if (extensions[i].equalsIgnoreCase(ext)) { match = true; } } if (match) { results.add(type); } } } } if ((results.isEmpty() || results.size() > 1) && in != null) { // clear results because only an ambiguous result was found results.clear(); // use input stream to make a better test IContentTypeManager ctm = Platform.getContentTypeManager(); try { InputStream is = in.getInput(); /* * XXX IContentTypeManager.findContentTypes seems to return all * kind of content types that match in any way, but ordered by * relevance - so if all but the allowed types are removed, the * remaining types may be very irrelevant and not a match that * actually was determined based on the input stream. * * XXX thus findContentTypesFor should not be used or only * relied upon the single best match that is returned */ // IContentType[] candidates = ctm.findContentTypesFor(is, filename); // // for (IContentType candidate : candidates) { // if (types.contains(candidate)) { // results.add(candidate); // } // } // instead use findContentTypeFor IContentType candidate = ctm.findContentTypeFor(is, null); if (types.contains(candidate)) { results.add(candidate); } is.close(); } catch (IOException e) { log.warn("Could not read input to determine content type", e); } } return results; }