java開發的zimg客戶端


1、zimg的安裝部署

最開始的時候是下載zimg的源碼安裝的,由於zimg依賴項眾多,沒有安裝成功,剛好那期間在學習docker,於是docker search zimg一下,驚奇的發現有zimg鏡像。

鏡像名:iknow0612/zimg

docker pull iknow0612/zimg

運行zimg的方法(開發用,沒有該配置,簡單的做了圖片存儲位置的映射,重啟后上傳的圖片還在。docker的實例名稱很重要,同一主機的docker容器間通信最好用--link,IP有時候不太穩定)

docker run -it -d -p 4869:4869 -v /home/liuzhijun/zimg/images:/zimg/bin/img --name guttv_zimg iknow0612/zimg sh app.sh

2、java客戶端代碼

package com.guttv.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.google.gson.Gson;
import com.guttv.common.utils.ZimgClient.ZimgResult;

public class ZimgClient {
    public static String zimgUrl = "http://192.168.1.221:4869/";
    public static String zimgShareUrl = "http://192.168.1.221:4869/";
    public static String tmpPath = "/var/guttv/logs/tmp";
    protected Logger logger = LoggerFactory.getLogger(getClass());

    public static void main(String[] args) {
        // 從文件上傳圖片
        ZimgResult ret0 = new ZimgClient()
                .uploadImg("/var/guttv/logs/tmp/gophercolor.png");
        System.out.println(ret0.isRet() + "\r\n" + ret0.getImageUrl());
        // 從URL上傳圖片
        ZimgResult ret = new ZimgClient()
                .uploadImgFromUrl("http://creatives.ftchinese.com/ads/beijing/201509/20150914_cartier_33660.gif");
        System.out.println(ret.isRet() + "\r\n" + ret.getImageUrl());
        // Send("http://192.168.1.221:4869/upload",
        // "c:/4c422e03jw1ejoqm5ghm0j20nl0fb76x.jpg", "jpg");
    }

