Java将文件转为字节数组】的更多相关文章

Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列混合后的二进制数据.本文旨在记录自己在使用Java将文件转为字节数组的一些知识理解与汇总. FileInputStream 利用FileInputStream读取文件 FileInputStream是InputStream的子类,用于从文件中读取信息,构造器接收一个File类型或表示文件路径的Str…
/*文件64位编码*/ public static void main(String[] args) {    byte[] fileByte = toByteArray(newFile);   String imgStr = new BASE64Encoder().encode(fileByte);  } /*读取文件的字节数组*/public static byte[] toByteArray(File file) throws IOException { File f = file; if…
注:来源于JavaEye 文件转化为字节数组: http://www.javaeye.com/topic/304980 /** * 文件转化为字节数组 * * @param file * @return */ public static byte[] getBytesFromFile(File file) { byte[] ret = null; try { if (file == null) { // log.error("helper:the file is null!"); re…
java对获取的字节数组bytes[]进行处理: 第一种,直接将该字节数组转换为字符串(部分): String content = ,); //从位置0开始获取2个字节 这样,对获取的数据报进行全部转换: String content = ,dp.getLength()); //从位置0开始获取dp.getLength()个长度转换为字符串 通过获取从任意位置(比如0,x)处开始获取2或者dp.getLength()个字节将其转换为字符串,给予显示 之后转换为整型或者小数都可以,这是字符串转整型…
通过http URL 获取图片流 转为字节数组 读取本地文件转为数组 /** * 获取 文件 流 * @param url * @return * @throws IOException */ private byte[] getFile(String url) throws IOException{ URL urlConet = new URL(url); HttpURLConnection con = (HttpURLConnection)urlConet.openConnection();…
JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile(String pathStr) { File file = new File(pathStr); try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutp…
public static void main(String[] args) throws IOException { byte[] bytes = FileUtils.readFileToByteArray(new File("C://Users//Administrator//Desktop//简单装修合同样本(一).html")); System.out.println(new String(bytes)); } 没错就是这么简单.…
FileInputStream in = new FileInputStream(文件路径File); byte[] buffer = new byte[in.available()]; in.read(buffer); in.available()返回的是数组字节长度int:…
一般我们读写文件的时候都是这么写的,看着没问题哈.   public static void main(String[] args) throws Exception {   FileInputStream fileInput = new FileInputStream("e://test1.txt"); FileOutputStream fileOutput = new FileOutputStream("e://test2.txt");   byte[] buf…
package hiveTest; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import…