org.apache.commons.io.FileUtils.copyInputStreamToFile()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(13.9k)|赞(0)|评价(0)|浏览(1367)

本文整理了Java中org.apache.commons.io.FileUtils.copyInputStreamToFile()方法的一些代码示例,展示了FileUtils.copyInputStreamToFile()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils.copyInputStreamToFile()方法的具体详情如下:
包路径:org.apache.commons.io.FileUtils
类名称:FileUtils
方法名:copyInputStreamToFile

FileUtils.copyInputStreamToFile介绍

[英]Copies bytes from an InputStream source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
[中]将字节从输入流source复制到文件destination。如果目录不存在,则将创建高达destination的目录。destination如果已经存在,将被覆盖。

代码示例

代码示例来源:origin: gocd/gocd

public void handle(InputStream stream) throws IOException {
  FileUtils.copyInputStreamToFile(stream, checksumFile);
}

代码示例来源:origin: commons-io/commons-io

/**
 * Copies bytes from the URL <code>source</code> to a file
 * <code>destination</code>. The directories up to <code>destination</code>
 * will be created if they don't already exist. <code>destination</code>
 * will be overwritten if it already exists.
 * <p>
 * Warning: this method does not set a connection or read timeout and thus
 * might block forever. Use {@link #copyURLToFile(URL, File, int, int)}
 * with reasonable timeouts to prevent this.
 *
 * @param source      the <code>URL</code> to copy bytes from, must not be {@code null}
 * @param destination the non-directory <code>File</code> to write bytes to
 *                    (possibly overwriting), must not be {@code null}
 * @throws IOException if <code>source</code> URL cannot be opened
 * @throws IOException if <code>destination</code> is a directory
 * @throws IOException if <code>destination</code> cannot be written
 * @throws IOException if <code>destination</code> needs creating but can't be
 * @throws IOException if an IO error occurs during copying
 */
public static void copyURLToFile(final URL source, final File destination) throws IOException {
  copyInputStreamToFile(source.openStream(), destination);
}

代码示例来源:origin: selenide/selenide

protected File saveFileContent(HttpResponse response, File downloadedFile) throws IOException {
  copyInputStreamToFile(response.getEntity().getContent(), downloadedFile);
  return downloadedFile;
 }
}

代码示例来源:origin: commons-io/commons-io

/**
 * Copies bytes from the URL <code>source</code> to a file
 * <code>destination</code>. The directories up to <code>destination</code>
 * will be created if they don't already exist. <code>destination</code>
 * will be overwritten if it already exists.
 *
 * @param source            the <code>URL</code> to copy bytes from, must not be {@code null}
 * @param destination       the non-directory <code>File</code> to write bytes to
 *                          (possibly overwriting), must not be {@code null}
 * @param connectionTimeout the number of milliseconds until this method
 *                          will timeout if no connection could be established to the <code>source</code>
 * @param readTimeout       the number of milliseconds until this method will
 *                          timeout if no data could be read from the <code>source</code>
 * @throws IOException if <code>source</code> URL cannot be opened
 * @throws IOException if <code>destination</code> is a directory
 * @throws IOException if <code>destination</code> cannot be written
 * @throws IOException if <code>destination</code> needs creating but can't be
 * @throws IOException if an IO error occurs during copying
 * @since 2.0
 */
public static void copyURLToFile(final URL source, final File destination,
                 final int connectionTimeout, final int readTimeout) throws IOException {
  final URLConnection connection = source.openConnection();
  connection.setConnectTimeout(connectionTimeout);
  connection.setReadTimeout(readTimeout);
  copyInputStreamToFile(connection.getInputStream(), destination);
}

代码示例来源:origin: SonarSource/sonarqube

private static void downloadBinaryTo(InstalledPlugin plugin, File downloadedFile, WsResponse response) {
 try (InputStream stream = response.contentStream()) {
  FileUtils.copyInputStreamToFile(stream, downloadedFile);
 } catch (IOException e) {
  throw new IllegalStateException(format("Fail to download plugin [%s] into %s", plugin.key, downloadedFile), e);
 }
}