    /**
     * 從頁面提交圖片,上傳到zimg
     * 
     * @param request
     * @param fileTag
     * @return
     */
    public String uploadImgToZimg(HttpServletRequest request, String fileTag) {
        String imgUrl = "";
        MultipartHttpServletRequest mhs = (MultipartHttpServletRequest) request;
        List<MultipartFile> files = mhs.getFiles(fileTag);
        if (files != null && files.size() > 0) {
            // 上傳到圖片服務器
            MultipartFile f = files.get(0);
            if (f.getSize() == 0)
                return "";
            String tmpFileName = ZimgClient.tmpPath + "/"
                    + f.getOriginalFilename();
            // mkdir("./tmp")
            File tmp = new File(ZimgClient.tmpPath);
            tmp.mkdir();
            tmp = new File(tmpFileName);
            try {
                // tmp.delete();
                f.transferTo(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }

            ZimgResult ret = this.uploadImg(tmpFileName);
            logger.debug(new Gson().toJson(ret));
            if (ret != null && ret.isRet())
                imgUrl = ret.getImageUrl();

            // 刪除文件
            if (tmp != null) {
                tmp.setWritable(true);
                // try {
                // new FileOutputStream(tmp).close();
                // } catch (Exception e) {
                // e.printStackTrace();
                // }
                System.gc();// java'bug,must be gc before delete
                tmp.delete();
            }
        }

        return imgUrl;
    }

    /**
     * 指定文件名,上傳到zimg
     * 
     * @param fileName
     * @return
     */
    public ZimgResult uploadImg(String fileName) {
        String ext = "jpeg";
        int inx = fileName.lastIndexOf(".");
        if (inx > 0)
            ext = fileName.substring(inx + 1);
        String resp = this.Send(ZimgClient.zimgUrl + "upload", fileName, ext);
        return new Gson().fromJson(resp, ZimgResult.class);
    }

    public ZimgResult uploadImgFromUrl(String url) {
        String resp = this.SendFromUrl(url);
        return new Gson().fromJson(resp, ZimgResult.class);
    }

    /**
     * 從指定的URL下載圖片並上傳到zimg服務器
     * 
     * @param zimgUrl
     * @param imgUrl
     * @return
     */
    protected String SendFromUrl(String imgUrl) {

        // 設置文件類型默認值
        String ext = "jpeg";
        String respXML = "";
        try {
            // 獲得connection對象
            logger.debug("zimg server url:" + ZimgClient.zimgUrl);
            URL zimgUL = new URL(ZimgClient.zimgUrl);
            URLConnection zimgConnection = zimgUL.openConnection();
            zimgConnection.setReadTimeout(50000);
            zimgConnection.setConnectTimeout(25000);
            HttpURLConnection zimgUC = (HttpURLConnection) zimgConnection;

            // 設置HTTP協議的消息頭
            logger.debug("zimg set header");
            zimgUC.setRequestMethod("POST");
            zimgUC.setRequestProperty("Connection", "Keep-Alive");
            zimgUC.setRequestProperty("Cache-Control", "no-cache");
            zimgUC.setRequestProperty("Content-Type", ext.toLowerCase());// "jpeg");//
            zimgUC.setRequestProperty("COOKIE", "william");
            zimgUC.setDoOutput(true);
            zimgUC.setDoInput(true);

            logger.debug("zimg connect server.");
            // 與建立服務器連接
            zimgUC.connect();
            // 設置傳輸模式為二進制
            logger.debug("zimg upload image in binary.");
            OutputStream om = zimgUC.getOutputStream();
            // 循環讀取圖片,發送到zimg服務器

            ext = this.writeImage(imgUrl, om);
            logger.debug("image type=" + ext);
            // byte[] buf = new byte[8192];
            // while (true) {
            // int len = in.read(buf);
            // if (len <= 0)
            // break;
            // om.write(buf, 0, len);
            // }

            // 到開輸入(返回信息)流
            InputStreamReader im = new InputStreamReader(
                    zimgUC.getInputStream(), "UTF-8");
            // 循環讀取,知道結束,獲取返回信息
            logger.debug("zimg get response text.");
            char[] bb = new char[8192];
            while (true) {
                int length = im.read(bb);
                if (length == -1)
                    break;
                char[] bc = new char[length];
                for (int i = 0; i < length; i++)
                    bc[i] = bb[i];
                respXML += new String(bc);
            }
            logger.debug("zimg response:" + respXML);
            // 關閉上下行
            im.close();
            zimgUC.disconnect();
        } catch (Exception e) {
            logger.debug("zimg exception :" + e.getMessage());
            e.printStackTrace();
        }

        return respXML;

    }

    /**
     * 返貨圖片類型
     * 
     * @param data
     * @return
     */
    protected String getImageType(byte[] data) {
        String type = null;
        // Png test:
        if (data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
            type = "PNG";
            return type;
        }
        // Gif test:
        if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') {
            type = "GIF";
            return type;
        }
        // JPG test:
        if (data[6] == 'J' && data[7] == 'F' && data[8] == 'I'
                && data[9] == 'F') {
            type = "JPG";
            return type;
        }
        return type;
    }

    /**
     * 獲取URL的輸入流
     * 
     * @param imgUrl
     * @return
     */
    private String writeImage(String imgUrl, OutputStream om) {
        long totalBytes = 0;
        String imgType = "jpeg";
        try {
            // 獲得connection對象
            URL imgUL = new URL(imgUrl);
            URLConnection imgConnection = imgUL.openConnection();
            imgConnection.setReadTimeout(50000);
            imgConnection.setConnectTimeout(25000);
            HttpURLConnection imgUC = (HttpURLConnection) imgConnection;

            // 設置HTTP協議的消息頭
            logger.debug("set header");
            imgUC.setRequestMethod("GET");
            imgUC.setRequestProperty("Connection", "Keep-Alive");
            imgUC.setRequestProperty("Cache-Control", "no-cache");
            // imgUC.setRequestProperty("Content-Type", ext.toLowerCase());//
            // "jpeg");//
            imgUC.setRequestProperty("COOKIE", "GostLiu程序員老劉");
            imgUC.setDoOutput(true);
            imgUC.setDoInput(true);
            InputStream in = imgUC.getInputStream();

            byte[] buf = new byte[8192];
            boolean GotType = false;
            while (true) {
                int len = in.read(buf);
                if (len <= 0)
                    break;
                if (!GotType) {
                    imgType = this.getImageType(buf);
                    GotType = true;
                }
                totalBytes += len;
                om.write(buf, 0, len);
            }
            in.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }
        if (totalBytes > 0)
            return imgType;
        else
            return "";
    }

