package com.tgod.core.task;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgod.album.model.TgodPhoto;
import com.tgod.album.model.TgodPhotoalbum;
import com.tgod.album.service.TgodPhotoalbumService;
import com.tgod.core.utils.FileUtils;
@Component
public class FlightTrainTask {
@Autowired
private TgodPhotoalbumService photoAlbumService;//圖集處理
//@Scheduled(cron = "0/5 * * * * ? ") // 間隔5秒執行
@Scheduled(cron = "0 0 0 * * ?") // 淩晨12點執行
public void taskCycle() throws IOException {
System.out.println("使用SpringMVC架構配置定時任務");
JSONArray jsonArray = new JSONArray();
TgodPhotoalbum photoAlbum = new TgodPhotoalbum();
photoAlbum.setPageSize(100000);
//photoAlbum.setStatus((byte) 1);
LinkedList<TgodPhotoalbum> albums = photoAlbumService.queryAlbum(photoAlbum);
if(albums!=null && albums.size()>0){
for(int i=0;i<albums.size();i++){
TgodPhotoalbum albumy = albums.get(i);
TgodPhoto tgodPhotos = new TgodPhoto();
tgodPhotos.setAlbumid(albumy.getId());
tgodPhotos.setFigureFlag(3);
LinkedList<TgodPhoto> ptholst = photoAlbumService.getAlbumPhotos(tgodPhotos);
if(ptholst!=null){
for(int j=0;j<ptholst.size();j++){
String bigimgPath = ptholst.get(j).getBigImage();
Integer figureFlags = ptholst.get(j).getFigureFlag();
if(figureFlags==null || figureFlags==0 || figureFlags==1){
if(bigimgPath.indexOf("siteupload")<=0){
String basePath ="";
if(bigimgPath.indexOf("product")>0){
if(bigimgPath.indexOf("封面")>0){
basePath = bigimgPath;
}else{
basePath = bigimgPath.substring(0, bigimgPath.lastIndexOf("/"))+"/nowaterbackup"+bigimgPath.substring(bigimgPath.lastIndexOf("/"),bigimgPath.length());
}
}else{
basePath = bigimgPath;
}
//new一個URL對象 從網絡讀取圖檔
URL url = new URL("http://image.qingdouke.com"+bigimgPath.replace("upload_big", "upload_original"));
//打開連結
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設定請求方式為"GET"
conn.setRequestMethod("GET");
//逾時響應時間為5秒
conn.setConnectTimeout(5 * 1000);
if(conn.getResponseCode()==200){
//通過輸入流擷取圖檔資料
InputStream inStream = conn.getInputStream();
//得到圖檔的二進制資料,以二進制封裝得到資料,具有通用性
byte[] data = null;
try {
data = readInputStream(inStream);
} catch (Exception e) {
System.out.println("讀取失敗");
e.printStackTrace();
}
String picSy="/home/tgod-image/upload_new/"+albumy.getPhotographerid()+"_"+albumy.getModelid()+"/"+albumy.getId()+"/upload_original"+bigimgPath.substring(bigimgPath.lastIndexOf("/"),bigimgPath.length());
//new一個檔案對象用來儲存圖檔,預設儲存目前工程根目錄
File imageFile = new File(picSy);
if (!imageFile.getParentFile().exists()) {
imageFile.getParentFile().mkdirs();
}
//建立輸出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//寫入資料
outStream.write(data);
//關閉輸出流
outStream.close();
TgodPhoto tgodPhoto = new TgodPhoto();
tgodPhoto.setFigureFlag(3);
tgodPhoto.setId(ptholst.get(j).getId());
photoAlbumService.updatePhotoByPrimaryKey(tgodPhoto);
}
}else{
String basePathup = bigimgPath.replace("upload_big", "upload_original");
String picSy="/home/tgod-image/upload_new/"+albumy.getPhotographerid()+"_"+albumy.getModelid()+"/"+albumy.getId()+"/upload_original"+bigimgPath.substring(bigimgPath.lastIndexOf("/"),bigimgPath.length());
System.out.println("picSy======================"+picSy);
File fileFrom = new File("/home/tgod-image"+basePathup);
File _picSy = new File(picSy);
if (!_picSy.getParentFile().exists()) {
_picSy.getParentFile().mkdirs();
}
if(fileFrom.exists()){
System.out.println("圖檔出現tgod-image"+fileFrom);
FileUtils.copyFile(fileFrom, _picSy);
TgodPhoto tgodPhoto = new TgodPhoto();
tgodPhoto.setFigureFlag(3);
tgodPhoto.setId(ptholst.get(j).getId());
photoAlbumService.updatePhotoByPrimaryKey(tgodPhoto);
}
}
}
}
}
}
}
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//建立一個Buffer字元串
byte[] buffer = new byte[1024];
//每次讀取的字元串長度,如果為-1,代表全部讀取完畢
int len = 0;
//使用一個輸入流從buffer裡把資料讀取出來
while( (len=inStream.read(buffer)) != -1 ){
//用輸出流往buffer裡寫入資料,中間參數代表從哪個位置開始讀,len代表讀取的長度
outStream.write(buffer, 0, len);
}
//關閉輸入流
inStream.close();
//把outStream裡的資料寫入記憶體
return outStream.toByteArray();
}
}