|
package com.ekexiu.console.system.service;
import com.ekexiu.console.service.Upload;
import org.jfw.apt.web.annotation.Path;
import org.jfw.util.JpgUtil;
import org.jfw.util.io.IoUtil;
import java.io.*;
/**
* Created by TT on 2017/8/24.
*/
@Path("/image")
public class ImageService extends Upload {
private File imagePath;
private File tmpPath;
private File orgPath;
private File bannerPath;
private int largeHeadPhotoWidth = 200;
private int largeHeadPhotoHeight = 200;
private int middleHeadPhotoWidth = 200;
private int middleHeadPhotoHeight = 200;
private int smallHeadPhotoWidth = 200;
private int smallHeadPhotoHeight = 200;
private File defaultHeadPhoto;
private File defaultOrgLogo;
private File defaultResourcePhoto;
private byte[] dli;
private byte[] dmi;
private byte[] dsi;
private byte[] dol;
private byte[] dsl;
public File getDefaultHeadPhoto() {
return defaultHeadPhoto;
}
public void setDefaultHeadPhoto(File defaultHeadPhoto) {
this.defaultHeadPhoto = defaultHeadPhoto;
}
public File getDefaultOrgLogo() {
return defaultOrgLogo;
}
public void setDefaultOrgLogo(File defaultOrgLogo) {
this.defaultOrgLogo = defaultOrgLogo;
}
public File getDefaultResourcePhoto() {
return defaultResourcePhoto;
}
public void setDefaultResourcePhoto(File defaultResourcePhoto) {
this.defaultResourcePhoto = defaultResourcePhoto;
}
public File getImagePath() {
return imagePath;
}
public void setImagePath(File imagePath) {
this.imagePath = imagePath;
this.orgPath = new File(this.imagePath, "org");
this.bannerPath = new File(this.imagePath, "banner");
}
public File getTmpPath() {
return tmpPath;
}
public void setTmpPath(File tmpPath) {
this.tmpPath = tmpPath;
}
public File getOrgPath() {
return orgPath;
}
public void setOrgPath(File orgPath) {
this.orgPath = orgPath;
}
public void saveOrgLogo(String id, String fn) throws IOException {
InputStream in = new FileInputStream(new File(this.tmpPath, fn));
try {
IoUtil.copy(in, new FileOutputStream(new File(this.orgPath, id + ".jpg")), false, true);
} finally {
in.close();
}
}
public void saveBannerImage(long id, String fn) throws IOException{
InputStream in = new FileInputStream(new File(this.tmpPath, fn));
try {
IoUtil.copy(in,new FileOutputStream(new File(this.bannerPath, id +".jpg")),false,true);
}finally {
in.close();
}
}
public void saveDefaultOrgLogo(String id) throws IOException {
this.initDefaultImage();
IoUtil.saveStream(new FileOutputStream(new File(this.orgPath, id + ".jpg")), this.dol, true);
}
private byte[] zoomImage(byte[] src, int w, int h) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(src);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JpgUtil.zoom(in, out, w, h);
out.flush();
return out.toByteArray();
}
private void initDefaultImage() {
if (this.dli != null)
return;
synchronized (this) {
if (this.dli != null)
return;
try {
byte[] dd = IoUtil.readStream(new FileInputStream(this.defaultHeadPhoto), true);
this.dli = this.zoomImage(dd, this.largeHeadPhotoWidth, this.largeHeadPhotoHeight);
this.dmi = this.zoomImage(dd, this.middleHeadPhotoWidth, this.middleHeadPhotoHeight);
this.dsi = this.zoomImage(dd, this.smallHeadPhotoWidth, this.smallHeadPhotoHeight);
this.dol = IoUtil.readStream(new FileInputStream(this.defaultOrgLogo), true);
this.dsl = IoUtil.readStream(new FileInputStream(this.defaultResourcePhoto), true);
} catch (IOException e) {
this.dli = null;
this.dmi = null;
this.dsi = null;
this.dol = null;
this.dsl = null;
throw new RuntimeException("init image error", e);
}
}
}
public boolean hasOrgLogo(String id) {
String orgPath = this.orgPath+"/"+id+".jpg";
File file = new File(orgPath);
if(file.exists()){
return true;
}else{
return false;
}
}
}
|