package org.qhit.source.struts.action;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;
import org.qhit.source.common.common;
import org.qhit.source.dao.IUploadDAO;
import org.qhit.source.service.commService;
import org.qhit.source.vo.Phototype;
import org.qhit.source.vo.Showimg;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class photoAction extends ActionSupport {
//注入数据层的dao
@Resource private IUploadDAO iudao;
private HttpServletRequest request;
private HttpServletResponse response;
//查询相册类型
public void seacherType() throws Exception {
// TODO Auto-generated method stub
request = ServletActionContext.getRequest();
HttpSession session=request.getSession();
request.setCharacterEncoding("gbk");
List<Phototype> list = iudao.getTypeList();
session.setAttribute("photoType", list);
}
//查询所有相片
public void searchAllPhoto() throws Exception{
request = ServletActionContext.getRequest();
HttpSession session=request.getSession();
request.setCharacterEncoding("gbk");
List<Showimg> list=commService.ChangeDate(iudao.getPhotoAll());
session.setAttribute("photoAll", list);
}
//首页显示的控制类
public String showPhotoExecute() throws Exception{
this.seacherType();
this.searchAllPhoto();
return Action.SUCCESS;
}
//图片上传
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
private int typeid; //上传的位置
//图片上传
public String uploadPhoto(){
InputStream in=null;
OutputStream out=null;
try{
String dir=ServletActionContext.getRequest().getRealPath("/upload");
for(int i=0; i<file.size(); i++){
in = new FileInputStream(file.get(i));
File upfile=new File(dir,fileFileName.get(i));
out = new FileOutputStream(upfile);
byte[] buff=new byte[1024];
int length=0;
while((length=in.read(buff))!=-1){
out.write(buff,0,length);
}
//见图片信息插入到数据库
savePhotoInfo(upfile);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
in.close();
out.close();
}catch(IOException e){e.printStackTrace();}
}
return Action.SUCCESS;
}
//将上传的图片信息插入到数据库
public void savePhotoInfo(File file){
try{
BufferedImage buff = ImageIO.read(file);
Showimg img=new Showimg();
//得到图片的信息
int height=buff.getHeight();
int width=buff.getWidth();
if(width<700){
img.setWidth(width);
}else{
img.setWidth(600);
}
if(height<600){
img.setHeight(height);
}else{
img.setHeight(500);
}
img.setPhotoname(file.getName());
img.setPhototype((Phototype)iudao.load(typeid, Phototype.class));
iudao.addObject(img);
}catch(IOException e){
e.printStackTrace();
System.out.println("---- "+file.getName()+" -----文件上传失败。");
}
}
//创建相册类型
private Phototype ptype;
public String createTypeExecute() throws Exception{
iudao.addObject(ptype);
//跟新类型
this.seacherType();
return Action.SUCCESS;
}
//按相册类型查询图片
public String searchPartPhoto() throws Exception{
request = ServletActionContext.getRequest();
HttpSession session=request.getSession();
request.setCharacterEncoding("gbk");
int tid=Integer.parseInt(request.getParameter("typeid"));
if(tid==0){
this.searchAllPhoto();
}else{
List<Showimg> list = commService.ChangeDate(iudao.getPhoto(tid));
session.setAttribute("photoAll", list);
}
return Action.SUCCESS;
}
//上一张和下一张时输出图片信息
@SuppressWarnings("unchecked")
public void findPhoto()throws Exception{
request=ServletActionContext.getRequest();
response=ServletActionContext.getResponse();
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk");
PrintWriter out=response.getWriter();
String pname = request.getParameter("imgName");
String par=request.getParameter("parameter");
List<Showimg> list = (List<Showimg>)request.getSession().getAttribute("photoAll");
if(pname!=null){
for(int i=0; i<list.size(); i++){
Showimg img=(Showimg)list.get(i);
if(img.getPhotoname().equals(pname)){
JSONObject json=new JSONObject();
json.put("height", img.getHeight());
json.put("width", img.getWidth());
json.put("sdate", common.formatDate(img.getUdate()));
out.write(json.toString());
}
}
}
//输出层中第一张图片的信息
if(par!=null&&"first".equals(par)){
Showimg img=(Showimg)list.get(0);
JSONObject json=new JSONObject();
json.put("height", img.getHeight());
json.put("width", img.getWidth());
json.put("sdate", common.formatDate(img.getUdate()));
out.write(json.toString());
}
}
public Phototype getPtype() {
return ptype;
}
public void setPtype(Phototype ptype) {
this.ptype = ptype;
}
public int getTypeid() {
return typeid;
}
public void setTypeid(int typeid) {
this.typeid = typeid;
}
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
}