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();
}
}
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();
}
}