|
@ -0,0 +1,352 @@
|
|
|
package com.yihu.mm.util;
|
|
|
|
|
|
import it.sauronsoftware.jave.*;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
@Component
|
|
|
public class CommonUtil {
|
|
|
@Value("${fastDFS.fastdfs_file_url}")
|
|
|
private String fastdfs_file_url;
|
|
|
@Value("${server.server_url}")
|
|
|
private String server_url;
|
|
|
/**
|
|
|
* 获取图片全路径
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public String getPhoneUrl(String url) {
|
|
|
if (StringUtils.isEmpty(url)) {
|
|
|
return "";
|
|
|
} else {
|
|
|
if (url.indexOf("http") > -1) {
|
|
|
return url;
|
|
|
} else {
|
|
|
return server_url+ url;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public String getIdcardEncode(String idcard) {
|
|
|
if (idcard != null) {
|
|
|
if (idcard.length() == 18) {
|
|
|
return idcard.substring(0, 9) + "*******" + idcard.substring(16, 18);
|
|
|
} else if (idcard.length() == 15) {
|
|
|
return idcard.substring(0, 8) + "***" + idcard.substring(11, 15);
|
|
|
}
|
|
|
}
|
|
|
return idcard;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 对象转数组
|
|
|
*
|
|
|
* @param obj
|
|
|
* @return
|
|
|
*/
|
|
|
public byte[] toByteArray(Object obj) {
|
|
|
byte[] bytes = null;
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
try {
|
|
|
ObjectOutputStream oos = new ObjectOutputStream(bos);
|
|
|
oos.writeObject(obj);
|
|
|
oos.flush();
|
|
|
bytes = bos.toByteArray();
|
|
|
oos.close();
|
|
|
bos.close();
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
return bytes;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 数组转对象
|
|
|
*
|
|
|
* @param bytes
|
|
|
* @return
|
|
|
*/
|
|
|
public Object toObject(byte[] bytes) {
|
|
|
Object obj = null;
|
|
|
try {
|
|
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
|
|
ObjectInputStream ois = new ObjectInputStream(bis);
|
|
|
obj = ois.readObject();
|
|
|
ois.close();
|
|
|
bis.close();
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
} catch (ClassNotFoundException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
return obj;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 拷贝临时语音文件到存储目录
|
|
|
*
|
|
|
* @param voices
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
|
|
|
|
|
|
/*
|
|
|
public String copyTempVoice(String voices) throws Exception {
|
|
|
// 文件保存的临时路径
|
|
|
String serverUrl = fastdfs_file_url;
|
|
|
FastDFSUtil fastDFSUtil = new FastDFSUtil();
|
|
|
String fileUrls = "";
|
|
|
File f = new File(voices);
|
|
|
if (f.exists()) {
|
|
|
String fileName = f.getName();
|
|
|
InputStream in = new FileInputStream(f);
|
|
|
ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
|
|
|
in.close();
|
|
|
if (result != null) {
|
|
|
fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + serverUrl
|
|
|
+ result.get("groupName").toString().replaceAll("\"", "") + "/"
|
|
|
+ result.get("remoteFileName").toString().replaceAll("\"", "");
|
|
|
f.delete();
|
|
|
}
|
|
|
}
|
|
|
return fileUrls;
|
|
|
}
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
* double转字符串,在转int
|
|
|
* double*100转int 有bug 34.3会会变成3429
|
|
|
* @param d
|
|
|
* @return
|
|
|
*/
|
|
|
public static Integer doubleToInt(Double d){
|
|
|
if(d==null){
|
|
|
return 0;
|
|
|
}
|
|
|
String currency = String.valueOf(d);
|
|
|
int index = currency.indexOf(".");
|
|
|
int length = currency.length();
|
|
|
Integer amLong = 0;
|
|
|
if(index == -1){
|
|
|
amLong = Integer.valueOf(currency+"00");
|
|
|
}else if(length - index >= 3){
|
|
|
amLong = Integer.valueOf((currency.substring(0, index+3)).replace(".", ""));
|
|
|
if(length-index>3){
|
|
|
if(Integer.valueOf(currency.substring(index+3,index+4))>=5){
|
|
|
amLong++;
|
|
|
}
|
|
|
}
|
|
|
}else if(length - index == 2){
|
|
|
amLong = Integer.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
|
|
|
}else{
|
|
|
amLong = Integer.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
|
|
|
}
|
|
|
return amLong;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 校验健康指标是否正常
|
|
|
*
|
|
|
* @param curValue 当前值
|
|
|
* @param standardMax 最大标准值
|
|
|
* @param standardMin 最小标准值
|
|
|
* @return 0正常,1高,-1低
|
|
|
*/
|
|
|
public double checkHealthIndex(double curValue, double standardMax, double standardMin) {
|
|
|
if (curValue <= 0) {
|
|
|
return 0;
|
|
|
}
|
|
|
if (standardMax > 0 && curValue > standardMax) {
|
|
|
return curValue;
|
|
|
}
|
|
|
// 低于标准值,暂不推
|
|
|
// if (standardMin > 0 && curValue < standardMin) {
|
|
|
// return -curValue;
|
|
|
// }
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 上传文件到FastDFS
|
|
|
*
|
|
|
* @param files 临时文件
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
/*public String copyTempImage(String files) throws Exception {
|
|
|
// 文件保存的临时路径
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
String serverUrl = fastdfs_file_url;
|
|
|
String[] fileArray = files.split(",");
|
|
|
FastDFSUtil fastDFSUtil = new FastDFSUtil();
|
|
|
String fileUrls = "";
|
|
|
|
|
|
for (String file : fileArray) {
|
|
|
File f = new File(tempPath + file);
|
|
|
File fs = new File(tempPath + file + "_small");
|
|
|
if (f.exists()) {
|
|
|
String fileName = f.getName();
|
|
|
InputStream in = new FileInputStream(f);
|
|
|
ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
|
|
|
in.close();
|
|
|
if (result != null) {
|
|
|
fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + serverUrl
|
|
|
+ result.get("groupName").toString().replaceAll("\"", "") + "/"
|
|
|
+ result.get("remoteFileName").toString().replaceAll("\"", "");
|
|
|
f.delete();
|
|
|
if (fs.exists()) {
|
|
|
fs.delete();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return fileUrls;
|
|
|
}*/
|
|
|
|
|
|
public void changeToMp3(String sourcePath, String targetPath) {
|
|
|
File source = new File(sourcePath);
|
|
|
File target = new File(targetPath);
|
|
|
AudioAttributes audio = new AudioAttributes();
|
|
|
Encoder encoder = new Encoder();
|
|
|
audio.setCodec("libmp3lame");
|
|
|
EncodingAttributes attrs = new EncodingAttributes();
|
|
|
attrs.setFormat("mp3");
|
|
|
attrs.setAudioAttributes(audio);
|
|
|
try {
|
|
|
encoder.encode(source, target, attrs);
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (InputFormatException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (EncoderException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public long getVideoTimeAndImg(String sourcePath,String targetPath) throws Exception{
|
|
|
File source = new File(sourcePath);
|
|
|
Encoder encoder = new Encoder();
|
|
|
MultimediaInfo m = encoder.getInfo(source);
|
|
|
long ms = m.getDuration();
|
|
|
if(ms<1000)return ms;
|
|
|
File target = new File(targetPath);//转图片
|
|
|
VideoAttributes video = new VideoAttributes();
|
|
|
video.setCodec("png");//转图片
|
|
|
video.setSize(new VideoSize(600, 500));
|
|
|
EncodingAttributes attrs = new EncodingAttributes();
|
|
|
attrs.setFormat("image2");//转图片
|
|
|
attrs.setOffset(0.01f);//设置偏移位置,即开始转码位置(3秒)
|
|
|
attrs.setDuration(0.01f);//设置转码持续时间(1秒)
|
|
|
attrs.setVideoAttributes(video);
|
|
|
encoder.encode(source, target, attrs);
|
|
|
return m.getDuration();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void changeToMp4(String sourcePath, String targetPath) {
|
|
|
File source = new File(sourcePath);
|
|
|
File target = new File(targetPath);
|
|
|
AudioAttributes audio = new AudioAttributes();
|
|
|
|
|
|
//audio.setCodec("aac");
|
|
|
audio.setCodec("libvorbis");
|
|
|
// audio.setBitRate(new Integer(64000));
|
|
|
//audio.setChannels(new Integer(1));
|
|
|
// audio.setSamplingRate(new Integer(22050));
|
|
|
VideoAttributes video = new VideoAttributes();
|
|
|
//video.setCodec("libxvid");// 转MP4
|
|
|
video.setCodec("libtheora");//
|
|
|
// video.setBitRate(new Integer(240000));// 180kb/s比特率
|
|
|
// video.setFrameRate(new Integer(28));// 1f/s帧频,1是目前测试比较清楚的,越大越模糊
|
|
|
EncodingAttributes attrs = new EncodingAttributes();
|
|
|
attrs.setFormat("ogg");// 转MP4
|
|
|
attrs.setAudioAttributes(audio);
|
|
|
attrs.setVideoAttributes(video);
|
|
|
Encoder encoder = new Encoder();
|
|
|
long beginTime = System.currentTimeMillis();
|
|
|
try {
|
|
|
// 获取时长
|
|
|
MultimediaInfo m = encoder.getInfo(source);
|
|
|
System.out.println(m.getDuration());
|
|
|
System.out.println("获取时长花费时间是:" + (System.currentTimeMillis() - beginTime));
|
|
|
beginTime = System.currentTimeMillis();
|
|
|
encoder.encode(source, target, attrs);
|
|
|
System.out.println("视频转码花费时间是:" + (System.currentTimeMillis() - beginTime));
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (InputFormatException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (EncoderException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void main(String args[]) throws Exception{
|
|
|
System.out.print("中文".length());
|
|
|
System.out.print("中文".substring(0,30));
|
|
|
}
|
|
|
|
|
|
|
|
|
public String getSubString(String content,int min,int max){
|
|
|
if(StringUtils.isBlank(content)){
|
|
|
return "";
|
|
|
}else if(content.length()<=max){
|
|
|
return content;
|
|
|
}else{
|
|
|
return content.substring(min,max);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*public String saveVoiceToDisk(InputStream inputStream,String newFileName) throws Exception {
|
|
|
// 文件保存的临时路径
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
// 拼接年月日路径
|
|
|
String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
|
|
|
// 保存路径
|
|
|
File uploadFile = new File(tempPath + datePath + newFileName);
|
|
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
try {
|
|
|
if (!uploadFile.getParentFile().exists()) {
|
|
|
uploadFile.getParentFile().mkdirs();
|
|
|
}
|
|
|
byte[] data = new byte[1024];
|
|
|
int len = 0;
|
|
|
fileOutputStream = new FileOutputStream(uploadFile);
|
|
|
while ((len = inputStream.read(data)) != -1) {
|
|
|
fileOutputStream.write(data, 0, len);
|
|
|
}
|
|
|
// 返回保存路径
|
|
|
return tempPath+datePath + newFileName;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
if (inputStream != null) {
|
|
|
try {
|
|
|
inputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
if (fileOutputStream != null) {
|
|
|
try {
|
|
|
fileOutputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}*/
|
|
|
|
|
|
|
|
|
}
|