⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kjavastorageaction.java

📁 一个免费wap站
💻 JAVA
字号:
package com.eline.wap.resource.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.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.kjava.util.Packager;
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;
import com.eline.wap.resource.client.KJavaHelper;
import com.eline.wap.resource.exceptions.ResourceException;
import com.eline.wap.resource.model.KJavaStorage;

public class KJavaStorageAction extends Action {
	private static final int ACTION_CREATE	= 0;
	private static final int ACTION_UPDATE	= 1;
	private static final int ACTION_DELETE	= 2;

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("hello, PictureAction.execute()");

		boolean isOK = false;
		int webAction = StringUtils.getInt(request.getParameter("webAction"), -1);
		System.out.println("form=" + form);

		if (form instanceof KJavaStorageForm) {
			KJavaStorageForm actionForm = (KJavaStorageForm) form;
			System.out.println("STEP:1");
			
			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");
	}

	private boolean doCreate(KJavaStorageForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		KJavaStorage item = new KJavaStorage();
		
		item.setKjavaId(form.getParentId());
		item.setDeviceManufacturer(form.getDeviceManufacturer());
		item.setDeviceModels(form.getSelectedDeviceModels());
		item.setDescription(form.getDescription());

		// JAR上传
		String packageFilePath = uploadPackageFile(form.getFileUpload());
		String physicalPackagePath = this.getServlet().getServletContext().getRealPath(
				AppSettings.getInstance().getProperty(
						AppKeys.UPLOAD_ROOT)
						+ "/jars/" + packageFilePath);

		// 创建JAD文件
		Packager.generanteJADFile(physicalPackagePath);
		String jadFilePath = packageFilePath.replaceAll("jar", "jad");
		item.setJadFile(jadFilePath);
		item.setFileSize(form.getFileUpload().getFileSize());


		try {
			KJavaHelper helper = new KJavaHelper();
			helper.createKJavaStorage(item);
		} catch (ResourceException e) {
			AppLogger.error("KJavaStorageAction.doCreate() Exception: " + e.getMessage());
			return false;
		}
		return true;
	}
	
	private boolean doUpdate(KJavaStorageForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		KJavaHelper helper = new KJavaHelper();

		try {
			KJavaStorage item = helper.getKJavaStorage(form.getIndexId());
	
			item.setDeviceManufacturer(form.getDeviceManufacturer());
			item.setDeviceModels(form.getSelectedDeviceModels());
			item.setDescription(form.getDescription());
	
			// JAR上传
			String packageFilePath = uploadPackageFile(form.getFileUpload());
			if (packageFilePath != null) {
				String physicalPackagePath = this.getServlet().getServletContext().getRealPath(
						AppSettings.getInstance().getProperty(
								AppKeys.UPLOAD_ROOT)
								+ "/jars/" + packageFilePath);
	
				// 创建JAD文件
				String jadFilePath = Packager.generanteJADFile(physicalPackagePath);
				item.setJadFile(jadFilePath);
				item.setFileSize(form.getFileUpload().getFileSize());
			}
			helper.updateKJavaStorage(item);
		} catch (Exception e) {
			AppLogger.error("KJavaStorageAction.doUpdate() Exception: " + e.getMessage());
			return false;
		}
		return true;
	}

	private boolean doDelete(KJavaStorageForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// convert id(s) to be deleted from string to string array.
		String[] strIdList = form.getSelectedItems().split(";");
		KJavaHelper helper = new KJavaHelper();
		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 storageId = Integer.parseInt(strIdList[i]);
				helper.deleteKJavaStorage(storageId);
			} catch (NumberFormatException e) {
				AppLogger.error("NumberFormatException: KJavaStorageAction.doDelete() - parse id error, contiune.");
				continue;
			} catch (ResourceException e) {
				AppLogger.error("ResourceException: KJavaStorageAction.doDelete() - " + e.getMessage());
				returnValue = false;
			}
		}
		return returnValue;
	}

	private String uploadPackageFile(FormFile file) {
		if (file == null || file.getFileSize() < 1)
			return null;

		System.out.println("uploadPackageFile():STEP 1");
		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("jar"))
					return null;
			}
			// 生成上传目录,如果不存在则创建
			GregorianCalendar calendar = new GregorianCalendar();
			String virtualPath = AppSettings.getInstance().getProperty(
					AppKeys.UPLOAD_ROOT)
					+ "/jars/" + 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 = "game_" + 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("KJavaAction.uploadImageFile() The file has been written to \"" + filePath + "\"");
			
			return filePath;

		} catch (FileNotFoundException e) {
			AppLogger.error("KJavaAction.uploadImageFile() FileNotFoundException:" + e.getMessage());
			return null;
		} catch (IOException e) {
			AppLogger.error("KJavaAction.uploadImageFile() IOException:" + e.getMessage());
			return null;
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -