[工作日志] 2019-04-12 按行读取文件和读取全部文件信息

private String getContent(MultipartFile file) {
long start = System.currentTimeMillis();
InputStream is = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
is = file.getInputStream();
byte[] b = new byte[1024];
int n;
while ((n = is.read(b)) != -1) {
out.write(b, 0, n);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return out.toString();
}




private void checkHeadAndTail(MultipartFile file){
if (file == null) {
throw new ApiException(WrapperEnumError.PARAMS_MISS.getCode(), "公钥文件不能为空");
}
List<String> list = new ArrayList<>();
try {
InputStream inputStream = file.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line;
while ((line = read.readLine()) != null){
list.add(line);
}
if(!list.get(0).trim().equals("-----BEGIN PUBLIC KEY-----")){
throw new ApiException(WrapperEnumError.OTHER_ERROR.getCode(), "文件开头格式错误");
}
if(!list.get(list.size() - 1).trim().equals("-----END PUBLIC KEY-----")){
throw new ApiException(WrapperEnumError.OTHER_ERROR.getCode(), "文件结尾格式错误");
}
} catch (IOException e) {
e.printStackTrace();
}


}

猜你喜欢

转载自www.cnblogs.com/fan3516/p/10694816.html