本文整理汇总了Java中org.apache.commons.io.FileUtils.readFileToByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java FileUtils.readFileToByteArray方法的具体用法?Java FileUtils.readFileToByteArray怎么用?Java FileUtils.readFileToByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.io.FileUtils
的用法示例。
在下文中一共展示了FileUtils.readFileToByteArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: read
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
private void read(Object manifestPath) {
File manifestFile = fileResolver.resolve(manifestPath);
try {
byte[] manifestBytes = FileUtils.readFileToByteArray(manifestFile);
manifestBytes = prepareManifestBytesForInteroperability(manifestBytes);
// Eventually convert manifest content to UTF-8 before handing it to java.util.jar.Manifest
if (!DEFAULT_CONTENT_CHARSET.equals(contentCharset)) {
manifestBytes = new String(manifestBytes, contentCharset).getBytes(DEFAULT_CONTENT_CHARSET);
}
// Effectively read the manifest
Manifest javaManifest = new Manifest(new ByteArrayInputStream(manifestBytes));
addJavaManifestToAttributes(javaManifest);
addJavaManifestToSections(javaManifest);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
示例2: onDone
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Override
public void onDone(String utteranceId) {
//this allows us to keep track of multiple callbacks
SpeechFromTextCallback callback = mCallbacks.get(utteranceId);
if(callback != null){
//get our cache file where we'll be storing the audio
File cacheFile = getCacheFile(utteranceId);
try {
byte[] data = FileUtils.readFileToByteArray(cacheFile);
callback.onSuccess(data);
} catch (IOException e) {
e.printStackTrace();
//bubble up our error
callback.onError(e);
}
cacheFile.delete();
//remove the utteranceId from our callback once we're done
mCallbacks.remove(utteranceId);
}
}
示例3: downloadNotebook
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@RequestMapping(value = "/exportNotebook")
public ResponseEntity<byte[]> downloadNotebook(@RequestParam(value = "type") String type, @RequestParam(value = "notebookId") int notebookId, HttpSession session) throws IOException, DocumentException{
String leftPath = session.getServletContext().getRealPath("/temp/");
//在服务器端生成zip文件
File file = downloadService.downloadNotebook(notebookId, type, leftPath);
//将文件返回给用户
HttpHeaders headers = downloadService.genHttpHeaders(notebookId, type, "notebook");
ResponseEntity<byte[]> result = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
file.delete();
return result;
}
示例4: sync
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
private void sync() {
File tempFile = new File(Constants.CONFIG_TEMP_DB);
if(!tempFile.exists()){
DialogHelper.alert("����", "ͬ���ļ������ڣ��������غ���ͬ����");
return;
}
progressbar.setVisible(true);
progressbar.setProgress(-1f);
progressbar.setProgress(0.5f);
progressbar.setProgress(-1f);
iv_down.setDisable(true);
iv_sync.setDisable(true);
String projectDir = System.getProperty("user.dir");
File destFile = new File(projectDir,"notebook.db");
try {
byte[] data = FileUtils.readFileToByteArray(tempFile);
FileUtils.writeByteArrayToFile(destFile, data);
} catch (IOException e) {
e.printStackTrace();
DialogHelper.alert("Error", e.toString());
}
progressbar.setProgress(1f);
iv_down.setDisable(false);
iv_sync.setDisable(false);
DialogHelper.alert("Success", "ͬ���ɹ���-> \r\n" + tempFile.getAbsolutePath() + " �ѱ��ƶ��� " + projectDir);
}
示例5: sendFile
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
private void sendFile(URL fileUrl, String contentType, DataOutputStream os) throws IOException, URISyntaxException {
File file = new File(fileUrl.toURI());
if (file.exists()) {
os.writeBytes("HTTP/1.1 200 OK" + HTTP_EOL);
os.writeBytes("Content-Type: " + contentType + HTTP_EOL);
os.writeBytes(HTTP_EOL);
byte[] bytes = FileUtils.readFileToByteArray(file);
IOUtils.write(bytes, os);
} else {
os.writeBytes("HTTP/1.1 404 Not Found" + HTTP_EOL);
os.writeBytes(HTTP_EOL);
os.writeBytes("404 Not Found");
}
}
示例6: testWriteFileAndReadAsBytes
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
/**
* Write ORC file using generic file base ORC writer and then read bytes of the file and read and
* assert using memory (byte-array) based reader.
*
* @throws IOException if not able to write or read the data
*/
@Test
public void testWriteFileAndReadAsBytes() throws IOException {
final File file = new File("/tmp/tmp_2.orc");
file.deleteOnExit();
final OrcFile.WriterOptions wOpts =
OrcFile.writerOptions(new Configuration()).inspector(ROW_OI);
final OrcFile.ReaderOptions rOpts = new OrcFile.ReaderOptions(new Configuration());
final Writer writer = OrcFile.createWriter(new Path(file.toString()), wOpts);
writeUsingOrcWriter(writer);
final byte[] memOrcBytes = FileUtils.readFileToByteArray(file);
readAndAssertUsingReader(OrcUtils.createReader(memOrcBytes, rOpts).rows());
}
示例7: getBytesFromFile
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Nullable
public static byte[] getBytesFromFile(@NotNull String filename) {
try {
return FileUtils.readFileToByteArray(new File(filename));
} catch (Exception e) {
return null;
}
}
示例8: getFileEncoding
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
/**
* 获取文件编码
* @author eko.zhan at Jul 3, 2017 1:54:50 PM
* @param file
* @return
* @throws IOException
*/
public static String getFileEncoding(File file) throws IOException{
UniversalDetector detector = new UniversalDetector(null);
byte[] bytes = FileUtils.readFileToByteArray(file);
detector.handleData(bytes, 0, bytes.length);
detector.dataEnd();
return detector.getDetectedCharset();
}
示例9: downloadNote
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@RequestMapping(value = "/exportNote")
public ResponseEntity<byte[]> downloadNote(@RequestParam(value = "type") String type, @RequestParam(value = "noteId") int noteId, HttpSession session) throws IOException, DocumentException{
String leftPath = session.getServletContext().getRealPath("/temp/");
//在服务器端生成html文件
File file = downloadService.downloadNote(noteId, type, leftPath);
//将文件返回给用户
HttpHeaders headers = downloadService.genHttpHeaders(noteId, type, "note");
ResponseEntity<byte[]> result = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
file.delete();
return result;
}
示例10: createStepNodeValue
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Override
protected void createStepNodeValue(Document doc, Element stepNode) throws EngineException {
if (isEnabled()) {
try {
Engine.logBeans.info("Hashing file \"" + sourceFilePath + "\"");
File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
throw new EngineException("Source file does not exist: " + sourceFilePath);
}
if (!sourceFile.isFile()) {
throw new EngineException("Source file is not a file: " + sourceFilePath);
}
byte[] path = null;
path = FileUtils.readFileToByteArray(sourceFile);
String hash = null;
if (hashAlgorithm == HashAlgorithm.MD5) {
hash = org.apache.commons.codec.digest.DigestUtils.md5Hex(path);
} else if (hashAlgorithm == HashAlgorithm.SHA1) {
hash = org.apache.commons.codec.digest.DigestUtils.sha1Hex(path);
}
Engine.logBeans.info("File \"" + sourceFilePath + "\" has been hashed.");
// Node hashNode = doc.createElement("hash");
// hashNode.appendChild(doc.createTextNode(hash));
// stepNode.appendChild(hashNode);
Node text = doc.createTextNode(hash);
stepNode.appendChild(text);
} catch (Exception e) {
setErrorStatus(true);
throw new EngineException("Unable to compute hash code", e);
}
}
}
示例11: upload
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Override
public String upload(File file) {
FDFSFile fdfsFile=null;
try {
byte[] content = FileUtils.readFileToByteArray(file);
String extension = FilenameUtils.getExtension(file.getName());
fdfsFile = new FDFSFile(file.getName(), content, extension);
return this.upload(fdfsFile);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
示例12: readFile
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
/**
* 读取二进制文件
* @param filePathAndName 带有完整绝对路径的文件名
* @return 二进制数据
* @version 2017-02-28
*/
public static byte[] readFile(String filePathAndName) {
byte[] byteA = null;
try {
byteA = FileUtils.readFileToByteArray(new File(filePathAndName));
} catch (IOException e) {
e.printStackTrace();
logger.error("调用ApacheCommon读二进制数据时:" + e.toString());
}
return byteA;
}
示例13: testUpload2
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Test
public void testUpload2() throws Exception {
byte[] byteArray = FileUtils.readFileToByteArray(new File("/Users/ayg/Desktop/logo.gif"));
CompletableFuture<FileId> path = client.upload("123.gif", byteArray);
FileId fileId = path.get();
System.out.println(fileId);
CompletableFuture<FileMetadata> metadataGet = client.metadataGet(fileId);
System.out.println(metadataGet.get(10, TimeUnit.SECONDS));
System.out.println("==========");
}
示例14: getJarBytesFromThisLocator
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
/**
* read the jar bytes in the file system
*/
// used when creating cluster config response
// and used when uploading the jars to another locator
public byte[] getJarBytesFromThisLocator(String group, String jarName) throws Exception {
Configuration configuration = getConfiguration(group);
File jar = getPathToJarOnThisLocator(group, jarName).toFile();
if (configuration == null || !configuration.getJarNames().contains(jarName) || !jar.exists()) {
return null;
}
return FileUtils.readFileToByteArray(jar);
}
示例15: dispatch
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Override
@Nullable
public byte[] dispatch(@NonNull final String _command,
@NonNull final String[] _arguments,
@NonNull final String _outputFile) {
final String[] commandWithArguments = Stream.concat(Arrays.stream(new String[]{_command}), Arrays.stream(_arguments))
.toArray(String[]::new);
try {
final String formattedCommand = Stream
.of(commandWithArguments)
.collect(Collectors.joining(" ","[","]"));
log.info("Executing process: " + formattedCommand);
@Cleanup("destroy") final Process process = new ProcessBuilder(commandWithArguments).start();
if (process.waitFor(30, TimeUnit.MINUTES)) {
if (process.exitValue() == 0) {
final File outputFile = new File(_outputFile);
log.debug("Reading output produced by command");
byte data[] = FileUtils.readFileToByteArray(outputFile);
log.debug("Deleting file produced by command");
final boolean deletionSuccessful = outputFile.delete();
if (!deletionSuccessful) {
log.warn("Failed to delete temporary file: %s", outputFile.toPath());
}
return data;
} else {
log.error(String.format("Non-zero exit code (%d) executing command: %s", process.exitValue(), _command));
}
} else {
log.error("Timed out executing command: " + _command);
}
} catch (Exception _e) {
log.error("Failed to execute command: " + _command, _e);
}
return null;
}