通过文件字节流字符串传输文件
2018-01-11 16:08
246 查看
1.将文件解析为字节流字符串
String strFilePathName = "文件全路径";
byte[] data = null ;//data数组为文件解析后得到的数组
try {
FileInputStream in =new FileInputStream(new File(strFilePathName));
//当文件没有结束时,每次读取一个字节显示
data=new byte[in.available()];
in.read(data);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
String str= Base64.encode(data); //获得加密字字符串
注: 需要的jar包 com.sun.org.apache.xml.internal.security.utils.Base64;
2.将字节流字符串转为文件
public void getFile(String str){
String filePath = "文件存储路径";
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
byte[] srtbyte = str.getBytes();
byte[] bytes = Base64.decode(srtbyte);//解码字符串
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+"文件名字");//完整的文件名称(+后缀)
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
}catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
String strFilePathName = "文件全路径";
byte[] data = null ;//data数组为文件解析后得到的数组
try {
FileInputStream in =new FileInputStream(new File(strFilePathName));
//当文件没有结束时,每次读取一个字节显示
data=new byte[in.available()];
in.read(data);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
String str= Base64.encode(data); //获得加密字字符串
注: 需要的jar包 com.sun.org.apache.xml.internal.security.utils.Base64;
2.将字节流字符串转为文件
public void getFile(String str){
String filePath = "文件存储路径";
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
byte[] srtbyte = str.getBytes();
byte[] bytes = Base64.decode(srtbyte);//解码字符串
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+"文件名字");//完整的文件名称(+后缀)
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
}catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
相关文章推荐
- 通过字节流来代替链接来下载小文件
- centos 通过xshell 传输文件
- 通过FTP实现vxsim和vxworks主机之间的文件传输
- 嵌入式linux系统与主机通过串口传输文件
- Linux 使用scp通过ssh传输文件
- android通过拼写字符串获取资源文件
- Windows下通过VMware向虚拟机传输文件
- 在两个Android设备间通过UDP传输目录内文件
- I/O流 , 通过字节流实现 (文件夹-文件夹;文件-文件夹;文件-文件)的Copy
- 笔记:使用json传输图片,根据个人经验:我做不到,想了个办法将文件转变成字符串并压缩
- ARM 开发板嵌入式linux系统与主机PC通过串口传输文件
- 在linux下一般用scp这个命令来通过ssh传输文件。
- 通过alljoyn传输文件
- git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常常需要寻找两个分支最近的分割点,即base.假设git 树是多叉树,请实现一个算法,计算git树上任意两点的最近分割点。 (假设git树节点数为n,用邻接矩阵的形式表示git树:字符串数组matrix包含n个字符串,每个字符串由字符'0
- 通过java字节流复制文件,逐个字节读取写入
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- 嵌入式linux系统与主机通过串口传输文件
- 通过缓冲流把一个文本文件中的字符串排序后输入到另一个文件中
- ADO.net通过配置文件操作连接字符串