catalogaction.java
来自「一个免费wap站」· Java 代码 · 共 320 行
JAVA
320 行
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.Item;
import com.eline.wap.catalog.model.ListItem;
import com.eline.wap.catalog.model.SingleItem;
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.SerializeData;
import com.eline.wap.common.util.StringUtils;
public class CatalogAction extends Action {
private static final int ACTION_CREATE = 0;
private static final int ACTION_UPDATE = 1;
private static final int ACTION_DELETE = 2;
/**
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("hello, CatalogAction.execute()");
boolean isOK = false;
int webAction = StringUtils.getInt(request.getParameter("webAction"), -1);
if (form instanceof CatalogForm) {
CatalogForm actionForm = (CatalogForm) form;
if (webAction == ACTION_CREATE) {
isOK = doCreate(actionForm, request, response);
} else if (webAction == ACTION_UPDATE) {
isOK = doUpdate(actionForm, request, response);
} else if (webAction == ACTION_DELETE) {
isOK = doDelete(actionForm, request, response);
}
}
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(CatalogForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Item baseItem = null;
try {
// 根据类型创建实例
if (form.getType() == Item.TYPE_LIST) { // 目录列表
ListItem item = new ListItem();
item.setParentId(form.getParentId());
item.setName(form.getName());
item.setSearchable(form.isSearchable());
item.setListAttribute(form.getListType());
item.setActive(form.isActive());
item.setDescription(form.getDescription());
// 设置时间
Date currentTime = new Date();
item.setDateCreated(currentTime);
item.setLastUpdate(currentTime);
// 设置搜索关键字
if (form.isSearchable())
item.setSearchKey(form.getName() + ";");
else
item.setSearchKey("UNDEFINED");
// 图片上传
String iconFilePath = uploadImageFile(form.getFileIcon(), "icon");
String bannerFilePath = uploadImageFile(form.getFileBanner(), "banner");
item.setIconFile(iconFilePath == null ? "" : iconFilePath);
item.setBannerFile(bannerFilePath == null ? "" : bannerFilePath);
baseItem = item;
} else {// if (form.getType() == Item.TYPE_SINGLE) // 目录项
SingleItem item = new SingleItem();
baseItem = item;
}
// 写入数据库
SerializeData data = baseItem.deserialize();
System.out.println("SerializeData.getKeys()=" + data.getKeys());
System.out.println("SerializeData.getValues()=" + data.getValues());
CatalogHelper ch = new CatalogHelper();
ch.createItem(baseItem);
} catch (Exception e) {
AppLogger.error("CatalogAction.doCreate() Exception: " + e.getMessage());
return false;
}
return true;
}
/**
*
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
private boolean doUpdate(CatalogForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Item baseItem = null;
try {
// 根据类型创建实例
if (form.getType() == Item.TYPE_LIST) { // 目录列表
CatalogHelper ch = new CatalogHelper();
baseItem = ch.getItem(form.getIndexId());
ListItem item = null;
if (baseItem instanceof ListItem) {
item = (ListItem) baseItem;
item.setIndexId(form.getIndexId());
// item.setParentId(form.getParentId());
item.setName(form.getName());
item.setSearchable(form.isSearchable());
// item.setListAttribute(form.getListType());
item.setActive(form.isActive());
item.setDescription(form.getDescription());
// 设置时间
Date currentTime = new Date();
item.setLastUpdate(currentTime);
// 设置搜索关键字
if (form.isSearchable())
item.setSearchKey(form.getName() + ";");
else
item.setSearchKey("UNDEFINED");
// 图片上传
String iconFilePath = uploadImageFile(form.getFileIcon(), "icon");
System.out.println("iconFilePath=" + iconFilePath);
String bannerFilePath = uploadImageFile(form.getFileBanner(), "banner");
System.out.println("bannerFilePath=" + bannerFilePath);
item.setIconFile(iconFilePath == null ? item.getIconFile() : iconFilePath);
item.setBannerFile(bannerFilePath == null ? item.getBannerFile() : bannerFilePath);
baseItem = item;
}
// 写入数据库
SerializeData data = baseItem.deserialize();
System.out.println("SerializeData.getKeys()=" + data.getKeys());
System.out.println("SerializeData.getValues()=" + data.getValues());
ch.updateItem(baseItem);
} else {// if (form.getType() == Item.TYPE_SINGLE) // 目录项
SingleItem item = new SingleItem();
baseItem = item;
}
} catch (Exception e) {
AppLogger.error("CatalogAction.doUpdate() Exception: " + e.getMessage());
return false;
}
return true;
}
/**
*
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
private boolean doDelete(CatalogForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// convert id(s) to be deleted from string to string array.
String[] strIdList = form.getDeletes().split(";");
CatalogHelper ch = new CatalogHelper();
boolean returnValue = true;
// parse id(s) and delete them.
for (int i = 0; i < strIdList.length; i ++) {
try {
if (strIdList[i] == null || strIdList[i].length() < 1)
continue;
int catalogId = Integer.parseInt(strIdList[i]);
ch.deleteItem(catalogId);
} catch (NumberFormatException e) {
AppLogger.error("NumberFormatException: CatalogAction.doDelete() - parse id error, contiune.");
continue;
} catch (CatalogException e) {
AppLogger.error("CatalogException: CatalogAction.doDelete() - " + e.getMessage());
returnValue = false;
}
}
return returnValue;
}
/**
* 上传图像文件
* @param file 需要上传的文件
* @param prefix 文件名前缀、用于区分icon/banner图像
* @return 在上传目录下的文件路径名
*/
private String uploadImageFile(FormFile file, String prefix) {
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") && !ext.equals("bmp"))
ext = null;
}
if (ext == null)
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 = prefix + "_" + 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("CatalogAction.uploadImageFile() The file has been written to \"" + filePath + "\"");
return filePath;
} catch (FileNotFoundException e) {
AppLogger.error("CatalogAction.uploadImageFile() FileNotFoundException:" + e.getMessage());
return null;
} catch (IOException e) {
AppLogger.error("CatalogAction.uploadImageFile() IOException:" + e.getMessage());
return null;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?