import org.apache.commons.codec.binary.Base64;
import java.io.*;
/**
* @description:
* @author: Mr.FZT
* @create: 2020-06-29
**/
public class FileUtil{
public static void main(String[] args) {
String s =file2String("/Users/wf/Downloads/测试.xlsx");
System.out.println(s);
string2File(s, "/Users/wf/Downloads/测试2.xlsx");
}
private static void string2File(String base64Code, String targetPath) {
byte[] buffer;
FileOutputStream out =null;
try {
Base64 base64 =new Base64();
//解码
buffer = base64.decode(base64Code);
out =new FileOutputStream(targetPath);
out.write(buffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out !=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static Stringfile2String(String path) {
File file =new File(path);
FileInputStream fis =null;
StringBuilder content =new StringBuilder();
try {
fis =new FileInputStream(file);
int length =2 *1024 *1024;
byte[] byteAttr =new byte[length];
int byteLength;
while ((byteLength = fis.read(byteAttr, 0, byteAttr.length)) != -1) {
String encode;
if (byteLength != byteAttr.length) {
byte[] temp =new byte[byteLength];
System.arraycopy(byteAttr, 0, temp, 0, byteLength);
Base64 base64 =new Base64();
encode = base64.encodeToString(temp);
content.append(encode);
} else {
Base64 base64 =new Base64();
encode = base64.encodeToString(byteAttr);
content.append(encode);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis !=null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString();
}
}
java-File转String,String转File
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 原文地址:https://blog.csdn.net/csdn_terence/article/details/7...
- import java.io.UnsupportedEncodingException;import java.m...