Java Code Examples for org.apache.commons.lang3.SystemUtils#getJavaIoTmpDir()

The following examples show how to use org.apache.commons.lang3.SystemUtils#getJavaIoTmpDir() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyPdfUtils.java    From spring-boot with Apache License 2.0 7 votes vote down vote up
/**
 * 获得自定义的空心字体 STCAIYUN.TTF,该字体已经制成为 jar,需要加入项目的 classpath
 * 经过测试,该空心字体作为 pdf 的水印,不会遮挡 pdf 原文,支持中文
 * 需要注意的是,空心字体不能太小,否则会看不清楚
 *
 * @return
 * @throws IOException
 */
private static PdfFont getPdfFont() throws IOException {

    //空心字体
    String fontName = "/STCAIYUN.TTF";
    String fontPath =
            SystemUtils.getJavaIoTmpDir()
                    + File.separator + MyConstants.JarTempDir + File.separator
                    + fontName;

    //如果已经拷贝过,就不用再拷贝了
    if (!Files.exists(Paths.get(fontPath))) {
        MyFileUtils.copyResourceFileFromJarLibToTmpDir(fontName);
    }
    return PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
}
 
Example 2
Source File: PrivateTmpLogger.java    From aceql-http with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void log() throws FileNotFoundException, IOException {

	File tempDir = SystemUtils.getJavaIoTmpDir();
	String tempKawanSoftDirStr = tempDir.toString();
	if (! tempKawanSoftDirStr.endsWith(File.separator)) {
	    tempKawanSoftDirStr+=File.separator;
	}
	
	tempKawanSoftDirStr += ".kawansoft";
	File tempKawanSoftDir = new File(tempKawanSoftDirStr);
	if (!tempKawanSoftDir.exists()) {
	    tempKawanSoftDir.mkdirs();
	}
	
	File file = new File(tempKawanSoftDirStr +  File.separator + "aceql_exceptions.log");
	
	// Security delete if more than 200 MB
	if (file.length() > MAX_LENGTH) {
	    file.delete();
	}
	
	try(OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
	    ServerSqlManager.writeLine(out, getNowFormatted() + " " + ExceptionUtils.getStackTrace(throwable));	
	}
	
    }
 
Example 3
Source File: MyPdfUtils.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 获取 pdfdecrypt.exe 文件路径
 *
 * @return
 * @throws IOException
 */
private static String getPdfPdfdecryptExec() {

    //命令行模式,只需要两个文件即可
    String exec1 = "/pdfdecrypt.exe";
    String exec2 = "/license.dat";

    String tempPath =
            SystemUtils.getJavaIoTmpDir()
                    + File.separator + MyConstants.JarTempDir + File.separator;

    String exec1Path = tempPath + exec1;
    String exec2Path = tempPath + exec2;

    //如果已经拷贝过,就不用再拷贝了
    if (!Files.exists(Paths.get(exec1Path)))
        MyFileUtils.copyResourceFileFromJarLibToTmpDir(exec1);

    if (!Files.exists(Paths.get(exec2Path)))
        MyFileUtils.copyResourceFileFromJarLibToTmpDir(exec2);


    return exec1Path;
}
 
Example 4
Source File: DataSourceController.java    From DBus with Apache License 2.0 5 votes vote down vote up
@GetMapping("/downloadExcleModel")
public ResponseEntity downloadExcleModel() throws Exception {
    File file = null;
    try {
        file = new File(SystemUtils.getJavaIoTmpDir(), "加表模板.xlsx");
        return service.downloadExcleModel(file);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw e;
    } finally {
        if (file != null && file.exists()) {
            file.deleteOnExit();
        }
    }
}
 
Example 5
Source File: MyFileUtils.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 拷贝 classpath 中依赖库 jar 资源中的文件到系统临时目录下
 * jar 文件是必须用 java 命令打包的。
 *
 * @param resourceName 待拷贝的 jar 中的文件名称
 *                     参数写法
 *                     "/license.dat" 在 jar 根目录下
 *                     "/abc/license.dat" 在 jar /abc/ 目录下
 * @return 拷贝完成后的文件
 */
public static File copyResourceFileFromJarLibToTmpDir(String resourceName) {
    // 拷贝资源文件到临时目录
    //  { "/license.dat", "/pdfdecrypt.exe", "/SkinMagic.dll" };
    // { "/STCAIYUN.TTF" };
    InputStream inputStream = getResourceFileInputStream(resourceName);

    if (inputStream == null) {
        log.info(resourceName + " not exist in jar liberary.");
        return null;
    }
    //在系统临时目录下建立文件夹,存放拷贝后的文件
    //建立 java_jar_source_temp 文件夹,不用随机数,否则创建文件夹过多
    String tempFilePath =
            SystemUtils.getJavaIoTmpDir()
                    + File.separator + MyConstants.JarTempDir + File.separator
                    + resourceName;

    File resourceFile = new File(tempFilePath);

    System.out.println("resource copy to :" + tempFilePath);

    try {
        // 拷贝资源文件到临时文件夹,每次都覆盖
        FileUtils.copyInputStreamToFile(inputStream, resourceFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        IOUtils.closeQuietly(inputStream);
        return null;
    }


    IOUtils.closeQuietly(inputStream);
    return resourceFile;
}
 
Example 6
Source File: IOUtils.java    From sejda with GNU Affero General Public License v3.0 5 votes vote down vote up
public static File createTemporaryFolder() {
    File baseDir = SystemUtils.getJavaIoTmpDir();
    String baseName = new StringBuilder(BUFFER_NAME).append(System.currentTimeMillis()).append("-").toString();

    for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) {
        File tempDir = new File(baseDir, baseName + counter);
        if (tempDir.mkdir()) {
            return tempDir;
        }
    }
    throw new IllegalStateException("Failed to create directory within " + TEMP_DIR_ATTEMPTS + " attempts (tried "
            + baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')');
}