1 package org.ofbiz.content.layout; 2 3 import java.util.HashMap ; 4 import java.util.Locale ; 5 import java.util.Map ; 6 import javax.servlet.http.HttpServletRequest ; 7 8 import org.ofbiz.base.util.UtilHttp; 9 import org.ofbiz.base.util.UtilMisc; 10 import org.ofbiz.base.util.UtilProperties; 11 import org.ofbiz.entity.GenericDelegator; 12 import org.ofbiz.entity.util.ByteWrapper; 13 import org.ofbiz.service.ServiceUtil; 14 15 import org.apache.commons.fileupload.DiskFileUpload; 16 import org.apache.commons.fileupload.FileItem; 17 import org.apache.commons.fileupload.FileUploadException; 18 19 28 public class LayoutWorker { 29 30 public static final String module = LayoutWorker.class.getName(); 31 public static final String err_resource = "ContentErrorUiLabel"; 32 33 38 public static Map uploadImageAndParameters(HttpServletRequest request, String uploadField) { 39 40 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 42 Locale locale = UtilHttp.getLocale(request); 43 44 HashMap results = new HashMap (); 45 HashMap formInput = new HashMap (); 46 results.put("formInput", formInput); 47 DiskFileUpload fu = new DiskFileUpload(); 48 java.util.List lst = null; 49 try { 50 lst = fu.parseRequest(request); 51 } catch (FileUploadException e4) { 52 return ServiceUtil.returnError(e4.getMessage()); 53 } 54 55 if (lst.size() == 0) { 56 String errMsg = UtilProperties.getMessage(LayoutWorker.err_resource, "layoutEvents.no_files_uploaded", locale); 57 request.setAttribute("_ERROR_MESSAGE_", errMsg); 58 return ServiceUtil.returnError("No files uploaded."); 60 } 61 62 63 FileItem fi = null; 65 FileItem imageFi = null; 66 for (int i=0; i < lst.size(); i++) { 67 fi = (FileItem)lst.get(i); 68 String fn = fi.getName(); 69 String fieldName = fi.getFieldName(); 70 String fieldStr = fi.getString(); 71 if (fi.isFormField()) { 72 formInput.put(fieldName, fieldStr); 73 } 75 if (fieldName.equals(uploadField)) imageFi = fi; 76 } 77 78 if (imageFi == null ) { 79 Map messageMap = UtilMisc.toMap("imageFi", imageFi); 80 String errMsg = UtilProperties.getMessage(LayoutWorker.err_resource, "layoutEvents.image_null", messageMap, locale); 81 request.setAttribute("_ERROR_MESSAGE_", errMsg); 82 return null; 84 } 85 86 byte[] imageBytes = imageFi.get(); 87 ByteWrapper byteWrap = new ByteWrapper(imageBytes); 88 results.put("imageData", byteWrap); 89 results.put("imageFileName", imageFi.getName()); 90 91 return results; 93 94 } 95 96 97 public static ByteWrapper returnByteWrapper(Map map) { 98 99 ByteWrapper byteWrap = (ByteWrapper)map.get("imageData"); 100 return byteWrap; 101 } 102 103 } 104 | Popular Tags |