();
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 ListgetOptionTypeList() {
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 ListgetQuestionList() {
return questionList;
}
public void setQuestionList(ListquestionList) {
this.questionList = questionList;
}
public ListgetOptionList() {
return optionList;
}
public void setOptionList(ListoptionList) {
this.optionList = optionList;
}
public ListgetOptionTypeItem() {
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;
}
}