Java Code Examples for org.springframework.core.io.ClassPathResource#getFile()

The following examples show how to use org.springframework.core.io.ClassPathResource#getFile() . 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: RsaUtils.java    From spring-boot-vue-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 加载文件后替换头和尾并解密
 *
 * @return 文件字节
 */
private byte[] replaceAndBase64Decode(
    final String filePath, final String headReplace, final String tailReplace) throws Exception {
  // 从 classpath:resources/ 中加载资源
  final ClassPathResource resource = new ClassPathResource(filePath);
  if (!resource.exists()) {
    throw new Exception("公私钥文件找不到");
  }
  final byte[] keyBytes = new byte[(int) resource.getFile().length()];
  final FileInputStream in = new FileInputStream(resource.getFile());
  in.read(keyBytes);
  in.close();

  final String keyPEM =
      new String(keyBytes).replace(headReplace, "").trim().replace(tailReplace, "").trim();

  return Base64.decodeBase64(keyPEM);
}
 
Example 2
Source File: ClassPathFileUtil.java    From spring-boot-assembly with Apache License 2.0 6 votes vote down vote up
/**
 * 获取文件
 * @param fileName 文件名称
 * @return
 */
public static File getFile(String fileName) throws IOException {
    // 获取当前执行启动命令的路径
    String userDir = System.getProperty(USER_DIR);
    System.out.println("user.dir:"+userDir);

    File targetFile;
    // 打包后启动时,路径以\\bin结束:E:\\github\\spring-boot-assembly\\target\\spring-boot-assembly\\bin
    // 执行startup.bat或者startup.sh命令的路径,默认目录是bin,如果不是,请修改
    if (userDir.endsWith(BIN)){
        File file = new File(userDir);
        // 获取E:\\github\\spring-boot-assembly
        File parentFile = file.getParentFile();
        // 获取配置文件目录,默认配置文件目录是config,如果不是,请修改
        File configDir = new File(parentFile,CONFIG);
        // 目标文件
        targetFile = new File(configDir,fileName);
    }else{
        // 工具中启动
        // E:\github\spring-boot-assembly\target\classes\test.jks
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        targetFile = classPathResource.getFile();
    }
    System.out.println("targetFile = " + targetFile);
    return targetFile;
}
 
Example 3
Source File: CustomerItApplicationTests.java    From bootiful-testing-online-training with Apache License 2.0 6 votes vote down vote up
@SneakyThrows
private File locationOfJar() {

	ClassPathResource properties = new ClassPathResource("/application.properties");
	Assert.assertTrue("the manifest.yml doesn't exist.", properties.exists());

	File path = properties.getFile();
	File parentFile = path;
	while (true) {
		parentFile = parentFile.getParentFile();
		if (parentFile.getName().equalsIgnoreCase("integration-tests")) {
			break;
		}
	}
	Assert.assertNotNull(parentFile);
	File jarTargetDir = new File(parentFile, "customer-service/target/");
	File[] files = jarTargetDir.listFiles(file -> file.isFile() && file.getName().toLowerCase().endsWith(".jar"));
	Assert.assertTrue(files.length > 0);
	return files[0];
}
 
Example 4
Source File: TomcatConfig.java    From enhanced-pet-clinic with Apache License 2.0 5 votes vote down vote up
private File getKeyStoreFile() throws IOException {
	ClassPathResource resource = new ClassPathResource(sslKeystoreFile);
	try {
		return resource.getFile();
	} catch (Exception ex) {
		File temp = File.createTempFile("keystore", ".tmp");
		FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream(temp));
		return temp;
	}
}
 
Example 5
Source File: LoadTester.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void importTestData(String acpName, NodeRef space) throws IOException
{
    ClassPathResource acpResource = new ClassPathResource(acpName);
    ACPImportPackageHandler acpHandler = new ACPImportPackageHandler(acpResource.getFile(), null);
    Location importLocation = new Location(space);
    importerService.importView(acpHandler, importLocation, null, null);
}
 
Example 6
Source File: ImapServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Test attachment extraction with a TNEF message
 * @throws Exception
 */
public void testAttachmentExtraction() throws Exception
{
    AuthenticationUtil.setRunAsUserSystem();
    /**
     * Load a TNEF message
     */
    ClassPathResource fileResource = new ClassPathResource("imap/test-tnef-message.eml");
    assertNotNull("unable to find test resource test-tnef-message.eml", fileResource);
    InputStream is = new FileInputStream(fileResource.getFile());
    MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), is);

    NodeRef companyHomeNodeRef = findCompanyHomeNodeRef();

    FileInfo f1 = fileFolderService.create(companyHomeNodeRef, "ImapServiceImplTest", ContentModel.TYPE_FOLDER);
    FileInfo f2 = fileFolderService.create(f1.getNodeRef(), "test-tnef-message.eml", ContentModel.TYPE_CONTENT);
    
    ContentWriter writer = fileFolderService.getWriter(f2.getNodeRef());
    writer.putContent(new FileInputStream(fileResource.getFile()));
    
    imapService.extractAttachments(f2.getNodeRef(), message);

    List<AssociationRef> targetAssocs = nodeService.getTargetAssocs(f2.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENTS_FOLDER);
    assertTrue("attachment folder is found", targetAssocs.size() == 1);
    NodeRef attachmentFolderRef = targetAssocs.get(0).getTargetRef();
    
    assertNotNull(attachmentFolderRef);

    List<FileInfo> files = fileFolderService.listFiles(attachmentFolderRef);
    assertTrue("three files not found", files.size() == 3);

}
 
Example 7
Source File: ImapServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void importInternal(String acpName, NodeRef space)
        throws IOException
{
    ClassPathResource acpResource = new ClassPathResource(acpName);
    ACPImportPackageHandler acpHandler = new ACPImportPackageHandler(acpResource.getFile(), null);
    Location importLocation = new Location(space);
    importerService.importView(acpHandler, importLocation, null, null);
}
 
Example 8
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void importInternal(String acpName, NodeRef space) throws IOException
{
    // Importing IMAP test acp
    ClassPathResource acpResource = new ClassPathResource(acpName);
    ACPImportPackageHandler acpHandler = new ACPImportPackageHandler(acpResource.getFile(), null);
    Location importLocation = new Location(space);
    importerService.importView(acpHandler, importLocation, null, null);
}
 
Example 9
Source File: FileTrustStoreSslSocketFactoryTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Test
public void verifyTrustStoreLoadingSuccessfullyWihInsecureEndpoint() throws Exception {
    final ClassPathResource resource = new ClassPathResource("truststore.jks");
    final FileTrustStoreSslSocketFactory factory = new FileTrustStoreSslSocketFactory(resource.getFile(), "changeit");
    final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
    clientFactory.setSslSocketFactory(factory);
    final HttpClient client = clientFactory.getObject();
    assertTrue(client.isValidEndPoint("http://wikipedia.org"));
}
 
Example 10
Source File: FileTrustStoreSslSocketFactoryTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Test
public void verifyTrustStoreLoadingSuccessfullyForValidEndpointWithNoCert() throws Exception {
    final ClassPathResource resource = new ClassPathResource("truststore.jks");
    final FileTrustStoreSslSocketFactory factory = new FileTrustStoreSslSocketFactory(resource.getFile(), "changeit");
    final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
    clientFactory.setSslSocketFactory(factory);
    final HttpClient client = clientFactory.getObject();
    assertTrue(client.isValidEndPoint("https://www.google.com"));
}
 
Example 11
Source File: FileTrustStoreSslSocketFactoryTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Test
public void verifyTrustStoreLoadingSuccessfullyWithCertAvailable2() throws Exception {
    final ClassPathResource resource = new ClassPathResource("truststore.jks");
    final FileTrustStoreSslSocketFactory factory = new FileTrustStoreSslSocketFactory(resource.getFile(), "changeit");
    final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
    clientFactory.setSslSocketFactory(factory);
    final HttpClient client = clientFactory.getObject();
    assertTrue(client.isValidEndPoint("https://test.scaldingspoon.org/idp/shibboleth"));
}
 
Example 12
Source File: FileTrustStoreSslSocketFactoryTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Test
 public void verifyTrustStoreLoadingSuccessfullyWithCertAvailable() throws Exception {
    final ClassPathResource resource = new ClassPathResource("truststore.jks");
    final FileTrustStoreSslSocketFactory factory = new FileTrustStoreSslSocketFactory(resource.getFile(), "changeit");
    final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
    clientFactory.setSslSocketFactory(factory);
    final HttpClient client = clientFactory.getObject();
    assertTrue(client.isValidEndPoint("https://www.cacert.org"));
}
 
Example 13
Source File: FileTrustStoreSslSocketFactoryTests.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
@Test(expected = RuntimeException.class)
public void verifyTrustStoreBadPassword() throws Exception {
    final ClassPathResource resource = new ClassPathResource("truststore.jks");
    new FileTrustStoreSslSocketFactory(resource.getFile(), "invalid");
}
 
Example 14
Source File: CertificateTestUtil.java    From blackduck-alert with Apache License 2.0 4 votes vote down vote up
protected String readCertificateContents() throws IOException {
    ClassPathResource classPathResource = new ClassPathResource(CERTIFICATE_FILE_PATH);
    File jsonFile = classPathResource.getFile();
    return FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
}
 
Example 15
Source File: PolicyOverrideMessageBuilderTest.java    From blackduck-alert with Apache License 2.0 4 votes vote down vote up
private String getNotificationContentFromFile(String notificationJsonFileName) throws Exception {
    ClassPathResource classPathResource = new ClassPathResource(notificationJsonFileName);
    File jsonFile = classPathResource.getFile();
    return FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
}
 
Example 16
Source File: PolicyViolationMessageBuilderTest.java    From blackduck-alert with Apache License 2.0 4 votes vote down vote up
private String getNotificationContentFromFile(String notificationJsonFileName) throws Exception {
    ClassPathResource classPathResource = new ClassPathResource(notificationJsonFileName);
    File jsonFile = classPathResource.getFile();
    return FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
}
 
Example 17
Source File: VulnerabilityMessageBuilderTest.java    From blackduck-alert with Apache License 2.0 4 votes vote down vote up
private VulnerabilityNotificationView getNotificationView(String path) throws IOException {
    ClassPathResource classPathResource = new ClassPathResource(path);
    File jsonFile = classPathResource.getFile();
    String notificationString = Files.toString(jsonFile, Charset.defaultCharset());
    return gson.fromJson(notificationString, VulnerabilityNotificationView.class);
}
 
Example 18
Source File: ClusterConfigurationWithAuthenticationIntegrationTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
private static void resolveAndConfigureGeodeHome() throws IOException {

			ClassPathResource resource = new ClassPathResource("/geode-home");

			File resourceFile = resource.getFile();

			System.setProperty(GEODE_HOME_PROPERTY, resourceFile.getAbsolutePath());
		}