List of usage examples for java.io File createTempFile
public static File createTempFile(String prefix, String suffix) throws IOException
From source file:be.idamediafoundry.sofa.livecycle.dsc.util.DelegatingComponentGeneratorTest.java
@Test public void test() throws Exception { File file = File.createTempFile("test", "xml"); File original = new File(this.getClass().getResource("/base/base-component.xml").getFile()); generator.generateComponentXML(original, file); System.out.println(FileUtils.readFileToString(file)); }
From source file:com.amazonaws.util.Md5UtilsTest.java
@Test public void testFile() throws Exception { File f = File.createTempFile("Md5UtilsTest-", "txt"); f.deleteOnExit();// www . j a v a 2 s .c o m FileUtils.writeStringToFile(f, "Testing MD5"); byte[] md5 = Md5Utils.computeMD5Hash(f); assertEquals("0b4f503b8eb7714ce12402406895cf68", StringUtils.lowerCase(Base16.encodeAsString(md5))); String b64 = Md5Utils.md5AsBase64(f); assertEquals("C09QO463cUzhJAJAaJXPaA==", b64); }
From source file:de.mpg.imeji.logic.storage.util.MediaUtils.java
/** * User imagemagick to convert any image into a jpeg * /*from w w w. j a v a2s . c o m*/ * @param bytes * @param extension * @throws IOException * @throws URISyntaxException * @throws InterruptedException * @throws IM4JavaException */ public static byte[] convertToJPEG(File tmp, String extension) throws IOException, URISyntaxException, InterruptedException, IM4JavaException { // In case the file is made of many frames, (for instance videos), generate only the frames from 0 to 48 to // avoid high memory consumption String path = tmp.getAbsolutePath() + "[0-48]"; ConvertCmd cmd = getConvert(); // create the operation, add images and operators/options IMOperation op = new IMOperation(); if (isImage(extension)) op.colorspace(findColorSpace(tmp)); op.strip(); op.flatten(); op.addImage(path); // op.colorspace("RGB"); File jpeg = File.createTempFile("uploadMagick", ".jpg"); try { op.addImage(jpeg.getAbsolutePath()); cmd.run(op); int frame = getNonBlankFrame(jpeg.getAbsolutePath()); if (frame >= 0) { File f = new File(FilenameUtils.getFullPath(jpeg.getAbsolutePath()) + FilenameUtils.getBaseName(jpeg.getAbsolutePath()) + "-" + frame + ".jpg"); return FileUtils.readFileToByteArray(f); } return FileUtils.readFileToByteArray(jpeg); } finally { removeFilesCreatedByImageMagick(jpeg.getAbsolutePath()); FileUtils.deleteQuietly(jpeg); } }
From source file:it.attocchi.utils.HttpClientUtils.java
/** * /*from w ww . j a v a 2 s . com*/ * @param url * un url dove scaricare il file * @param urlFileNameParam * specifica il parametro nell'url che definisce il nome del * file, se non specificato il nome corrisponde al nome nell'url * @param dest * dove salvare il file, se non specificato crea un file * temporaneo * @return * @throws ClientProtocolException * @throws IOException */ public static File getFile(String url, String urlFileNameParam, File dest) throws ClientProtocolException, IOException { DefaultHttpClient httpclient = null; HttpGet httpget = null; HttpResponse response = null; HttpEntity entity = null; try { httpclient = new DefaultHttpClient(); url = url.trim(); /* Per prima cosa creiamo il File */ String name = getFileNameFromUrl(url, urlFileNameParam); String baseName = FilenameUtils.getBaseName(name); String extension = FilenameUtils.getExtension(name); if (dest == null) dest = File.createTempFile(baseName + "_", "." + extension); /* Procediamo con il Download */ httpget = new HttpGet(url); response = httpclient.execute(httpget); entity = response.getEntity(); if (entity != null) { OutputStream fos = null; try { // var fos = new // java.io.FileOutputStream('c:\\temp\\myfile.ext'); fos = new java.io.FileOutputStream(dest); entity.writeTo(fos); } finally { if (fos != null) fos.close(); } logger.debug(fos); } } catch (Exception ex) { logger.error(ex); } finally { // org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpClient); // if (entity != null) // EntityUtils.consume(entity); // try { if (httpclient != null) httpclient.getConnectionManager().shutdown(); } catch (Exception ex) { logger.error(ex); } } return dest; }
From source file:au.org.ala.delta.intkey.model.DisplayInputTest.java
@Test public void testDisplayInput() throws Exception { IntkeyContext context = loadDataset("/dataset/sample/intkey.ink"); for (Character ch : context.getDataset().getCharactersAsList()) { if (!(ch instanceof MultiStateCharacter)) { System.out.println(ch.getCharacterId()); }/* ww w.j a v a 2s . co m*/ } File tempLogFile = File.createTempFile("DisplayInputTest", null); tempLogFile.deleteOnExit(); context.setLogFile(tempLogFile); File tempJournalFile = File.createTempFile("DisplayInputTest", null); tempJournalFile.deleteOnExit(); context.setJournalFile(tempJournalFile); URL directiveFileUrl = getClass().getResource("/input/test_directives_file.txt"); File directivesFile = new File(directiveFileUrl.toURI()); // first, process the sample file as a preferences file, and ensure that // the directive call is not output // to the journal or log new PreferencesDirective().parseAndProcess(context, directivesFile.getAbsolutePath()); String logFileAsString = FileUtils.readFileToString(tempLogFile); assertFalse(logFileAsString.contains("*DEFINE CHARACTERS preferencestest 1")); String journalFileAsString = FileUtils.readFileToString(tempJournalFile); assertFalse(journalFileAsString.contains("*DEFINE CHARACTERS preferencestest 1")); // now, process the sample file as an input file. As DISPLAY INPUT has // not been set to ON, // the directive call should still not turn up in the log or journal // files new FileInputDirective().parseAndProcess(context, directivesFile.getAbsolutePath()); logFileAsString = FileUtils.readFileToString(tempLogFile); assertFalse(logFileAsString.contains("*DEFINE CHARACTERS preferencestest 1")); journalFileAsString = FileUtils.readFileToString(tempJournalFile); assertFalse(journalFileAsString.contains("*DEFINE CHARACTERS preferencestest 1")); // now set DISPLAY INPUT to ON, and process the directives file as an input file again. new DisplayInputDirective().parseAndProcess(context, "ON"); new FileInputDirective().parseAndProcess(context, directivesFile.getAbsolutePath()); logFileAsString = FileUtils.readFileToString(tempLogFile); assertTrue(logFileAsString.contains("*DEFINE CHARACTERS preferencestest 1")); journalFileAsString = FileUtils.readFileToString(tempJournalFile); assertTrue(journalFileAsString.contains("*DEFINE CHARACTERS preferencestest 1")); }
From source file:com.adaptris.util.datastore.TestSimpleDataStore.java
private static Properties createProperties() throws IOException { Properties sp = new Properties(); File dataFile = File.createTempFile("junitsds", ".dat"); File lockFile = File.createTempFile("junitsds", ".lock"); dataFile.delete();//from w w w. ja v a 2s . c o m lockFile.delete(); sp.setProperty(SimpleDataStore.FILE_PROPERTY, dataFile.getCanonicalPath()); sp.setProperty(SimpleDataStore.LOCK_PROPERTY, lockFile.getCanonicalPath()); sp.setProperty(SimpleDataStore.MAXLOCK_PROPERTY, "10"); return sp; }
From source file:net.bpelunit.util.XMLUtilTest.java
@Test public void testWriteXMLToFile() throws Exception { Document doc = XMLUtil.parseXML(getClass().getResourceAsStream("simple.xml")); File f = File.createTempFile("bpelunit", ".xml"); try {/*from w ww . j a v a 2 s . c om*/ XMLUtil.writeXML(doc, f); ByteArrayOutputStream reference = new ByteArrayOutputStream(); IOUtils.copy(getClass().getResourceAsStream("simple.xml"), reference); byte[] actual = FileUtil.readFile(f); String referenceString = normalize(reference.toString("UTF-8")); String actualString = normalize(new String(actual)); assertEquals(referenceString, actualString); } finally { f.delete(); } }
From source file:eu.planets_project.tb.impl.serialization.ExperimentViaJAXB.java
/** * A deep copy experiment that used the JAXB serialisation to perform a copy. * @param exp The source experiment./*from w ww.ja v a 2 s . c o m*/ * @return A new Experiment, detached from the DB with no XmlTransient data set. */ public static ExperimentImpl deepCopy(ExperimentImpl exp) { try { File temp = File.createTempFile("tb-experiment", ".xml"); ExperimentViaJAXB.writeToFile(exp, temp); ExperimentImpl exp2 = ExperimentViaJAXB.readFromFile(temp); temp.delete(); return exp2; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:no.dusken.aranea.web.control.CaptchaController.java
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { String captchaID = ServletRequestUtils.getRequiredStringParameter(request, "captchaID"); BufferedImage image = imageCaptchaService.getImageChallengeForID(captchaID); Map<String, Object> map = new HashMap<String, Object>(); File tmpFile = File.createTempFile("temp", "jpg"); // TODO: Is there a better way? ImageIO.write(image, "JPG", tmpFile); map.put("file", tmpFile); map.put("deleteFile", true); return new ModelAndView("fileView", map); }
From source file:com.creactiviti.piper.plugin.ffmpeg.Mediainfo.java
@Override public Map<String, Object> handle(Task aTask) throws Exception { CommandLine cmd = new CommandLine("mediainfo"); cmd.addArgument(aTask.getRequiredString("input")); log.debug("{}", cmd); DefaultExecutor exec = new DefaultExecutor(); File tempFile = File.createTempFile("log", null); try (PrintStream stream = new PrintStream(tempFile);) { exec.setStreamHandler(new PumpStreamHandler(stream)); exec.execute(cmd);//from www . ja v a 2 s.c o m return parse(FileUtils.readFileToString(tempFile)); } catch (ExecuteException e) { throw new ExecuteException(e.getMessage(), e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile))); } finally { FileUtils.deleteQuietly(tempFile); } }