コード例 #1
0
  @Override
  /**
   * Returns the size of the image in bytes.
   *
   * @param sourceString the image link.
   * @return the file size in bytes of the image link provided; -1 if the size isn't available or
   *     exceeds the max allowed image size.
   */
  public int getImageSize(String sourceString) {
    int length = -1;
    try {

      URL url = new URL(sourceString);
      String protocol = url.getProtocol();
      if (protocol.equals("http") || protocol.equals("https")) {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        length = connection.getContentLength();
        connection.disconnect();
      } else if (protocol.equals("ftp")) {
        FTPUtils ftp = new FTPUtils(sourceString);
        length = ftp.getSize();
        ftp.disconnect();
      }

      if (length > imgMaxSize) {
        length = -1;
      }
    } catch (Exception e) {
      logger.debug("Failed to get the length of the image in bytes", e);
    }
    return length;
  }
コード例 #2
0
 /**
  * Returns true if the content type of the resource pointed by sourceString is an image.
  *
  * @param sourceString the original image link.
  * @return true if the content type of the resource pointed by sourceString is an image.
  */
 @Override
 public boolean isDirectImage(String sourceString) {
   boolean isDirectImage = false;
   try {
     URL url = new URL(sourceString);
     String protocol = url.getProtocol();
     if (protocol.equals("http") || protocol.equals("https")) {
       HttpURLConnection connection = (HttpURLConnection) url.openConnection();
       isDirectImage = connection.getContentType().contains("image");
       connection.disconnect();
     } else if (protocol.equals("ftp")) {
       if (sourceString.endsWith(".png")
           || sourceString.endsWith(".jpg")
           || sourceString.endsWith(".gif")) {
         isDirectImage = true;
       }
     }
   } catch (Exception e) {
     logger.debug("Failed to retrieve content type information for" + sourceString, e);
   }
   return isDirectImage;
 }