public static void main(String[] args) throws IOException {
File file = new File(
"C:\\Users\\Administrator\\Documents\\WeChat Files\\x564236578\\FileStorage\\File\\2019-07\\123.sql");
byte[] aa = InputStream2ByteArray("C:\\Users\\Administrator\\Documents\\WeChat Files\\x564236578\\FileStorage\\File\\2019-07\\123.sql");
System.out.println(aa);
System.out.println("-------------");
String json = JSON.toJSONString(aa);
byte[] bb = JSON.parseObject(json,byte[].class);
getFileByBytes(bb,"H:\\","111.sql");
System.out.println("bb====="+bb);
System.out.println(json);
}
private static byte[] InputStream2ByteArray(String filePath) throws IOException {
InputStream in = new FileInputStream(filePath);
byte[] data = toByteArray(in);
in.close();
return data;
}
private static byte[] toByteArray(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while ((n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
}
return out.toByteArray();
}
public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath + "\\" + fileName);
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 e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}