|
package com.ekexiu.portal.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.web.annotation.Path;
import org.jfw.apt.web.annotation.operate.Get;
import org.jfw.apt.web.annotation.operate.Post;
import org.jfw.apt.web.annotation.param.JdbcConn;
import org.jfw.apt.web.annotation.param.PathVar;
import org.jfw.util.StringUtil;
import org.jfw.util.codec.Base64;
import org.jfw.util.io.IoUtil;
import com.ekexiu.portal.dao.AuthImageDao;
import com.ekexiu.portal.po.AuthImage;
@Path("/authImage")
public class AuthImageService {
private File authImgPath;
private File tmpPath;
private String dateFormat = "yyyyMMdd";
@Autowrie
private AuthImageDao authImageDao;
public File getAuthImgPath() {
return authImgPath;
}
public void setAuthImgPath(File authImgPath) {
this.authImgPath = authImgPath;
}
public File getTmpPath() {
return tmpPath;
}
public void setTmpPath(File tmpPath) {
this.tmpPath = tmpPath;
}
public String getDateFormat() {
return dateFormat;
}
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
public AuthImageDao getAuthImageDao() {
return authImageDao;
}
public void setAuthImageDao(AuthImageDao authImageDao) {
this.authImageDao = authImageDao;
}
public void insertImg(@JdbcConn(true) Connection con, String authApplyId, String fn) throws SQLException, IOException{
String authImageId = StringUtil.buildUUID();
SimpleDateFormat df = new SimpleDateFormat(this.dateFormat);
String date = df.format(new Date());
File dateFile = new File(this.authImgPath + "/" + date);
if (!dateFile.exists()) {
dateFile.mkdirs();
}
InputStream in = new FileInputStream(new File(this.tmpPath, fn));
try {
IoUtil.copy(in, new FileOutputStream(new File(dateFile, authImageId + ".jpg")), false, true);
} finally {
in.close();
}
AuthImage authImage = new AuthImage();
authImage.setAuthImageId(authImageId);
authImage.setAuthApplyId(authApplyId);
authImage.setAuthSrc(date + "/" + authImageId + ".jpg");
this.authImageDao.insert(con, authImage);
}
@Post
@Path
public String insert(@JdbcConn(true) Connection con, String authApplyId, String base64) throws SQLException, IOException{
String authImageId = StringUtil.buildUUID();
SimpleDateFormat df = new SimpleDateFormat(this.dateFormat);
String date = df.format(new Date());
File dateFile = new File(this.authImgPath + "/" + date);
if (!dateFile.exists()) {
dateFile.mkdirs();
}
Base64 bs64 = new Base64();
byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
String imageSrc = authImageId + ".jpg";
FileOutputStream fos = new FileOutputStream(dateFile + "/" + imageSrc);
try {
fos.write(bs);
fos.flush();
} finally {
fos.close();
}
AuthImage authImage = new AuthImage();
authImage.setAuthImageId(authImageId);
authImage.setAuthApplyId(authApplyId);
authImage.setAuthSrc(date + "/" + imageSrc);
this.authImageDao.insert(con, authImage);
return authImageId;
}
@Get
@Path("/byApply/{id}")
public List<AuthImage> query(@JdbcConn Connection con, @PathVar String id)throws SQLException{
return this.authImageDao.query(con,id);
}
}
|