effectaction.java
来自「一个免费wap站」· Java 代码 · 共 245 行
JAVA
245 行
package com.eline.wap.catalog.struts;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.eline.wap.catalog.client.CatalogHelper;
import com.eline.wap.catalog.exceptions.CatalogException;
import com.eline.wap.catalog.model.EffectItem;
import com.eline.wap.catalog.model.Item;
import com.eline.wap.common.util.AppKeys;
import com.eline.wap.common.util.AppLogger;
import com.eline.wap.common.util.AppSettings;
import com.eline.wap.common.util.StringUtils;
/**
*
* @author Lucifer
*
*/
public class EffectAction extends Action {
private static final int ACTION_CREATE = 0;
private static final int ACTION_UPDATE = 1;
private static final int ACTION_UPLOAD = 3; // 上传操作
/**
*
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("EffectAction.execute()...");
boolean isOK = false;
int webAction = StringUtils.getInt(request.getParameter("webAction"), -1);
if (form instanceof EffectForm) {
EffectForm effectForm = (EffectForm) form;
if (webAction == ACTION_CREATE) { // 新建
isOK = doCreate(effectForm, request, response);
} else if (webAction == ACTION_UPDATE) {
isOK = doUpdate(effectForm, request, response);
} else if (webAction == ACTION_UPLOAD) { // 上传
isOK = doUpload(effectForm, request, response);
if (isOK)
return mapping.findForward("create");
}
}
String strMsg = isOK ? "操作成功" : "操作失败";
request.setAttribute("message", strMsg);
return mapping.findForward(isOK ? "success" : "failure");
}
/**
*
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
private boolean doCreate(EffectForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
EffectItem item = new EffectItem();
item.setParentId(form.getParentId());
item.setName(form.getName());
item.setContent(form.getContent());
item.setActive(form.isActive());
item.setSearchable(false);
item.setSearchKey("UNDEFINED");
Date currentTime = new Date();
item.setLastUpdate(currentTime);
item.setDateCreated(currentTime);
try {
CatalogHelper ch = new CatalogHelper();
ch.createItem(item);
} catch (CatalogException e) {
AppLogger.error("EffectAction.doCreate() Exception: " + e.getMessage());
return false;
}
return true;
}
/**
*
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
private boolean doUpdate(EffectForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
CatalogHelper ch = new CatalogHelper();
Item baseItem = ch.getItem(form.getCatalogId());
if (baseItem != null && baseItem instanceof EffectItem) {
EffectItem item = (EffectItem) baseItem;
item.setName(form.getName());
item.setContent(form.getContent());
item.setActive(form.isActive());
Date currentTime = new Date();
item.setLastUpdate(currentTime);
try {
ch.updateItem(item);
} catch (CatalogException e) {
AppLogger.error("EffectAction.doUpdate() Exception: " + e.getMessage());
return false;
}
return true;
}
return false;
}
/**
*
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
private boolean doUpload(EffectForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("EffectAction.doUpload().form.getFileUpload()=" + form.getFileUpload());
System.out.println("EffectAction.doUpload().form.getParentId()=" + form.getParentId());
String uploadPath = uploadImageFile(form.getFileUpload());
System.out.println("EffectAction.doUpload().uploadPath=" + uploadPath);
if (uploadPath == null || uploadPath.length() < 1)
return false;
AppSettings config = AppSettings.getInstance();
uploadPath = config.getProperty(AppKeys.APP_ROOT)
+ config.getProperty(AppKeys.UPLOAD_ROOT) + "/images/icons"
+ uploadPath;
request.setAttribute("parentId", new Integer(form.getParentId()));
request.setAttribute("catalogId", new Integer(form.getCatalogId()));
request.setAttribute("uploadPath", uploadPath);
return true;
}
/**
*
* @param file
* @return
*/
private String uploadImageFile(FormFile file) {
if (file == null || file.getFileSize() < 1)
return null;
try {
InputStream stream = file.getInputStream();
// 获取文件扩展名并判断其有效性
String[] sp = file.getFileName().split("\\.");
String ext = null;
if (sp.length > 1) {
ext = sp[sp.length - 1].toLowerCase();
if (!ext.equals("jpg") && !ext.equals("gif")
&& !ext.equals("png"))
return null;
}
// 生成上传目录,如果不存在则创建
GregorianCalendar calendar = new GregorianCalendar();
String virtualPath = AppSettings.getInstance().getProperty(
AppKeys.UPLOAD_ROOT)
+ "/images/icons/" + calendar.get(Calendar.YEAR)
+ "-" + (calendar.get(Calendar.MONTH) + 1)
+ "-" + calendar.get(Calendar.DAY_OF_MONTH);
String physicalPath = this.getServlet().getServletContext().getRealPath(virtualPath);
File dir = new File(physicalPath);
if (!dir.exists())
dir.mkdir();
// 生成文件名
String fileName = "effect_" + calendar.get(Calendar.HOUR)
+ calendar.get(Calendar.MINUTE)
+ calendar.get(Calendar.SECOND)
+ calendar.get(Calendar.MILLISECOND) + "." + ext;
physicalPath = this.getServlet().getServletContext().getRealPath(
virtualPath + "/" + fileName);
OutputStream bos = new FileOutputStream(physicalPath);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
// 返回相对路径名
String filePath = "/" + calendar.get(Calendar.YEAR) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.DAY_OF_MONTH) + "/" + fileName;
AppLogger.info("EffectAction.uploadImageFile() The file has been written to \"" + filePath + "\"");
return filePath;
} catch (FileNotFoundException e) {
AppLogger.error("EffectAction.uploadImageFile() FileNotFoundException:" + e.getMessage());
return null;
} catch (IOException e) {
AppLogger.error("EffectAction.uploadImageFile() IOException:" + e.getMessage());
return null;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?