/** * Simple method that creates temp file. */ public static File createTempFile() throws IOException { return createTempFile(JoddCore.tempFilePrefix, null, null, true); }
/** * Creates temporary directory. */ public static File createTempDirectory(String prefix, String suffix, File tempDir) throws IOException { File file = createTempFile(prefix, suffix, tempDir); file.delete(); file.mkdir(); return file; }
/** * Creates temporary file. * If <code>create</code> is set to <code>true</code> file will be * physically created on the file system. Otherwise, it will be created and then * deleted - trick that will make temp file exist only if they are used. */ public static File createTempFile(String prefix, String suffix, File tempDir, boolean create) throws IOException { File file = createTempFile(prefix, suffix, tempDir); file.delete(); if (create) { file.createNewFile(); } return file; }
/** * Creates temporary directory. * * @see #createTempFile(String, String, File) */ public static File createTempDirectory(final String prefix, final String suffix, final File tempDir) throws IOException { File file = createTempFile(prefix, suffix, tempDir); file.delete(); file.mkdir(); return file; }
/** * @see #createTempFile(String, String, File, boolean) */ public static File createTempFile() throws IOException { return createTempFile(tempPrefix(), null, null, true); }
/** * Creates temporary {@link File}. * * @param prefix The prefix string to be used in generating the file's * name; must be at least three characters long * @param suffix The suffix string to be used in generating the file's * name; may be {@code null}, in which case the * suffix {@code ".tmp"} will be used * @param tempDir The directory in which the file is to be created, or * {@code null} if the default temporary-file * directory is to be used * @param create If {@code create} is set to {@code true} {@link File} will be * physically created on the file system. Otherwise, it will be created and then * deleted - trick that will make temp {@link File} exist only if they are used. * @return File */ public static File createTempFile(final String prefix, final String suffix, final File tempDir, final boolean create) throws IOException { File file = createTempFile(prefix, suffix, tempDir); file.delete(); if (create) { file.createNewFile(); } return file; }
if (type == byte[].class) { try { File tempFile = FileUtil.createTempFile(); FileUtil.writeBytes(tempFile, (byte[])value); return tempFile; File tempFile = FileUtil.createTempFile(); FileUtil.writeString(tempFile, value.toString()); return tempFile;
try { in = fileUpload.getFileInputStream(); File tempFile = FileUtil.createTempFile(); FileUtil.writeStream(tempFile, in); return tempFile; if (type == byte[].class) { try { File tempFile = FileUtil.createTempFile(); FileUtil.writeBytes(tempFile, (byte[])value); return tempFile; File tempFile = FileUtil.createTempFile(); FileUtil.writeString(tempFile, value.toString()); return tempFile;
tempFile = FileUtil.createTempFile(JoddCore.tempFilePrefix, TMP_FILE_SUFFIX, uploadPath); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile)); if (data != null) {
@Test void file_not_exists() throws Exception { final File input = FileUtil.createTempFile("hello", ".jodd", new File(SystemUtil.info().getTempDir()), false); final boolean actual = FileUtil.isExistingFile(input); // asserts assertEquals(false, actual); }
@Test void check_against_created_text_file() throws Exception { final File input = FileUtil.createTempFile(); FileUtil.writeString(input, "jodd makes fun!"); final boolean actual = FileUtil.isBinary(input); // asserts assertEquals(false, actual); }
@Test void check_against_created_binary_file() throws Exception { final File input = FileUtil.createTempFile(); // first bytes of a zip / jar file FileUtil.writeBytes(input, new byte[] {0x50, 0x4b, 0x03, 0x04, 0x14, 0x20, 0x08, 0x08, 0x08, 0x20, 0x09, 0x76, 0x19, 0x45, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }); final boolean actual = FileUtil.isBinary(input); // asserts assertEquals(true, actual); }
@Test void testUpload() throws IOException { EchoTestServer echoTestServer = new EchoTestServer(); File file = FileUtil.createTempFile(); file.deleteOnExit(); FileUtil.writeString(file, "upload тест"); assertEquals("upload тест", FileUtil.readString(file)); HttpResponse response = HttpRequest .post("http://localhost:8081/hello") .form("id", "12") .form("file", file) .send(); assertEquals(200, response.statusCode()); assertEquals("OK", response.statusPhrase()); assertEquals("POST", echoTestServer.method); assertEquals("12", echoTestServer.params.get("id")); File uploadedFile = new File(echoTestServer.files.get("file").toString()); assertNotNull(uploadedFile); assertEquals("upload тест", FileUtil.readString(uploadedFile)); assertEquals("POST /hello", response.body()); echoTestServer.stop(); file.delete(); }
@Test void testUploadWithMonitor() throws IOException { EchoTestServer echoTestServer = new EchoTestServer(); File file = FileUtil.createTempFile(); file.deleteOnExit(); FileUtil.writeString(file, StringUtil.repeat('A', 1024)); final StringBuilder sb = new StringBuilder(); HttpResponse response = HttpRequest .post("http://localhost:8081/hello") .form("id", "12") .form("file", file) .monitor(new HttpProgressListener() { @Override public void transferred(int len) { sb.append(":" + len); } }) .send(); assertEquals(200, response.statusCode()); assertEquals("OK", response.statusPhrase()); echoTestServer.stop(); file.delete(); assertEquals(":0:512:1024:148", StringUtil.substring(sb.toString(), 0, -1)); }
@Test void testFileUpload() throws IOException { HttpRequest request = HttpRequest.get("http://jodd.org/?id=173"); request.header("User-Agent", "Scaly").form("one", "funny"); File tempFile = FileUtil.createTempFile(); tempFile.deleteOnExit(); FileUtil.writeString(tempFile, "qwerty"); request.form("two", tempFile); byte[] bytes = request.toByteArray(); // read HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes)); HttpMultiMap<?> httpParams2 = request2.form(); assertEquals(request.method(), request2.method()); assertEquals(request.path(), request2.path()); assertEquals(request.queryString(), request2.queryString()); assertEquals(request.header("User-Agent"), request2.header("User-Agent")); assertEquals(request.header("Content-Type"), request2.header("content-type")); assertEquals(request.header("Content-Length"), request2.header("content-length")); HttpMultiMap<?> params1 = request.form(); HttpMultiMap<?> params2 = request2.form(); assertEquals(params1.size(), params2.size()); assertEquals(params2.get("one"), params2.get("one")); FileUpload fu = (FileUpload) httpParams2.get("two"); assertEquals(6, fu.getSize()); String str = new String(fu.getFileContent()); assertEquals("qwerty", str); tempFile.delete(); }
/** * Creates temporary directory. * * @see #createTempFile(String, String, File) */ public static File createTempDirectory(final String prefix, final String suffix, final File tempDir) throws IOException { File file = createTempFile(prefix, suffix, tempDir); file.delete(); file.mkdir(); return file; }
/** * Creates temporary {@link File}. * * @param prefix The prefix string to be used in generating the file's * name; must be at least three characters long * @param suffix The suffix string to be used in generating the file's * name; may be {@code null}, in which case the * suffix {@code ".tmp"} will be used * @param tempDir The directory in which the file is to be created, or * {@code null} if the default temporary-file * directory is to be used * @param create If {@code create} is set to {@code true} {@link File} will be * physically created on the file system. Otherwise, it will be created and then * deleted - trick that will make temp {@link File} exist only if they are used. * @return File */ public static File createTempFile(final String prefix, final String suffix, final File tempDir, final boolean create) throws IOException { File file = createTempFile(prefix, suffix, tempDir); file.delete(); if (create) { file.createNewFile(); } return file; }
/** * @see #createTempFile(String, String, File, boolean) */ public static File createTempFile() throws IOException { return createTempFile(tempPrefix(), null, null, true); }
/** * Create Temp Jar file with given classpath in manifest * * @param mainClassName main class name for manifest * @param classPath classpath elements for manifest * @return {@link java.io.File} with created Jar * @throws IOException if can't create temp file */ public static File createJarWithClassPath(String mainClassName, String... classPath) throws IOException { return createJarWithClassPath(createTempFile(), mainClassName, classPath); }
tempFile = FileUtil.createTempFile(JoddCore.tempFilePrefix, TMP_FILE_SUFFIX, uploadPath); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile)); if (data != null) {