根据图片路径返回图片的字节流byte[]?

新手上路,请多包涵

目前从c#项目转到java,有一个功能是根据图片路径返回图片的字节流,实现如下:
public static string GetImageByUrl(string imagePath)

    {
        System.Net.WebRequest webreq = System.Net.WebRequest.Create(imagePath);

        int port = TypeUtil.ToInt32(CommonUtil.GetAppSetting("ProxyPort"));
        webreq.Proxy = new WebProxy(CommonUtil.GetAppSetting("Proxy"), port);
        // 超时时间 10s
        webreq.Timeout = 10000;
        System.Net.WebResponse webres = webreq.GetResponse();
        System.IO.Stream stream = webres.GetResponseStream();
        System.Drawing.Image img1 = System.Drawing.Image.FromStream(stream);
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img1);

        return ImgToBase64String(bmp);
    }

    //图片转为base64编码的字符串
    public static string ImgToBase64String(Bitmap bmp)
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            return Convert.ToBase64String(arr);
        }
        catch (Exception ex)
        {
            return null;
        }
    }
用java进行重写如下


   
/**
 * 从URL中获取输出流
 *
 * @param urlPath
 * @return
 */
public static byte[] getImageFromURL(String urlPath) {
    byte[] data = null;
    InputStream inputStream = null;
    HttpURLConnection conn = null;
    try {
        URL url = new URL(urlPath);
        conn = (HttpURLConnection) url.openConnection(getProxy());
        conn.setDoInput(true);
        conn.setRequestMethod(HttpMethod.GET.name());
        conn.setConnectTimeout(10000);
        inputStream = conn.getInputStream();
        if (conn.getResponseCode() == 200) {
            data = readInputStream(inputStream);
        } else {
            data = null;
        }
    } catch (MalformedURLException e) {
        CLogger.error("getImageFromURL", e);
    } catch (IOException e) {
        CLogger.error("getImageFromURL", e);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            CLogger.error("getImageFromURL", e);
        }
        conn.disconnect();
    }
    return data;
}

/**
 * 读取输入流
 *
 * @param is
 * @return
 */
public static byte[] readInputStream(InputStream is) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length = -1;
    try {
        while ((length = is.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        baos.flush();
    } catch (IOException e) {
        CLogger.error("readInputStream", e);
    }
    byte[] data = baos.toByteArray();
    try {
        is.close();
        baos.close();
    } catch (IOException e) {
        CLogger.error("readInputStream", e);
    }
    return data;
}

  final byte[] image = getImageFromURL(request.getImageUrl());
  String imageData = "";
   if (ArrayUtil.isNotEmpty(image)) {
            //base64加密
    imageData = Base64.encodeBase64String(image);
 }
 
 
 最后的结果总是不同,想问一下我该如何写java方法呢?谢谢各位

阅读 3.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题