1 16 package dlog4j.action; 17 18 import java.awt.image.BufferedImage ; 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 24 import javax.imageio.ImageIO ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 29 import org.apache.commons.lang.StringUtils; 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.action.ActionForward; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.upload.FormFile; 34 35 import dlog4j.formbean.UploadFlashForm; 36 import dlog4j.formbean.UploadImageForm; 37 38 43 public class DlogUploadAction extends DlogActionBase { 44 45 public final static String UPLOADDIR_KEY = "uploadDir"; 47 public final static String FILEPATH_KEY = "filePath"; 48 public final static String IMAGESTRING_KEY = "imageString"; 49 static String uploadDir = null; 50 static String dirName = null; 51 52 62 public ActionForward uploadImage(ActionMapping mapping, UploadImageForm form, 63 HttpServletRequest request, HttpServletResponse response) 64 throws Exception { 65 FormFile picture = form.getUploadFile(); 66 String fn = getUniqueFileName(getFileExtendName(picture.getFileName())); 67 StringBuffer imgPath = new StringBuffer (uploadDir); 68 imgPath.append(File.separator); 69 imgPath.append(fn); 70 File imgFile = new File (imgPath.toString()); 71 FileOutputStream fos = new FileOutputStream (imgFile); 72 try { 73 fos.write(picture.getFileData()); 75 } finally { 76 fos.close(); 77 } 78 79 int width = 0, height=0; 81 try{ 82 BufferedImage bi = ImageIO.read(imgFile); 83 width = bi.getWidth(); 84 height = bi.getHeight(); 85 }catch(Exception e){ 86 getServlet().log("uploadImage process image failed.",e); 87 } 88 String img = dirName+'/'+fn; 89 request.setAttribute(FILEPATH_KEY,img); 90 StringBuffer imgstr = new StringBuffer (128); 91 imgstr.append("<img SRC='"); 92 imgstr.append(img); 93 imgstr.append("'"); 94 if(StringUtils.isNotEmpty(form.getTxtAlign())) { 95 imgstr.append(" align='"); 96 imgstr.append(form.getTxtAlign()); 97 imgstr.append("'"); 98 } 99 if(StringUtils.isNotEmpty(form.getTxtBorder())) { 100 imgstr.append(" border='"); 101 imgstr.append(form.getTxtBorder()); 102 imgstr.append("'"); 103 } 104 if(StringUtils.isNotEmpty(form.getDisplayText())) { 105 imgstr.append(" alt='"); 106 imgstr.append(form.getDisplayText()); 107 imgstr.append("'"); 108 } 109 boolean sizeok = false; 110 if(width > 600 || StringUtils.isNotEmpty(form.getTxtWidth())) { 111 imgstr.append(" width='"); 112 int nWidth = getValue(600,form.getTxtWidth()); 113 imgstr.append(nWidth); 114 imgstr.append("'"); 115 if(nWidth==600){ 116 int nHeight = (nWidth * height)/ width; 117 imgstr.append(" height='"); 118 imgstr.append(nHeight); 119 imgstr.append("'"); 120 sizeok = true; 121 request.setAttribute("width",String.valueOf(nWidth)); 122 request.setAttribute("height",String.valueOf(nHeight)); 123 } 124 } 125 if(!sizeok && (height > 450 || StringUtils.isNotEmpty(form.getTxtHeight()))) { 126 imgstr.append(" height='"); 127 int nHeight = getValue(450,form.getTxtHeight()); 128 imgstr.append(nHeight); 129 imgstr.append("'"); 130 if(nHeight==450){ 131 int nWidth = (nHeight * width) / height; 132 imgstr.append(" width='"); 133 imgstr.append(nWidth); 134 imgstr.append("'"); 135 request.setAttribute("width",String.valueOf(nWidth)); 136 request.setAttribute("height",String.valueOf(nHeight)); 137 } 138 } 139 imgstr.append("/>"); 140 request.setAttribute(IMAGESTRING_KEY,imgstr.toString()); 141 return mapping.getInputForward(); 142 } 143 144 protected int getValue(int pvalue, String rvalue){ 145 int rv = Integer.MAX_VALUE; 146 try{ 147 rv = Integer.parseInt(rvalue); 148 }catch(Exception e){} 149 return Math.min(pvalue, rv); 150 } 151 160 public ActionForward uploadFlash(ActionMapping mapping, UploadFlashForm form, 161 HttpServletRequest request, HttpServletResponse response) 162 throws Exception { 163 FormFile picture = form.getUploadFile(); 164 String fn = getUniqueFileName(getFileExtendName(picture.getFileName())); 165 FileOutputStream fos = new FileOutputStream (uploadDir + File.separator + fn); 166 try { 167 fos.write(picture.getFileData()); 169 String flash = dirName+'/'+fn; 170 request.setAttribute(FILEPATH_KEY,flash); 171 } finally { 172 fos.close(); 173 } 174 return mapping.getInputForward(); 175 } 176 179 public ActionForward doDefault(ActionMapping mapping, ActionForm form, 180 HttpServletRequest request, HttpServletResponse response) 181 throws Exception { 182 if (uploadDir == null) 183 uploadDir = getUploadDir(); 184 if(form instanceof UploadImageForm) 185 return uploadImage(mapping,(UploadImageForm)form,request,response); 186 if(form instanceof UploadFlashForm) 187 return uploadFlash(mapping,(UploadFlashForm)form,request,response); 188 return mapping.getInputForward(); 189 } 190 194 protected String getUploadDir() { 195 if(dirName==null) { 196 dirName = servlet.getInitParameter(UPLOADDIR_KEY); 197 if (dirName == null) 198 dirName = "uploads"; 199 } 200 String webpath = servlet.getServletContext().getRealPath(dirName); 201 if (webpath.endsWith(File.separator)) 202 webpath += File.separator; 203 return webpath; 204 } 205 210 protected String getUniqueFileName(String extName) { 211 final SimpleDateFormat sdf = 212 new SimpleDateFormat ("yyyyMMddhhmmssSSSS."); 213 String fn = null; 214 do { 215 fn = sdf.format(new Date ()) + extName; 216 if (new File (uploadDir + fn).exists()) 217 continue; 218 break; 219 } while (true); 220 return fn; 221 } 222 227 protected static String getFileExtendName(String file) { 228 int idx = file.lastIndexOf('.'); 229 return (idx == -1 || idx == (file.length() - 1)) 230 ? "" 231 : file.substring(idx + 1).toLowerCase(); 232 } 233 234 public static void main(String [] args){ 235 String str = null; 236 System.out.println(StringUtils.isEmpty(str)); 237 } 238 239 } 240 | Popular Tags |