    /**
     * 將圖片文件上傳到zimg服務器
     * 
     * @param url
     * @param fileName
     * @param ext
     * @return
     */
    protected String Send(String url, String fileName, String ext) {

        if (ext.toLowerCase().compareTo("jpg") == 0)
            ext = "jpeg";
        String respXML = "";
        try {
            // 獲得connection對象
            logger.debug("zimg server url:" + url);
            URL ul = new URL(url);
            URLConnection connection = ul.openConnection();
            connection.setReadTimeout(50000);
            connection.setConnectTimeout(25000);
            HttpURLConnection uc = (HttpURLConnection) connection;

            // 設置HTTP協議的消息頭
            logger.debug("zimg set header");
            uc.setRequestMethod("POST");
            uc.setRequestProperty("Connection", "Keep-Alive");
            uc.setRequestProperty("Cache-Control", "no-cache");
            uc.setRequestProperty("Content-Type", ext.toLowerCase());// "jpeg");//
            uc.setRequestProperty("COOKIE", "william");
            uc.setDoOutput(true);
            uc.setDoInput(true);

            logger.debug("zimg connect server.");
            // 與建立服務器連接
            uc.connect();
            // 設置傳輸模式為二進制
            logger.debug("zimg upload image in binary.");
            OutputStream om = uc.getOutputStream();
            // 循環讀取圖片,發送到zimg服務器
            FileInputStream in = new FileInputStream(fileName);
            byte[] buf = new byte[8192];
            while (true) {
                int len = in.read(buf);
                if (len <= 0)
                    break;
                om.write(buf, 0, len);
            }

            // 到開輸入(返回信息)流
            InputStreamReader im = new InputStreamReader(uc.getInputStream(),
                    "UTF-8");
            // 循環讀取,知道結束,獲取返回信息
            logger.debug("zimg get response text.");
            char[] bb = new char[8192];
            while (true) {
                int length = im.read(bb);
                if (length == -1)
                    break;
                char[] bc = new char[length];
                for (int i = 0; i < length; i++)
                    bc[i] = bb[i];
                respXML += new String(bc);
            }
            logger.debug("zimg response:" + respXML);
            // 關閉上下行
            im.close();
            uc.disconnect();
        } catch (Exception e) {
            logger.debug("zimg exception :" + e.getMessage());
            e.printStackTrace();
        }

        return respXML;

    }

    /********** zimg 服務器返回消息定義 ***********************************/
    public class ZimgError {
        private int code;
        private String message;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

    }

    public class ZimgInfo {
        private String md5;

        public String getMd5() {
            return md5;
        }

        public void setMd5(String md5) {
            this.md5 = md5;
        }

        private int size;

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }
    }

    public class ZimgResult {
        private boolean ret;
        private ZimgInfo info;
        private ZimgError error;

        public ZimgError getError() {
            return error;
        }

        public void setError(ZimgError error) {
            this.error = error;
        }

        public String getImageUrl() {
            if (this.isRet()) {
                return ZimgClient.zimgShareUrl + this.info.getMd5();
            }
            return "";
        }

        public boolean isRet() {
            return ret;
        }

        public void setRet(boolean ret) {
            this.ret = ret;
        }

        public ZimgInfo getInfo() {
            return info;
        }

        public void setInfo(ZimgInfo info) {
            this.info = info;
        }

    }
}

3、用法

該類提供了三個用法:

a、從文件上傳:

 ZimgResult ret0 = new ZimgClient()
                .uploadImg("/var/guttv/logs/tmp/gophercolor.png");

 

b、從url直接上傳

ZimgResult ret = new ZimgClient()
                .uploadImgFromUrl("http://creatives.ftchinese.com/ads/beijing/201509/20150914_cartier_33660.gif");

 

c、從頁面提交上傳,uploadImgToZimg

包含兩個參數HttpServletRequest request和String fileTag,頁面上要存在一個type是file類型的input標簽,其name的值就是這里的fileTag,即下面例子中的posterFile。

本例中使用了spring的MultipartHttpServletRequest,因此調用者要支持spring。

String imgUrl = new ZimgClient().uploadImgToZimg(request,
                    "posterFile");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM