QQBot/src/main/java/com/yutou/qqbot/utlis/StreamTools.java

58 lines
1.8 KiB
Java
Raw Normal View History

2021-12-06 11:19:00 +08:00
package com.yutou.qqbot.utlis;
import java.io.*;
public class StreamTools {
public static String streamReadLine(InputStream stream) {
StringBuilder builder = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String tmp;
while ((tmp = reader.readLine()) != null) {
builder.append(tmp);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
public static File streamSave(InputStream stream) {
try {
if (stream != null) {
File file = new File("tmp" + File.separator + System.currentTimeMillis());
FileOutputStream outputStream = new FileOutputStream(file);
byte[] bytes = new byte[2048];
int len;
while ((len = stream.read(bytes)) > -1) {
outputStream.write(bytes, 0, len);
}
outputStream.flush();
stream.close();
outputStream.close();
return file;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] fileToByte(File file){
byte[] buffer = new byte[1024];
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
int len;
try {
FileInputStream in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
outputStream.write(buffer,0,len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return outputStream.toByteArray();
}
}