2

http 呼び出しの応答ヘッダーを表示する方法はありますか? より具体的に説明します。リソース (Web 上の URL で示される) がいつ変更されたかを確認する必要があります。最後の変更の日付を知っているので、ダウンロードするかどうかを決定します。その方法は、http 呼び出しのヘッダーを監視することだと思います。助言がありますか ?

4

1 に答える 1

1

これは、ユーザー定義の Java クラスを使用して簡単に達成できます。これはクラスのサンプルで、前のステップからの画像(画像の URL) と呼ばれる 1 つの入力行を想定しています。次のコードを使用して、ユーザー定義の Java クラスを追加します。

import java.util.*;
import java.lang.System.*;
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.Date;
import java.util.Calendar;


public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException, Exception
{

  //First, get a row from the default input hop
  Object[] r = getRow();

  //If the row object is null, we are done processing.
  if (r == null) {
      setOutputDone();
      return false;
  }



String filesSavePath = getParameter("filesSavePath")+"/tmp/pictures";
//remove "file://" from filesSavePath, otherwise gives a file io exception, file not found
filesSavePath =  filesSavePath.replace("file://","");

String picture = get(Fields.In, "picture").getString(r);

//get the last chunk of picture as filename to save in disk
String filePictureName = picture.substring(picture.lastIndexOf('/') + 1);
String fileFullPath = filesSavePath+ "/"+ filePictureName;

//lets get the headers from picture
try {  

    boolean fileExists = new File(fileFullPath).isFile();

    //if picture do not exists save it
    if(fileExists != true){
        saveImage(picture, fileFullPath);
        System.out.println("new picture saved = " + filePictureName);
        System.out.println("*******************************");
    }

    //if file exists compare date last modified file from header, younger than yesterday.
    //if true save it.
    else{
        //get the last-modified header
        URL url = new URL(picture);
        URLConnection conn = url.openConnection();

        long lastModified = conn.getLastModified();         

        //get last-modified date
        Date lastModifiedDate = new Date(lastModified);

        //get yesterday date
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date yesterdayDate = cal.getTime();


        //today just for testing
        //Date today = new Date();
        //boolean  dateCompare = today.after(yesterdayDate);


        boolean  dateCompare = lastModifiedDate.after(yesterdayDate);           

        //if true save it!
        if(dateCompare == true){
            saveImage(picture, fileFullPath);
            System.out.println("new picture saved(last modified after yesterday) = " + filePictureName);
        }


        System.out.println("picture = " + picture);
        System.out.println("last modified after yesterday = " + dateCompare);
        System.out.println("last modified = " + lastModifiedDate);
        //System.out.println("today = " + today);
        System.out.println("yesterday date = " + yesterdayDate);
        System.out.println("*******************************");
    }



} 
catch (Exception e) {
System.out.println("error: " + e);
String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e);
System.out.println("fullStackTrace: " + fullStackTrace);

}

return true;


}






    private static void saveImage(String imageUrl, String destinationFile) throws IOException {
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}
于 2013-08-13T06:54:36.790 に答える