您的位置:首页 > 其它

计算文件大小和base64解码图片

2016-12-05 14:54 281 查看
//文件大小
private static String getFileSize(File file) {
Long fileSize = file.length();
if (fileSize.equals((Long) 0L)) {
return "0KB";
}
double kiloByte = fileSize / 1024L;
if (kiloByte <= 1) {
return "1KB";
}
double megaByte = kiloByte / 1024;
if (megaByte < 1) {
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte + 1));
return result1.setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
}
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
}


//解码图片并保存

colFileModel = colFileBizService.innerDownFileById(fileId);
String downloadHis = COLFileServiceUtils.downloadHis(colFileModel.getFileName());
ChenData da=JSON.parseObject(downloadHis, ChenData.class);
byte[] bt = null;
try {
BASE64Decoder decoder = new BASE64Decoder();
bt = decoder.decodeBuffer(da.getData());
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = new FileOutputStream("D:/chen.png");
fos.write(bt);
fos.close();


public static String downloadHis( String fileName) throws Exception {
Map<String, String> paramMap = new HashMap<>();
paramMap.put("fileName", fileName);
paramMap.put(COL_FILE_SIGN_ID_KEY, COL_FILE_SIGN_ID);
String sign = generateApiSign(paramMap);
paramMap.put(COL_FILE_SIGN_KEY_KEY, sign);
Map<String, Object> transformed = new HashMap<>();
logger.info("请求文件服务域>>>>>>{}",paramMap.toString());
String resStr = HttpUtils.executePostMethodRequest(PARTNERFILE_DOWNLOAD_URL, paramMap);
//logger.info("接收文件服务域<<<<<<{}",resStr);
if (StringUtils.isBlank(resStr)) {
logger.info("返回结果为空");
return null;
}
return resStr;
}


public static String executePostMethodRequest(String path, Map<String, String> paramMap) {
String result = null;
HttpPost httpPost = null;
try {
httpPost = new HttpPost(path);
List<NameValuePair> paramList = constructNameValuePair(paramMap);
httpPost.setEntity(new UrlEncodedFormEntity(paramList, DEFAULT_CHARSET));
HttpClient httpClient = getPooledHttpClient(DEFAULT_TIMEOUT);
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() >= WRONG_CODE) {
LOGGER.error("Http Request Failed: URL".concat(path).concat(",Params:").concat(paramMap.toString())
.concat(",Error Code").concat(String.valueOf(response.getStatusLine().getStatusCode())));
} else {
result = fetchContent(response.getEntity().getContent());
}
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} finally {
if (null != httpPost) {
httpPost.abort();
}
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string