Bladeren bron

fastdfs工具添加

lyr 8 jaren geleden
bovenliggende
commit
bbdb42ab0a

+ 236 - 209
patient-co-wlyy/src/main/java/com/yihu/wlyy/util/CommonUtil.java

@ -1,218 +1,245 @@
package com.yihu.wlyy.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.wlyy.config.FastDFSConfig;
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
public class CommonUtil {
	/**
	 * 获取图片全路径
	 * 
	 * @return
	 */
	public static String getPhoneUrl(String url) {
		if (StringUtils.isEmpty(url)) {
			return "";
		} else {
			if (url.indexOf("http") > -1) {
				return url;
			} else {
				return SystemConf.getInstance().getServerUrl() + url;
			}
		}
	}
	public static String getMobileEncode(String mobile) {
		if (StringUtils.isNotEmpty(mobile) && mobile.length() == 11) {
			return mobile.substring(0, 4) + "****" + mobile.substring(8, 11);
		}
		return mobile;
	}
	public static 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 static 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 static 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;
	}
	public static final InputStream byte2Input(byte[] buf) {
		return new ByteArrayInputStream(buf);
	}
	public static final byte[] input2byte(InputStream inStream) throws IOException {
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		byte[] buff = new byte[100];
		int rc = 0;
		while ((rc = inStream.read(buff, 0, 100)) > 0) {
			swapStream.write(buff, 0, rc);
		}
		byte[] in2b = swapStream.toByteArray();
		return in2b;
	}
	/**
	 * 返回:张三为张*
	 * @param name 姓名
	 * @return
	 */
	public static String getEncryptName(String name) {
		if (StringUtils.isEmpty(name)) {
			return null;
		}
		if (name.length() == 1) {
			return name;
		}
		// 获取姓
		String temp = name.substring(0, 1);
		for (int i = 1; i < name.length(); i++) {
			temp += "*";
		}
		return temp;
	}
	/**
	 * 拷贝临时图片到存储目录
	 * @param images
	 * @return
	 * @throws IOException
	 */
	public static String copyTempImage(String images) throws IOException {
		// 文件保存的临时路径
		String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
		// 图片保存路径
		String imagePath = SystemConf.getInstance().getImagePath() + File.separator;
		String[] tempImages = images.split(",");
		String temp = "";
		for (String image : tempImages) {
			File file = new File(tempPath + image);
			File smallFile = new File(tempPath + image + "_small");
			if (file.exists() && smallFile.exists()) {
				// 原图拷贝路径
				File targetFile = new File(imagePath + image);
				// 缩略图拷贝路径
				File targetSmallFile = new File(imagePath + image + "_small");
				// 拷贝原图
				FileUtils.copyFile(file, targetFile);
				// 拷贝缩略图
				FileUtils.copyFile(smallFile, targetSmallFile);
				// 删除临时文件
				FileUtils.forceDelete(file);
				FileUtils.forceDelete(smallFile);
				if (temp.length() == 0) {
					temp = SystemConf.getInstance().getImageServer() + image;
				} else {
					temp += "," + SystemConf.getInstance().getImageServer() + image;
				}
			}
		}
		return temp;
	}
	/**
	 * 拷贝临时语音文件到存储目录
	 * @param images
	 * @return
	 * @throws IOException
	 */
	public static String copyTempVoice(String voices) throws IOException {
		// 文件保存的临时路径
		String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
		// 语音保存路径
		String voicePath = SystemConf.getInstance().getVoicePath() + File.separator;
		String[] tempVoices = voices.split(",");
		String temp = "";
		for (String voice : tempVoices) {
			File file = new File(tempPath + voice);
			if (file.exists()) {
				// 目标文件
				File targetFile = new File(voicePath + voice);
				// 拷贝到指定路径
				FileUtils.copyFile(file, targetFile);
				// 删除临时文件
				FileUtils.forceDelete(file);
				if (temp.length() == 0) {
					temp = SystemConf.getInstance().getVoiceServer() + voice;
				} else {
					temp += "," + SystemConf.getInstance().getVoiceServer() + voice;
				}
			}
		}
		return temp;
	}
	/**
	 * 校验健康指标是否正常
	 * @param curValue 当前值
	 * @param standardMax 最大标准值
	 * @param standardMin 最小标准值
	 * @return 0正常,1高,-1低
	 */
	public static 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;
	}
    /**
     * 获取图片全路径
     *
     * @return
     */
    public static String getPhoneUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        } else {
            if (url.indexOf("http") > -1) {
                return url;
            } else {
                return SystemConf.getInstance().getServerUrl() + url;
            }
        }
    }
    public static String getMobileEncode(String mobile) {
        if (StringUtils.isNotEmpty(mobile) && mobile.length() == 11) {
            return mobile.substring(0, 4) + "****" + mobile.substring(8, 11);
        }
        return mobile;
    }
    public static 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 static 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 static 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;
    }
    public static final InputStream byte2Input(byte[] buf) {
        return new ByteArrayInputStream(buf);
    }
    public static final byte[] input2byte(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }
    /**
     * 返回:张三为张*
     *
     * @param name 姓名
     * @return
     */
    public static String getEncryptName(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        if (name.length() == 1) {
            return name;
        }
        // 获取姓
        String temp = name.substring(0, 1);
        for (int i = 1; i < name.length(); i++) {
            temp += "*";
        }
        return temp;
    }
    /**
     * 拷贝临时图片到存储目录
     *
     * @param images
     * @return
     * @throws IOException
     */
    public static String copyTempImage(String images) throws IOException {
        // 文件保存的临时路径
        String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
        // 图片保存路径
        String imagePath = SystemConf.getInstance().getImagePath() + File.separator;
        String[] tempImages = images.split(",");
        String temp = "";
        for (String image : tempImages) {
            File file = new File(tempPath + image);
            File smallFile = new File(tempPath + image + "_small");
            if (file.exists() && smallFile.exists()) {
                // 原图拷贝路径
                File targetFile = new File(imagePath + image);
                // 缩略图拷贝路径
                File targetSmallFile = new File(imagePath + image + "_small");
                // 拷贝原图
                FileUtils.copyFile(file, targetFile);
                // 拷贝缩略图
                FileUtils.copyFile(smallFile, targetSmallFile);
                // 删除临时文件
                FileUtils.forceDelete(file);
                FileUtils.forceDelete(smallFile);
                if (temp.length() == 0) {
                    temp = SystemConf.getInstance().getImageServer() + image;
                } else {
                    temp += "," + SystemConf.getInstance().getImageServer() + image;
                }
            }
        }
        return temp;
    }
    /**
     * 拷贝临时语音文件到存储目录
     *
     * @param voices
     * @return
     * @throws IOException
     */
    public static String copyTempVoice(String voices) throws IOException {
        // 文件保存的临时路径
        String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
        // 语音保存路径
        String voicePath = SystemConf.getInstance().getVoicePath() + File.separator;
        String[] tempVoices = voices.split(",");
        String temp = "";
        for (String voice : tempVoices) {
            File file = new File(tempPath + voice);
            if (file.exists()) {
                // 目标文件
                File targetFile = new File(voicePath + voice);
                // 拷贝到指定路径
                FileUtils.copyFile(file, targetFile);
                // 删除临时文件
                FileUtils.forceDelete(file);
                if (temp.length() == 0) {
                    temp = SystemConf.getInstance().getVoiceServer() + voice;
                } else {
                    temp += "," + SystemConf.getInstance().getVoiceServer() + voice;
                }
            }
        }
        return temp;
    }
    /**
     * 校验健康指标是否正常
     *
     * @param curValue    当前值
     * @param standardMax 最大标准值
     * @param standardMin 最小标准值
     * @return 0正常,1高,-1低
     */
    public static 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;
    }
    public static String copyTempFilesToFastDFS(String files) throws Exception {
        // 文件保存的临时路径
        String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
        String[] fileArray = files.split(",");
        FastDFSUtil fastDFSUtil = new FastDFSConfig().fastDFSUtil();
        String fileUrls = "";
        for (String file : fileArray) {
            File f = new File(tempPath + file);
            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) ? "" : ",") + result.get("fileUrl").toString().replaceAll("\"","");
                }
            }
        }
        return fileUrls;
    }
}

