有没有办法查看http调用的响应头?我会更具体。我需要查看资源(由网络上的 URL 指向)何时被修改。知道最后修改的日期,我决定是否下载它。我认为这样做的一种方法是查看 http 调用的标头。有什么建议么 ?
问问题
990 次
1 回答
1
这将使用用户定义的 Java 类轻松完成。在这里,您是一个类示例,期望上一步中的一个输入行称为图片(图片的 url)。现在使用以下代码添加您的用户定义的 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 回答