📄 fileuploadbean.java
字号:
/* Copyright 2006 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://developer.sun.com/berkeley_license.html$Id: FileUploadBean.java,v 1.51 2007/01/16 22:48:28 inder Exp $ */package com.sun.javaee.blueprints.petstore.controller;import java.io.File;import java.io.IOException;import java.util.logging.Level;import javax.faces.context.FacesContext;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.faces.context.ResponseWriter;import javax.faces.model.SelectItem;import org.apache.shale.remoting.faces.ResponseFactory;import com.sun.javaee.blueprints.petstore.util.PetstoreUtil;import com.sun.javaee.blueprints.petstore.util.PetstoreConstants;import com.sun.javaee.blueprints.petstore.util.ImageScaler;import com.sun.javaee.blueprints.components.ui.fileupload.FileUploadStatus;import com.sun.javaee.blueprints.components.ui.fileupload.FileUploadUtil;import com.sun.javaee.blueprints.petstore.proxy.GeoCoder;import com.sun.javaee.blueprints.petstore.proxy.GeoPoint;import com.sun.javaee.blueprints.petstore.model.Address;import com.sun.javaee.blueprints.petstore.model.CatalogFacade;import com.sun.javaee.blueprints.petstore.model.Category;import com.sun.javaee.blueprints.petstore.model.Product;import com.sun.javaee.blueprints.petstore.model.FileUploadResponse;import com.sun.javaee.blueprints.petstore.model.Item;import com.sun.javaee.blueprints.petstore.model.Tag;import com.sun.javaee.blueprints.petstore.model.SellerContactInfo;import java.math.BigDecimal;import java.math.MathContext;import java.util.ArrayList;import java.util.Hashtable;import java.util.List;import java.util.Map;import java.util.StringTokenizer;public class FileUploadBean { private boolean bDebug=false; private static final String comma=", "; private List<SelectItem> categories = null; private List<SelectItem> products = null; private CatalogFacade catalogFacade = null; /** * <p>Factory for response writers that we can use to construct the * outgoing response.</p> */ private static ResponseFactory factory = new ResponseFactory(); /** * session attribute to contain the fileupload status */ private static final String FILE_UL_RESPONSE = "fileuploadResponse"; /** Creates a new instance of FileUploadBean */ public FileUploadBean() { } public void setProducts(List<SelectItem> products) { this.products = products; } public List<SelectItem> getProducts() { if (catalogFacade == null) { Map<String,Object> contextMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap(); this.catalogFacade = (CatalogFacade)contextMap.get("CatalogFacade"); } //get the catalog facade if (products == null) { products = new ArrayList<SelectItem>(); for (Product pd : catalogFacade.getProducts()) { products.add(new SelectItem(pd.getProductID(), pd.getName())); } } return this.products; } public void setCategories(List<SelectItem> categories) { this.categories = categories; } public List<SelectItem> getCategories() { if (catalogFacade == null) { Map<String,Object> contextMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap(); this.catalogFacade = (CatalogFacade)contextMap.get("CatalogFacade"); } //get the catalog facade if (categories == null) { categories = new ArrayList<SelectItem>(); for (Category cat : catalogFacade.getCategories()) { categories.add(new SelectItem(cat.getCategoryID(), cat.getName())); } } return this.categories; } public void postProcessingMethod(FacesContext context, Hashtable htUpload, FileUploadStatus status) { if(bDebug) System.out.println("IN Custom Post Processing method"); try { // set custom return enabled so Phaselistener knows not to send default response status.enableCustomReturn(); HttpServletResponse response=(HttpServletResponse)context.getExternalContext().getResponse(); // get proxy host and port from servlet context String proxyHost=context.getExternalContext().getInitParameter("proxyHost"); String proxyPort=context.getExternalContext().getInitParameter("proxyPort"); // Acquire a response containing these results ResponseWriter writer = factory.getResponseWriter(context, "text/xml"); String itemId = null; String name = null; String thumbPath = null; String firstName = null; String prodId = null; HttpSession session = (HttpSession)context.getExternalContext().getSession(true); try { String fileNameKey = null; for (Object key1 : htUpload.keySet()) { String key = key1.toString(); if(key.startsWith("fileLocation_")) { fileNameKey = key; break; } } if(fileNameKey == null) { // error, you must have and upload file sendErrorResponse(response, writer, PetstoreUtil.getMessage("invalid_item_imageurl")); return; } String absoluteFileName=getStringValue(htUpload, fileNameKey); if(bDebug) System.out.println("Abs name: "+ absoluteFileName); String fileName = null; if(absoluteFileName.length() > 0) { int lastSeparator = absoluteFileName.lastIndexOf("/") + 1; if (lastSeparator != -1) { // set to proper location so image can be read fileName = "images/" + absoluteFileName.substring(lastSeparator, absoluteFileName.length()); } String spath = constructThumbnail(absoluteFileName); thumbPath = null; if (spath != null) { // recreate "images/FILENAME" int idx = spath.lastIndexOf(System.getProperty("file.separator")); thumbPath = "images/"+spath.substring(idx+1, spath.length()); } } else{ fileName = "images/dragon-iron-med.jpg"; thumbPath = "images/dragon-iron-thumb.jpg "; } String compName=getStringValue(htUpload, FileUploadUtil.COMPONENT_NAME); prodId=getStringValue(htUpload, compName+":product"); name=getStringValue(htUpload, compName+":name"); String desc=getStringValue(htUpload, compName+":description"); String price=getStringValue(htUpload, compName+":price"); // set to negative to trigger validation message for price if(price.length() == 0) price="-1"; String tags=getStringValue(htUpload, compName+":tags"); if(tags == null) tags=""; //Address StringBuffer addressx=new StringBuffer(); String street1=getStringValue(htUpload, compName+":street1"); String city=getStringValue(htUpload, compName+":cityField"); String state=getStringValue(htUpload, compName+":stateField"); String zip=getStringValue(htUpload, compName+":zipField"); // Contact info firstName = getStringValue(htUpload, compName+":firstName"); String lastName = getStringValue(htUpload, compName+":lastName"); String email = getStringValue(htUpload, compName+":email"); if(street1 != null && street1.length() > 0) { addressx.append(street1); } if(city != null && city.length() > 0) { addressx.append(comma); addressx.append(city); } if(state != null && state.length() > 0) { addressx.append(comma); addressx.append(state); } if(zip != null && zip.length() > 0) { addressx.append(comma); addressx.append(zip); } // get latitude & longitude GeoCoder geoCoder=new GeoCoder(); if(proxyHost != null && proxyPort != null) { // set proxy host and port if it exists // NOTE: This may require write permissions for java.util.PropertyPermission to be granted PetstoreUtil.getLogger().log(Level.INFO, "Setting proxy to " + proxyHost + ":" + proxyPort + ". Make sure server.policy is updated to allow setting System Properties"); geoCoder.setProxyHost(proxyHost); try { geoCoder.setProxyPort(Integer.parseInt(proxyPort)); } catch (NumberFormatException ee) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -