浏览代码

正式线配置-出去euraka注册

huangzhiyong 6 年之前
父节点
当前提交
1590068215

+ 49 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/controller/dfs/FastDFSController.java

@ -6,6 +6,7 @@ import com.yihu.fastdfs.FastDFSUtil;
import com.yihu.fastdfs.config.FastDFSConfig;
import com.yihu.jw.healthyhouse.model.dfs.FileResource;
import com.yihu.jw.healthyhouse.service.dfs.FileResourceService;
import com.yihu.jw.healthyhouse.util.FileUtil;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
@ -17,7 +18,10 @@ import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@ -45,6 +49,14 @@ public class FastDFSController extends EnvelopRestEndpoint {
    @Value("${img.path}")
    private String img_path;
    private final ResourceLoader resourceLoader;
    @Autowired
    public FastDFSController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    /**
     * 文件上传 - 返回相关索引信息
     *
@ -321,5 +333,42 @@ public class FastDFSController extends EnvelopRestEndpoint {
        }
    }
    /**
     <<<<<<< HEAD
     * 显示单张图片
     * @return
     */
    @GetMapping("/load")
    @ApiOperation(value = "获取文件(byPath)")
    public void  showPhotos(HttpServletResponse response,
                            @ApiParam(name = "fileName", value = "文件名", required = true)
                            @RequestParam(value = "fileName") String fileName) throws IOException {
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        try {
            // 由于是读取本机的文件,file是一定要加上的, path是在application配置文件中的路径
            Resource resource = resourceLoader.getResource("file:" + img_path + fileName);
            File file = resource.getFile();
            FileInputStream fis;
            fis = new FileInputStream(file);
            long size = file.length();
            byte[] temp = new byte[(int) size];
            fis.read(temp, 0, (int) size);
            fis.close();
            byte[] data = temp;
            OutputStream out = response.getOutputStream();
            response.setContentType("image/png");
            out.write(data);
            out.flush();
            out.close();
        } catch (Exception e) {
            throw e;
        }
    }
}

+ 1 - 1
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/interceptor/ActivatedInterceptor.java

@ -116,7 +116,7 @@ public class ActivatedInterceptor {
        if (user == null) {
            return joinPoint.proceed();
        } else if (HouseUserContant.activated_lock.equals(user.getActivated())) {
            response.setStatus(103);
            response.setStatus(406);
//           return failed("用户已被冻结,请联系管理员!",-10000);
        }
        return joinPoint.proceed();

+ 1 - 1
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/service/user/UserService.java

@ -175,7 +175,7 @@ public class UserService extends BaseJpaService<User, UserDao> {
        User user = userDao.findByLoginCode(userCode);
        User user1 = findById(id);
        if (user1 == null) {
            throw new ManageException("激活用户不存在!");
            throw new ManageException("更改用户不存在!");
        }
        user1.setActivated(status);
        user1.setActivatedContent(reason);

+ 132 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/util/Base64.java

@ -0,0 +1,132 @@
package com.yihu.jw.healthyhouse.util;
/**
 * 标准Base64编解码,具体规范请参见相关文档。
 */
public class Base64 {
    /**
     * Base64编码表。
     */
    private static char Base64Code[] = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
    };
    /**
     * Base64解码表。
     */
    private static byte Base64Decode[] = {
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  //注意两个63,为兼容SMP,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 63, -1, 63,  //“/”和“-”都翻译成63。
            52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,
            -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,  //注意两个0:
            15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,  //“A”和“=”都翻译成0。
            -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
            41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
    };
    /**
     * 构造方法私有化,防止实例化。
     */
    private Base64() {
    }
    /**
     * Base64编码。将字节数组中字节3个一组编码成4个可见字符。
     *
     * @param b 需要被编码的字节数据。
     * @return 编码后的Base64字符串。
     */
    public static String encode(byte[] b) {
        int code = 0;
        //按实际编码后长度开辟内存,加快速度
        StringBuffer sb = new StringBuffer(((b.length - 1) / 3) << 2 + 4);
        //进行编码
        for (int i = 0; i < b.length; i++) {
            code |= (b[i] << (16 - i % 3 * 8)) & (0xff << (16 - i % 3 * 8));
            if (i % 3 == 2 || i == b.length - 1) {
                sb.append(Base64Code[(code & 0xfc0000) >>> 18]);
                sb.append(Base64Code[(code & 0x3f000) >>> 12]);
                sb.append(Base64Code[(code & 0xfc0) >>> 6]);
                sb.append(Base64Code[code & 0x3f]);
                code = 0;
            }
        }
        //对于长度非3的整数倍的字节数组,编码前先补0,编码后结尾处编码用=代替,
        //=的个数和短缺的长度一致,以此来标识出数据实际长度
        if (b.length % 3 > 0) {
            sb.setCharAt(sb.length() - 1, '=');
        }
        if (b.length % 3 == 1) {
            sb.setCharAt(sb.length() - 2, '=');
        }
        return sb.toString();
    }
    /**
     * Base64解码。
     *
     * @param code 用Base64编码的ASCII字符串
     * @return 解码后的字节数据
     */
    public static byte[] decode(String code) {
        //检查参数合法性
        if (code == null) {
            return null;
        }
        int len = code.length();
        if (len % 4 != 0) {
            throw new IllegalArgumentException("Base64 string length must be 4*n");
        }
        if (code.length() == 0) {
            return new byte[0];
        }
        //统计填充的等号个数
        int pad = 0;
        if (code.charAt(len - 1) == '=') {
            pad++;
        }
        if (code.charAt(len - 2) == '=') {
            pad++;
        }
        int retLen = len / 4 * 3 - pad;
        byte[] ret = new byte[retLen];
        char ch1, ch2, ch3, ch4;
        int i;
        for (i = 0; i < len; i += 4) {
            int j = i / 4 * 3;
            ch1 = code.charAt(i);
            ch2 = code.charAt(i + 1);
            ch3 = code.charAt(i + 2);
            ch4 = code.charAt(i + 3);
            int tmp = (Base64Decode[ch1] << 18) | (Base64Decode[ch2] << 12)
                    | (Base64Decode[ch3] << 6) | (Base64Decode[ch4]);
            ret[j] = (byte) ((tmp & 0xff0000) >> 16);
            if (i < len - 4) {
                ret[j + 1] = (byte) ((tmp & 0x00ff00) >> 8);
                ret[j + 2] = (byte) ((tmp & 0x0000ff));
            } else {
                if (j + 1 < retLen) {
                    ret[j + 1] = (byte) ((tmp & 0x00ff00) >> 8);
                }
                if (j + 2 < retLen) {
                    ret[j + 2] = (byte) ((tmp & 0x0000ff));
                }
            }
        }
        return ret;
    }
}

+ 423 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/util/FileUtil.java

@ -0,0 +1,423 @@
package com.yihu.jw.healthyhouse.util;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
 * @author Air
 * @version 1.0
 * @created 2015.06.25 14:14
 */
public class FileUtil {
    public static boolean writeFile(String filePath, String data, String encoding) throws IOException {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                return false;
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, encoding);
        outputStreamWriter.write(data);
        outputStreamWriter.flush();
        outputStreamWriter.close();
        return true;
    }
    public static boolean writeFileDecode(String filePath, String data, String encoding) throws IOException {
        File file = new File(filePath);
//        if (file.exists()) {
//            return false;
//        }
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                return false;
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, encoding);
        byte[] bytes = Base64.decode(data);
        byte[] bbuf = new byte[1024];
        InputStream fis = new ByteArrayInputStream(bytes);
        int hasRead = 0;
        while ((hasRead = fis.read(bbuf)) > 0) {
            //每读取一次,即写入文件输出流,读了多少,就写多少
            fileOutputStream.write(bbuf, 0, hasRead);
        }
        outputStreamWriter.flush();
        fileOutputStream.close();
        outputStreamWriter.close();
        return true;
    }
    /**
     * 将文件的String类型二进制流转成文件
     *
     * @param filePath
     * @param data
     * @param encoding
     * @return
     * @throws IOException
     */
    public static boolean writeFileByString(String filePath, String data, String encoding) throws IOException {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                return false;
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        byte[] bytes = Base64.decode(data);
        byte[] bbuf = new byte[1024];
        InputStream fis = new ByteArrayInputStream(bytes);
        int hasRead = 0;
        //循环从输入流中取出数据
        while ((hasRead = fis.read(bbuf)) > 0) {
            //每读取一次,即写入文件输出流,读了多少,就写多少
            fileOutputStream.write(bbuf, 0, hasRead);
        }
        fileOutputStream.close();
        return true;
    }
    public static boolean writeFile(String filePath, byte[] bytes, String encoding) throws IOException {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                return false;
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        byte[] bbuf = new byte[1024];
        InputStream fis = new ByteArrayInputStream(bytes);
        int hasRead = 0;
        //循环从输入流中取出数据
        while ((hasRead = fis.read(bbuf)) > 0) {
            fileOutputStream.write(bbuf, 0, hasRead);
        }
        fileOutputStream.close();
        return true;
    }
    /**
     * InputStream 转 byte[]
     *
     * @param input
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }
    /**
     * 通过文件途径,将文件转成byte[] 流
     *
     * @param filepath 文件路径
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(String filepath) throws IOException {
        File file = new File(filepath);
        InputStream input = new FileInputStream(file);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        byte[] bytes =output.toByteArray();
        input.close();
        output.close();
        return bytes;
    }
    /**
     * 通过文件途径,将文件转成String二进制流  流
     *
     * @param filepath 文件路径
     * @return
     * @throws IOException
     */
    public static String file2Base64String(String filepath) throws IOException {
        File file = new File(filepath);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return Base64.encode(buffer);
    }
    /**
     * file转string
     * @param fileName 文件名
     * @return string类型流
     */
    public static String convertFileToString(String fileName) {
        File file = new File(fileName);
        StringBuilder sb = new StringBuilder();
        if(file.isFile()&&file.exists()) {
            InputStreamReader read = null;
            try {
                read = new InputStreamReader(new FileInputStream(file), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            BufferedReader reader = new BufferedReader(read);
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
    /**
     * 复制一个指定文件夹下的指定文件 到另一个指定文件夹下
     * @param path 复制文件路径
     * @param toPath 文件路径
     */
    public static void copyAllFileByPath(String path, String toPath) throws IOException {
        File file = new File(path);
        File toFile = new File(toPath);
        byte[] b = new byte[(int) file.length()];
        if(file.isFile()){
            try {
                FileInputStream is= new FileInputStream(file);
                FileOutputStream ps= new FileOutputStream(toFile);
                is.read(b);
                ps.write(b);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(file.isDirectory()){
            if(!file.exists()) {
                file.mkdir();
            }
            if(!toFile.exists()) {
                toFile.mkdir();
            }
            String[] list = file.list();
            for(int i=0;i<list.length;i++){
                copyAllFileByPath(path + "/" + list[i], toPath + "/" + list[i]);
            }
        }
    }
    /**
     * 删除整个目录
     *
     * @param dir 目录
     * @return boolean
     * @created Airhead
     */
    public static boolean deleteDirectory(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDirectory(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空,可以删除
        return dir.delete();
    }
    /**
     * InputStream转string
     * @param is 输入流
     * @return Base64编码 文件字节流
     */
    public static String streamToBase64String(InputStream is) {
        byte[] bytes= FileUtil.getBytesByStream(is);
        return Base64.encode(bytes);
    }
    public static  byte[] getBytesByStream(InputStream inputStream){
        byte[] buffer = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1024];
            int n;
            while ((n = inputStream.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
    /**
     * 读取文本文件内容
     * @param file  文件路径
     * @return
     */
    public static String readFileText(File file) {
        return readFileText(file, "UTF-8");
    }
    public static String readFileText(File file, String charset) {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream in = null;
        BufferedReader br = null;
        try {
            in = new FileInputStream(file);
            br = new BufferedReader(new InputStreamReader(in, charset));
            String line = null;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
        return stringBuilder.toString();
    }
    /**
     * 读取文件内容
     * @param bytes
     * @return
     */
    public static String readFileText(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream in = null;
        BufferedReader br = null;
        try {
            in = new ByteArrayInputStream(bytes);
            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
        return stringBuilder.toString();
    }
    public static String readFileText(InputStream in) {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
        return stringBuilder.toString();
    }
    /**
     * 获取文件名后缀
     * @param file
     * @return
     */
    public static String getFileSuffix(File file) {
        String fileName = file.getName();
        int index = fileName.indexOf(".");
        return fileName.substring(index);
    }
    /**
     * 获取文件名后缀
     * @param file
     * @return
     */
    public static String getFileSuffix(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        int index = fileName.indexOf(".");
        return fileName.substring(index);
    }
    public static String getCharSet(String fileName) throws Exception {
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
        int p = (bin.read() << 8) + bin.read();
        bin.close();
        String code = null;
        switch (p) {
            case 0xefbb:
                code = "UTF-8";
                break;
            case 0xfffe:
                code = "Unicode";
                break;
            case 0xfeff:
                code = "UTF-16BE";
                break;
            default:
                code = "GBK";
        }
        return code;
    }
}

+ 4 - 4
svr/svr-healthy-house/src/main/resources/application.yml

@ -86,8 +86,8 @@ fastDFS:
jw:
  smsUrl: http://svr-base:10020/sms_gateway/send
img:
  #path: E:/tryfile/upload/img  //测试
  path: /root/uploadFiles/group1
  path: E:/tryfile/upload/img/
#  path: /root/uploadFiles/group1
---
spring:
@ -105,7 +105,7 @@ fastDFS:
jw:
  smsUrl: http://svr-base:10020/sms_gateway/send
img:
  path: /data/upload/img
  path: /data/upload/img/
---
spring:
  profiles: prod
@ -122,4 +122,4 @@ fastDFS:
jw:
  smsUrl: http://svr-base:10020/sms_gateway/send
img:
  path: /data/upload/img
  path: /data/upload/img/

+ 1 - 0
svr/svr-healthy-house/src/main/resources/bootstrap.yml

@ -14,6 +14,7 @@ spring:
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
---
spring:
  profiles: jwdev