List of usage examples for org.apache.commons.io FilenameUtils getName
public static String getName(String filename)
From source file:com.haulmont.cuba.core.controllers.LogDownloadController.java
@RequestMapping(value = "/log/{file:[a-zA-Z0-9\\.\\-_]+}", method = RequestMethod.GET) public void getLogFile(HttpServletResponse response, @RequestParam(value = "s") String sessionId, @RequestParam(value = "full", required = false) Boolean downloadFull, @PathVariable(value = "file") String logFileName) throws IOException { UserSession userSession = getSession(sessionId, response); if (userSession == null) return;//from w ww .j ava 2 s. c om if (!userSession.isSpecificPermitted("cuba.gui.administration.downloadlogs")) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } // security check, handle only valid file name String filename = FilenameUtils.getName(logFileName); try { File logFile = logControl.getLogFile(filename); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setHeader("Content-Type", "application/zip"); response.setHeader("Pragma", "no-cache"); response.setHeader("Content-Disposition", "attachment; filename=" + filename); OutputStream outputStream = null; try { outputStream = response.getOutputStream(); if (BooleanUtils.isTrue(downloadFull)) { LogArchiver.writeArchivedLogToStream(logFile, outputStream); } else { LogArchiver.writeArchivedLogTailToStream(logFile, outputStream); } } catch (RuntimeException | IOException ex) { log.error("Unable to download file", ex); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { IOUtils.closeQuietly(outputStream); } } catch (LogFileNotFoundException e) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } }
From source file:com.silverpeas.util.ZipManager.java
/** * Compress a file into a zip file./*from w w w .j a v a2 s . c om*/ * * @param filePath * @param zipFilePath * @return * @throws IOException */ public static long compressFile(String filePath, String zipFilePath) throws IOException { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath)); InputStream in = new FileInputStream(filePath); try { // cration du flux zip zos = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath)); zos.setFallbackToUTF8(true); zos.setCreateUnicodeExtraFields(NOT_ENCODEABLE); zos.setEncoding(CharEncoding.UTF_8); String entryName = FilenameUtils.getName(filePath); entryName = entryName.replace(File.separatorChar, '/'); zos.putArchiveEntry(new ZipArchiveEntry(entryName)); IOUtils.copy(in, zos); zos.closeArchiveEntry(); return new File(zipFilePath).length(); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(zos); } }
From source file:de.sanandrew.mods.turretmod.registry.assembly.TurretAssemblyRecipes.java
private static boolean processJson(Path file, Ex2Function<JsonObject, Boolean, JsonParseException, IOException> callback) { if (!"json".equals(FilenameUtils.getExtension(file.toString())) || FilenameUtils.getName(file.toString()).startsWith("_")) { return true; }// w w w. j a v a 2 s . co m try (BufferedReader reader = Files.newBufferedReader(file)) { JsonObject json = JsonUtils.fromJson(reader, JsonObject.class); if (json == null || json.isJsonNull()) { throw new JsonSyntaxException("Json cannot be null"); } callback.apply(json); return true; } catch (JsonParseException e) { TmrConstants.LOG.log(Level.ERROR, String.format("Parsing error loading assembly table recipe from %s", file), e); return false; } catch (IOException e) { TmrConstants.LOG.log(Level.ERROR, String.format("Couldn't read recipe from %s", file), e); return false; } }
From source file:edu.si.services.sidora.rest.batch.MimeTypeTest.java
@Test public void mimeTypeTest() throws URISyntaxException { String string = "test-data/mimetype-test-files/Canon5DMkII_50mm_100_f4_001.dng"; URI path2 = new URI(string); System.out//from www . jav a 2s . c o m .println(FilenameUtils.getName(path2.getPath()) + " || MIME=" + new Tika().detect(path2.getPath())); }
From source file:com.servoy.j2db.server.ngclient.startup.resourceprovider.ComponentResourcesExporter.java
/** * @param path// w w w. ja v a2s . c o m * @param tmpWarDir * @throws IOException */ private static void copy(Enumeration<String> paths, File destDir) throws IOException { if (paths != null) { while (paths.hasMoreElements()) { String path = paths.nextElement(); if (path.endsWith("/")) { File targetDir = new File(destDir, FilenameUtils.getName(path.substring(0, path.lastIndexOf("/")))); copy(Activator.getContext().getBundle().getEntryPaths(path), targetDir); } else { URL entry = Activator.getContext().getBundle().getEntry(path); FileUtils.copyInputStreamToFile(entry.openStream(), new File(destDir, FilenameUtils.getName(path))); } } } }
From source file:bogdan.rechi.swt.commons.SwtFiles.java
/** * Browse for files.//from w w w .j a v a2s . c o m * * @param parent * Parent shell. * @param initialFolder * Folder to start the browsing from, or null to use the internal mechanism. * @param initialFile * Initially selected file. * @param title * Browsing window title. * @param filters * Files' names filters. * @param filterNames * Filter names. * @param style * Must contain SWT.MULTI or SWT.SINGLE. * * @return Selected files or null. */ public static String[] browseForFiles(Shell parent, String initialFolder, String initialFile, String title, String[] filters, String[] filterNames, int style) { FileDialog dlg = new FileDialog(parent == null ? SwtGeneral.CurrentApplicationShell : parent, style); dlg.setText(title); if (Files.exists(initialFolder)) if (!Files.isFolder(initialFolder)) { if (initialFile == null) initialFile = FilenameUtils.getName(initialFolder); initialFolder = Files.getParent(initialFolder); } if (initialFile != null) dlg.setFileName(initialFile); dlg.setFilterPath(initialFolder == null ? _initialFolder : initialFolder); dlg.setFilterExtensions(filters); dlg.setFilterNames(filterNames); if (dlg.open() != null) { String[] files = (style == SWT.MULTI ? dlg.getFileNames() : new String[] { dlg.getFileName() }); _initialFolder = dlg.getFilterPath(); for (int i = 0; i < files.length; i++) files[i] = _initialFolder + Files.FILE_SEPARATOR + files[i]; if (Platform.SYSTEM_IS_LINUX && (style & SWT.SAVE) != 0) { String[] extSplit = filters[dlg.getFilterIndex()].split("\\."); if (extSplit.length == 2) if (!files[0].endsWith("." + extSplit[1])) files[0] = files[0] + "." + extSplit[1]; } return files; } return null; }
From source file:me.xingrz.finder.ZipFinderActivity.java
@Override protected void onCreateInternal(Bundle savedInstanceState) { super.onCreateInternal(savedInstanceState); current = new File(getIntent().getData().getPath()); try {//from w w w . java 2 s. c o m zipFile = new ZipFile(current); zipFile.setRunInThread(true); } catch (ZipException e) { Log.d(TAG, "failed to open zip file " + current.getAbsolutePath(), e); } if (getIntent().hasExtra(EXTRA_PREFIX)) { toolbar.setTitle(FilenameUtils.getName(getIntent().getStringExtra(EXTRA_PREFIX))); toolbar.setSubtitle(current.getName()); } passwordPrompt = new AlertDialog.Builder(this).setView(R.layout.dialog_password) .setPositiveButton("", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { confirmFilePassword(); } }).create(); progressMonitor = zipFile.getProgressMonitor(); progressDialog = new ProgressDialog(this); progressDialog.setMax(100); progressDialog.setCancelable(false); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); }
From source file:com.intuit.tank.util.UploadedFileIterator.java
private void moveNext() { if (in != null) { try {/*from ww w . j a v a 2s . co m*/ entry = (ZipArchiveEntry) in.getNextEntry(); while (entry != null) { if (!entry.getName().startsWith("_") && !entry.getName().startsWith(".") && isValid(entry.getName())) { next = new FileInputStreamWrapper(FilenameUtils.getName(entry.getName()), in); return; } entry = (ZipArchiveEntry) in.getNextEntry(); } } catch (IOException e) { LOG.warn("Error in zip: " + e); } in.forceClose(); } }
From source file:com.haulmont.cuba.web.controllers.LogDownloadController.java
@RequestMapping(value = "/log/{file:[a-zA-Z0-9\\.\\-_]+}", method = RequestMethod.GET) public void getLogFile(HttpServletResponse response, @RequestParam(value = "s") String sessionId, @RequestParam(value = "full", required = false) Boolean downloadFull, @PathVariable(value = "file") String logFileName) throws IOException { UserSession userSession = getSession(sessionId, response); if (userSession == null) return;//from w w w. j ava2s . c o m if (!userSession.isSpecificPermitted("cuba.gui.administration.downloadlogs")) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } // security check, handle only valid file name String filename = FilenameUtils.getName(logFileName); try { File logFile = logControl.getLogFile(filename); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setHeader("Content-Type", "application/zip"); response.setHeader("Pragma", "no-cache"); response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".zip"); OutputStream outputStream = null; try { outputStream = response.getOutputStream(); if (BooleanUtils.isTrue(downloadFull)) { LogArchiver.writeArchivedLogToStream(logFile, outputStream); } else { LogArchiver.writeArchivedLogTailToStream(logFile, outputStream); } } catch (RuntimeException | IOException ex) { log.error("Unable to download file", ex); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { IOUtils.closeQuietly(outputStream); } } catch (LogFileNotFoundException e) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } }
From source file:com.sencha.gxt.examples.resources.server.RelativeRemoteServiceServlet.java
/** * This serialization policy implementation loads from a server relative url * rather than from the fully qualified module base url provided by the client. * * @see RemoteServiceServlet#doGetSerializationPolicy(HttpServletRequest, String, String) *///from w w w. j a v a2 s .c o m @Override protected SerializationPolicy doGetSerializationPolicy(HttpServletRequest request, String moduleBaseURL, String strongName) { // extract the module base name, which is always the last portion of the fully qualified url String moduleBaseName = FilenameUtils.getName(FilenameUtils.getPathNoEndSeparator(moduleBaseURL)); // construct a server relative url yes, this is legal: https://en.wikipedia.org/wiki/Uniform_Resource_Locator String moduleRelativeURL = request.getScheme() + ":" + request.getContextPath() + "/" + moduleBaseName + "/"; // load as normal, but using our server relative url instead of the fully qualified url return super.doGetSerializationPolicy(request, moduleRelativeURL, strongName); }