概述
package com.lehecai.oa.action.survey;
import com.lehecai.core.YesNoStatus;
import com.lehecai.core.api.ApiRequestOrder;
import com.lehecai.core.api.bean.query.IQueryProp;
import com.lehecai.core.api.bean.query.QueryOperator;
import com.lehecai.core.api.bean.query.impl.SingleQueryProp;
import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreFileUtils;
import com.lehecai.oa.action.baseinfo.BaseAction;
import com.lehecai.oa.bean.PageBean;
import com.lehecai.oa.bean.UserSessionBean;
import com.lehecai.oa.constant.Global;
import com.lehecai.oa.domain.components.User;
import com.lehecai.oa.domain.survey.Option;
import com.lehecai.oa.domain.survey.Question;
import com.lehecai.oa.entity.serializer.survey.OptionPropConstant;
import com.lehecai.oa.entity.serializer.survey.QuestionPropConstant;
import com.lehecai.oa.entity.survey.OptionQuery;
import com.lehecai.oa.entity.survey.QuestionQuery;
import com.lehecai.oa.service.survey.VoteService;
import com.lehecai.oa.types.OptionType;
import com.lehecai.oa.utils.PageUtil;
import com.opensymphony.xwork2.Action;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.util.WebUtils;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 题目管理
* Created by likunpeng on 2014/9/2.
*/
public class QuestionAction extends BaseAction {
private static final long serialVersionUID = -9158812344915120772L;
private File pic;
private String saveDir;
private Long voteId;
private String picFileName;
private Question question;
private Option option;
private int outputWidth = 160; // 默认输出图片宽
private int outputHeight = 160; // 默认输出图片高
private Integer optionTypeValue;
private List<Question> questionList;
private VoteService voteService;
private List<Option> optionList; //选项
public String handle() {
logger.info("进入题目查询列表!");
QuestionQuery questionQuery = new QuestionQuery();
List<IQueryProp> queryPropItemList = new ArrayList<IQueryProp>();
if (voteId != null && voteId != 0L) {
queryPropItemList.add(new SingleQueryProp(QuestionPropConstant.PROP_VOTE_ID, QueryOperator.EQUAL, String.valueOf(voteId)));
}
if (optionTypeValue != null && optionTypeValue > -1) {
queryPropItemList.add(new SingleQueryProp(QuestionPropConstant.PROP_OPTION_TYPE, QueryOperator.EQUAL, String.valueOf(optionTypeValue.intValue())));
}
//查询有效的
queryPropItemList.add(new SingleQueryProp(QuestionPropConstant.PROP_VALID, QueryOperator.EQUAL, String.valueOf(YesNoStatus.YES.getValue())));
questionQuery.setQueryPropItemList(queryPropItemList);
List<ApiRequestOrder> orderList = new ArrayList<ApiRequestOrder>();
questionQuery.setOrderList(orderList);
PageBean pageBean = super.getPageBean();
questionList = voteService.queryQuestion(questionQuery, pageBean);
super.setPageString(PageUtil.getPageString(ServletActionContext.getRequest(), pageBean));
logger.info("题目查询列表结束!");
return "list";
}
public String input() {
logger.info("进入添加修改页面!");
if (voteId == null || voteId == 0L) {
logger.error("投票id为空,不能添加题目!");
super.setErrorMessage("投票id为空,不能添加题目!");
return "failure";
}
if (question != null && question.getId() != null && question.getId() != 0L) {
question = voteService.getQuestion(question.getId());
OptionQuery optionQuery = new OptionQuery();
List<IQueryProp> queryPropItemList = new ArrayList<IQueryProp>();
queryPropItemList.add(new SingleQueryProp(OptionPropConstant.PROP_QUESTION_ID, QueryOperator.EQUAL, String.valueOf(question.getId().longValue())));
optionQuery.setQueryPropItemList(queryPropItemList);
List<ApiRequestOrder> orderList = new ArrayList<ApiRequestOrder>();
optionQuery.setOrderList(orderList);
optionList =voteService.queryOption(optionQuery, null);
} else {
question = new Question();
question.setOptionType(OptionType.RADIO_TYPE);
}
logger.info("添加修改页面结束!");
return "inputForm";
}
public String manage() {
if (optionTypeValue == null) {
logger.error("投票类型为空!");
super.setErrorMessage("投票类型为空!");
return "failure";
}
if (question == null) {
logger.error("题目信息为空,请联系管理员!");
super.setErrorMessage("题目信息为空,请联系管理员!");
return "failure";
}
if (optionTypeValue.intValue() != OptionType.FILL_IN_TYPE.getValue()) {
if (optionList == null || optionList.size() == 0) {
logger.error("选项为空,不能保存!");
super.setErrorMessage("选项为空,不能保存!");
return "failure";
}
}
UserSessionBean userSessionBean = (UserSessionBean) super.getSession().get(Global.USER_SESSION);
User user = null;
if (userSessionBean != null && userSessionBean.getUser() != null) {
user = userSessionBean.getUser();
} else {
logger.error("用户信息获取失败!");
super.setErrorMessage("用户信息获取失败!");
return "failure";
}
OptionType optionType = OptionType.getItem(optionTypeValue);
if (question != null && question.getId() != null && question.getId() != 0L) { //修改
Question tempQuestion = voteService.getQuestion(question.getId());
OptionQuery optionQuery = new OptionQuery();
List<IQueryProp> queryPropItemList = new ArrayList<IQueryProp>();
queryPropItemList.add(new SingleQueryProp(OptionPropConstant.PROP_QUESTION_ID, QueryOperator.EQUAL, String.valueOf(question.getId())));
optionQuery.setQueryPropItemList(queryPropItemList);
List<Option> tempOptionList = voteService.queryOption(optionQuery, null);
//删除之前保存的选项
voteService.delOptionBatch(tempOptionList);
//重新保存
tempQuestion.setName(question.getName());
tempQuestion.setOptionList(optionList);
tempQuestion.setOptionType(optionType);
//tempQuestion.setOrderValue(question.getOrderValue());
tempQuestion.setMostOptionNum(question.getMostOptionNum());
tempQuestion.setUpdatedTime(new Date());
tempQuestion.setOperator(user.getUserName());
Question resultQuestion = voteService.manageQuestionAndOption(tempQuestion);
if (resultQuestion == null) {
logger.error("修改保存题目信息异常,请联系管理员");
super.setErrorMessage("修改保存题目信息异常,请联系管理员");
return "failure";
}
} else { //添加
question.setOptionList(optionList);
question.setOptionType(optionType);
//设为有效
question.setValid(YesNoStatus.YES);
question.setCreatedTime(new Date());
question.setCreater(user.getUserName());
Question resultQuestion = voteService.manageQuestionAndOption(question);
if (resultQuestion == null) {
logger.error("添加保存题目信息异常,请联系管理员");
super.setErrorMessage("添加保存题目信息异常,请联系管理员");
return "failure";
}
}
if (voteId != null && voteId != 0L) {
super.setForwardUrl("/survey/question.do&voteId=" + voteId);
} else {
super.setForwardUrl("/survey/question.do");
}
return "success";
}
public String del() {
logger.info("进入删除题目!");
int rc = 0;
String msg = "";
HttpServletResponse response = ServletActionContext.getResponse();
JSONObject json = new JSONObject();
if (question == null || question.getId() == null || question.getId() == 0L) {
rc = 1;
logger.error("删除的题目id为空!");
msg = "删除的题目id为空!";
json.put("code", rc);
json.put("msg", msg);
writeRs(response, json);
return Action.NONE;
}
UserSessionBean userSessionBean = (UserSessionBean) super.getSession().get(Global.USER_SESSION);
User user = null;
if (userSessionBean != null && userSessionBean.getUser() != null) {
user = userSessionBean.getUser();
} else {
logger.error("用户信息获取失败!");
super.setErrorMessage("用户信息获取失败!");
return "failure";
}
Question tempQuestion = voteService.getQuestion(question.getId());
tempQuestion.setOperator(user.getUserName());
tempQuestion.setValid(YesNoStatus.NO);
Question resultQuestion = voteService.manageQuestion(tempQuestion);
if (resultQuestion == null) {
rc = 1;
msg = "删除失败!";
logger.error(msg);
json.put("code", rc);
json.put("msg", msg);
writeRs(response, json);
return Action.NONE;
}
msg="删除成功";
logger.info("删除题目结束!");
json.put("code", rc);
json.put("msg", msg);
writeRs(response, json);
return Action.NONE;
}
public String view() {
logger.info("进入查看此次投票所有题目页面!");
if (voteId == null || voteId == 0L) {
logger.error("投票id为空,不能查看此次投票所有题目!");
super.setErrorMessage("投票id为空,不能查看此次投票所有题目!");
return "failure";
}
QuestionQuery questionQuery = new QuestionQuery();
List<IQueryProp> queryPropItemList = new ArrayList<IQueryProp>();
queryPropItemList.add(new SingleQueryProp(QuestionPropConstant.PROP_VOTE_ID, QueryOperator.EQUAL, String.valueOf(voteId)));
//查询有效的
queryPropItemList.add(new SingleQueryProp(QuestionPropConstant.PROP_VALID, QueryOperator.EQUAL, String.valueOf(YesNoStatus.YES.getValue())));
questionQuery.setQueryPropItemList(queryPropItemList);
List<ApiRequestOrder> orderList = new ArrayList<ApiRequestOrder>();
questionQuery.setOrderList(orderList);
PageBean pageBean = super.getPageBean();
questionList = voteService.queryQuestion(questionQuery, pageBean);
super.setPageString(PageUtil.getPageString(ServletActionContext.getRequest(), pageBean));
logger.info("查看此次投票所有题目结束!");
return "list";
}
public String optionView() {
logger.info("进入查看选项页面!");
if (voteId == null || voteId == 0L) {
logger.error("投票id为空,不能查看此次投票所有题目!");
super.setErrorMessage("投票id为空,不能查看此次投票所有题目!");
return "failure";
}
if (question == null || question.getId() == null || question.getId() == 0L) {
logger.error("题目id为空,不能查看此题目中的选项!");
super.setErrorMessage("题目id为空,不能查看此题目中的选项!");
return "failure";
}
OptionQuery optionQuery = new OptionQuery();
List<IQueryProp> queryPropItemList = new ArrayList<IQueryProp>();
queryPropItemList.add(new SingleQueryProp(OptionPropConstant.PROP_QUESTION_ID, QueryOperator.EQUAL, String.valueOf(question.getId())));
optionQuery.setQueryPropItemList(queryPropItemList);
List<ApiRequestOrder> orderList = new ArrayList<ApiRequestOrder>();
optionQuery.setOrderList(orderList);
PageBean pageBean = super.getPageBean();
optionList = voteService.queryOption(optionQuery, pageBean);
super.setPageString(PageUtil.getPageString(ServletActionContext.getRequest(), pageBean));
logger.info("查看选项页面结束!");
return "optionView";
}
public String show() {
logger.info("上传图片");
return "show";
}
public String upload() {
logger.info("开始上传!");
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
int rc = 0;
String msg = "";
JSONObject rs = new JSONObject();
String picUrl = null;
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DATE);
try {
picUrl = saveDir + (saveDir.endsWith("/") ? "" : "/") + "vote" + "/" + year+"/" + month + "/" + day + "/" + (new Date()).getTime() + picFileName.substring(picFileName.lastIndexOf(".")).trim().toLowerCase();
String compressFileName = saveDir + (saveDir.endsWith("/") ? "" : "/") + "vote" + "/" + year+"/" + month + "/" + day + "/" + "compress_" + (new Date()).getTime() + picFileName.substring(picFileName.lastIndexOf(".")).trim().toLowerCase();
String webRoot = WebUtils.getRealPath(request.getSession().getServletContext(), "");
CoreFileUtils.uploadFile(pic, (webRoot + picUrl), CharsetConstant.CHARSET_UTF8);
//压缩图片
compressPic(pic, true, outputWidth, outputHeight, webRoot, compressFileName);
if (option != null && option.getId() != null && option.getId() != 0L) {
Option tempOption = voteService.getOption(option.getId());
if (tempOption != null) {
tempOption.setPicPath(picUrl);
tempOption.setCompressPicPath(compressFileName);
Option resultOption = voteService.manageOption(tempOption);
if (resultOption == null) {
rc = 1;
logger.error("保存图片路径失败!");
msg = "保存图片路径失败!";
rs.put("code", rc);
rs.put("msg", msg);
writeRs(response, rs);
return Action.NONE;
}
} else {
rc = 1;
logger.error("根据optionId没有查询到该选项!");
msg = "根据optionId没有查询到该选项!";
rs.put("code", rc);
rs.put("msg", msg);
writeRs(response, rs);
return Action.NONE;
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
rs.put("code", rc);
rs.put("msg", msg);
rs.put("fileUrl", picUrl);
super.writeRs(response, rs);
logger.info("上传结束!");
return Action.NONE;
}
// 图片处理
public boolean compressPic(File file, boolean proportion, int outputWidth, int outputHeight, String outputDir, String outputFileName) {
try {
//获得源文件
if (!file.exists()) {
return false;
}
Image img = ImageIO.read(file);
// 判断图片格式是否正确
if (img.getWidth(null) == -1) {
return false;
} else {
int newWidth; int newHeight;
// 判断是否是等比缩放
if (proportion == true) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
// 根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = outputWidth; // 输出的图片宽度
newHeight = outputHeight; // 输出的图片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
* 优先级比速度高 生成的图片质量比较好 但速度慢
*/
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException e) {
logger.error("生成缩略图异常," + e.getMessage());
}
return true;
}
public String getPicFileName() {
return picFileName;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
public String getSaveDir() {
return saveDir;
}
public void setSaveDir(String saveDir) {
this.saveDir = saveDir;
}
public Long getVoteId() {
return voteId;
}
public void setVoteId(Long voteId) {
this.voteId = voteId;
}
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
public List<OptionType> getOptionTypeList() {
return OptionType.getItems();
}
public VoteService getVoteService() {
return voteService;
}
public void setVoteService(VoteService voteService) {
this.voteService = voteService;
}
public Integer getOptionTypeValue() {
return optionTypeValue;
}
public void setOptionTypeValue(Integer optionTypeValue) {
this.optionTypeValue = optionTypeValue;
}
public List<Question> getQuestionList() {
return questionList;
}
public void setQuestionList(List<Question> questionList) {
this.questionList = questionList;
}
public List<Option> getOptionList() {
return optionList;
}
public void setOptionList(List<Option> optionList) {
this.optionList = optionList;
}
public List<OptionType> getOptionTypeItem() {
return OptionType.getItemsForQuery();
}
public Integer getFillItemValue() {
return OptionType.FILL_IN_TYPE.getValue();
}
public Integer getMultipletValue() {
return OptionType.MULTIPLET_TYPE.getValue();
}
public Option getOption() {
return option;
}
public void setOption(Option option) {
this.option = option;
}
}
最后
以上就是纯情荷花为你收集整理的java实现图片的缩放问题的全部内容,希望文章能够帮你解决java实现图片的缩放问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复