我正在尝试在此链接上下载高分辨率产品图片
单击下载高分辨率照片时,我可以轻松下载,但是当我尝试复制图像 URL,然后从其他选项卡下载时,我得到 3005_75310.jpg 不存在。
所以我试图从第一个请求中查看请求标头并将它们设置在我的 URL java 对象中,但是创建的文件是空的,有人知道吗?
public static void saveImage(String imageUrl, String destinationFile) {
URL url;
try {
url = new URL(imageUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
uc.setRequestProperty("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.3");
uc.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
uc.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
uc.setRequestProperty("Connection", "keep-alive");
uc.setRequestProperty(
"Referer",
"http://www.hookerfurniture.com/index.cfm/furniture/furniture-catalog.show-product/American-furniture/3005-75310/spindle-back-side-chair---ebony.cfm");
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();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}