代码示例来源:origin: gocd/gocd

private void replaceFileWithPackagedOne(File jettyConfig) {
  InputStream inputStream = null;
  try {
    inputStream = getClass().getResourceAsStream(JETTY_XML_LOCATION_IN_JAR + "/" + jettyConfig.getName());
    if (inputStream == null) {
      throw new RuntimeException(format("Resource {0}/{1} does not exist in the classpath", JETTY_XML_LOCATION_IN_JAR, jettyConfig.getName()));
    }
    FileUtils.copyInputStreamToFile(inputStream, systemEnvironment.getJettyConfigFile());
  } catch (IOException e) {
    throw new RuntimeException(e);
  } finally {
    IOUtils.closeQuietly(inputStream);
  }
}

代码示例来源:origin: apache/incubator-druid

private void downloadGeoLiteDbToFile(File geoDb)
{
 if (geoDb.exists()) {
  return;
 }
 try {
  LOG.info("Downloading geo ip database to [%s]. This may take a few minutes.", geoDb.getAbsolutePath());
  File tmpFile = File.createTempFile("druid", "geo");
  FileUtils.copyInputStreamToFile(
    new GZIPInputStream(
      new URL("http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz").openStream()
    ),
    tmpFile
  );
  if (!tmpFile.renameTo(geoDb)) {
   throw new RuntimeException("Unable to move geo file to [" + geoDb.getAbsolutePath() + "]!");
  }
 }
 catch (IOException e) {
  throw new RuntimeException("Unable to download geo ip database.", e);
 }
}

代码示例来源:origin: alipay/sofa-jarslink

private File createBizFile() throws IOException {
  String url = typeCommandOption.getArgs()[0];
  String fileName = parseFileName(url);
  URL bizUrl = new URL(url);
  File workingDir = new File(EnvironmentUtils.getProperty(Constants.JARSLINK_WORKING_DIR));
  File bizFile = new File(workingDir, fileName);
  FileUtils.copyInputStreamToFile(bizUrl.openStream(), bizFile);
  return bizFile;
}

代码示例来源:origin: AsyncHttpClient/async-http-client

public static File resourceAsFile(String path) throws URISyntaxException, IOException {
 ClassLoader cl = TestUtils.class.getClassLoader();
 URI uri = cl.getResource(path).toURI();
 if (uri.isAbsolute() && !uri.isOpaque()) {
  return new File(uri);
 } else {
  File tmpFile = File.createTempFile("tmpfile-", ".data", TMP_DIR);
  tmpFile.deleteOnExit();
  try (InputStream is = cl.getResourceAsStream(path)) {
   FileUtils.copyInputStreamToFile(is, tmpFile);
   return tmpFile;
  }
 }
}

代码示例来源:origin: ming1016/study