+ 42 - 0
patient-co-wlyy/src/main/java/com/yihu/wlyy/web/statistic/FileTestController.java

@ -0,0 +1,42 @@
package com.yihu.wlyy.web.statistic;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.wlyy.config.FastDFSConfig;
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
 * Created by lyr-pc on 2016/12/13.
 */
@RestController
@RequestMapping(value = "/test/file/")
@Api(description = "文件测试")
public class FileTestController extends BaseController {
    @Autowired
    FastDFSConfig fastDFSConfig;
    @ApiOperation(value = "文件上传", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String resolve(
            @ApiParam("文件")
            @RequestPart() MultipartFile file) throws Throwable {
        try {
            FastDFSUtil fastDFSUtil = fastDFSConfig.fastDFSUtil();
            ObjectNode result = fastDFSUtil.upload(file.getInputStream(), "png", "测试");
            return write(200,"上传成功","data",result.toString());
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"上传失败");
        }
    }
}

+ 2 - 1
patient-co-wlyy/src/main/resources/config/fdfs_client.conf

@ -6,4 +6,5 @@ http.anti_steal_token = no
http.secret_key = FastDFS1234567890
#tracker_server = 172.19.103.13:22122
tracker_server = 172.19.103.54:22122
tracker_server = 192.168.131.127:22122
#172.19.103.54:22122