try {
  entryInputStream = jarFile.getInputStream(entry);
  FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName));
} catch (Exception e) {
  die("Failed to copy resource: " + fileName);

代码示例来源:origin: ming1016/study

try {
  entryInputStream = jarFile.getInputStream(entry);
  FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName));
} catch (Exception e) {
  die("Failed to copy resource: " + fileName);

代码示例来源:origin: SonarSource/sonarqube

@Override
public void download(URI uri, File toFile) {
 try {
  copyInputStreamToFile(downloader.newInputSupplier(uri, this.connectTimeout, this.readTimeout).getInput(), toFile);
 } catch (IOException e) {
  deleteQuietly(toFile);
  throw failToDownload(uri, e);
 }
}

代码示例来源:origin: apache/incubator-gobblin

/***
 * Download a S3 object to local directory
 *
 * @param s3ObjectSummary S3 object summary for the object to download
 * @param targetDirectory Local target directory to download the object to
 * @throws IOException If any errors were encountered in downloading the object
 */
public void downloadS3Object(S3ObjectSummary s3ObjectSummary,
  String targetDirectory)
  throws IOException {
 final AmazonS3 amazonS3 = getS3Client();
 final GetObjectRequest getObjectRequest = new GetObjectRequest(
   s3ObjectSummary.getBucketName(),
   s3ObjectSummary.getKey());
 final S3Object s3Object = amazonS3.getObject(getObjectRequest);
 final String targetFile = StringUtils.removeEnd(targetDirectory, File.separator) + File.separator + s3Object.getKey();
 FileUtils.copyInputStreamToFile(s3Object.getObjectContent(), new File(targetFile));
 LOGGER.info("S3 object downloaded to file: " + targetFile);
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * clean up and set up test directory
 */
private void setupTestDir() throws IOException {
 _testDirPath = _config.getString("gobblin.cluster.work.dir");
 _jobDirPath = _config.getString(GobblinClusterConfigurationKeys.JOB_CONF_PATH_KEY);
 // clean up test directory and create job dir
 File testDir = new File(_testDirPath);
 File jobDir = new File(_jobDirPath);
 if (testDir.exists()) {
  FileUtils.deleteDirectory(testDir);
 }
 jobDir.mkdirs();
 // copy job file from resource
 String jobFileName = GobblinClusterKillTest.class.getSimpleName() + "Job.conf";
 try (InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream(jobFileName)) {
  if (resourceStream == null) {
   throw new RuntimeException("Could not find job resource " + jobFileName);
  }
  File targetFile = new File(_jobDirPath + "/" + jobFileName);
  FileUtils.copyInputStreamToFile(resourceStream, targetFile);
 } catch (IOException e) {
  throw new RuntimeException("Unable to load job resource " + jobFileName, e);
 }
}

代码示例来源:origin: jenkinsci/jenkins

@SuppressWarnings("deprecation")
  @Override
  public ParameterValue createValue(CLICommand command, String value) throws IOException, InterruptedException {
    // capture the file to the server
    File local = File.createTempFile("jenkins","parameter");
    String name;
    if (value.isEmpty()) {
      FileUtils.copyInputStreamToFile(command.stdin, local);
      name = "stdin";
    } else {
      FilePath src = new FilePath(command.checkChannel(), value);
      src.copyTo(new FilePath(local));
      name = src.getName();
    }

    FileParameterValue p = new FileParameterValue(getName(), local, name);
    p.setDescription(getDescription());
    p.setLocation(getName());
    return p;
  }
}

代码示例来源:origin: jenkinsci/jenkins

FileUtils.copyInputStreamToFile(stdin, f);
if (dynamicLoad) {
  pm.dynamicLoad(f);

代码示例来源:origin: spring-projects/spring-integration-samples

@Test
public void runDemo() throws Exception{
  ConfigurableApplicationContext ctx =
    new ClassPathXmlApplicationContext("META-INF/spring/integration/FtpOutboundChannelAdapterSample-context.xml");
  MessageChannel ftpChannel = ctx.getBean("ftpChannel", MessageChannel.class);
  baseFolder.mkdirs();
  final File fileToSendA = new File(baseFolder, "a.txt");
  final File fileToSendB = new File(baseFolder, "b.txt");
  final InputStream inputStreamA = FtpOutboundChannelAdapterSample.class.getResourceAsStream("/test-files/a.txt");
  final InputStream inputStreamB = FtpOutboundChannelAdapterSample.class.getResourceAsStream("/test-files/b.txt");
  FileUtils.copyInputStreamToFile(inputStreamA, fileToSendA);
  FileUtils.copyInputStreamToFile(inputStreamB, fileToSendB);
  assertTrue(fileToSendA.exists());
  assertTrue(fileToSendB.exists());
  final Message<File> messageA = MessageBuilder.withPayload(fileToSendA).build();
  final Message<File> messageB = MessageBuilder.withPayload(fileToSendB).build();
  ftpChannel.send(messageA);
  ftpChannel.send(messageB);
  Thread.sleep(2000);
  assertTrue(new File(TestSuite.FTP_ROOT_DIR + File.separator + "a.txt").exists());
  assertTrue(new File(TestSuite.FTP_ROOT_DIR + File.separator + "b.txt").exists());
  LOGGER.info("Successfully transferred file 'a.txt' and 'b.txt' to a remote FTP location.");
  ctx.close();
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@BeforeClass
public static void startServers() throws Exception {
 basedir = System.getProperty("basedir");
 if (basedir == null) {
  basedir = new File(".").getCanonicalPath();
 }
 // System.setProperty("sun.security.krb5.debug", "true");
 System.setProperty("java.security.krb5.conf",
   new File(basedir + File.separator + "target" + File.separator + "krb5.conf").getCanonicalPath());
 loginConfig = new File(basedir + File.separator + "target" + File.separator + "kerberos.jaas");
 System.setProperty("java.security.auth.login.config", loginConfig.getCanonicalPath());
 kerbyServer = new SimpleKdcServer();
 kerbyServer.setKdcRealm("service.ws.apache.org");
 kerbyServer.setAllowUdp(false);
 kerbyServer.setWorkDir(new File(basedir, "target"));
 //kerbyServer.setInnerKdcImpl(new NettyKdcServerImpl(kerbyServer.getKdcSetting()));
 kerbyServer.init();
 // Create principals
 alice = "alice@service.ws.apache.org";
 bob = "bob/service.ws.apache.org@service.ws.apache.org";
 kerbyServer.createPrincipal(alice, "alice");
 kerbyServer.createPrincipal(bob, "bob");
 aliceKeytab = new File(basedir + File.separator + "target" + File.separator + "alice.keytab");
 bobKeytab = new File(basedir + File.separator + "target" + File.separator + "bob.keytab");
 kerbyServer.exportPrincipal(alice, aliceKeytab);
 kerbyServer.exportPrincipal(bob, bobKeytab);
 kerbyServer.start();
 FileUtils.copyInputStreamToFile(SpnegoEngine.class.getResourceAsStream("/kerberos.jaas"), loginConfig);
}

代码示例来源:origin: gocd/gocd

public static GoConfigHolder loadWithMigration(InputStream input, final ConfigElementImplementationRegistry registry) throws Exception {
  File tempFile = TestFileUtil.createTempFile("cruise-config.xml");
  try {
    MagicalGoConfigXmlLoader xmlLoader = new MagicalGoConfigXmlLoader(new ConfigCache(), registry);
    FileUtils.copyInputStreamToFile(input, tempFile);
    migrate(tempFile);
    return xmlLoader.loadConfigHolder(FileUtils.readFileToString(tempFile, UTF_8));
  } finally {
   FileUtils.deleteQuietly(tempFile);
  }
}

代码示例来源:origin: apache/incubator-gobblin

private void testConversion(RestEntry<JsonObject> expected, WorkUnitState actualWorkUnitState) throws DataConversionException, IOException, JSONException {
  Schema schema = new Schema.Parser().parse(getClass().getResourceAsStream("/converter/nested.avsc"));
  GenericDatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema);

  File tmp = File.createTempFile(this.getClass().getSimpleName(), null);
  tmp.deleteOnExit();
  try {
   FileUtils.copyInputStreamToFile(getClass().getResourceAsStream("/converter/nested.avro"), tmp);
   DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(tmp, datumReader);
   GenericRecord avroRecord = dataFileReader.next();

   AvroToRestJsonEntryConverter converter = new AvroToRestJsonEntryConverter();
   RestEntry<JsonObject> actual = converter.convertRecord(null, avroRecord, actualWorkUnitState).iterator().next();

   Assert.assertEquals(actual.getResourcePath(), expected.getResourcePath());
   JSONAssert.assertEquals(expected.getRestEntryVal().toString(), actual.getRestEntryVal().toString(), false);

   converter.close();
   dataFileReader.close();
  } finally {
   if (tmp != null) {
    tmp.delete();
   }
  }
 }
}

相关文章

微信公众号

最新文章

更多

FileUtils类方法