001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.action;
017:
018: import java.awt.image.BufferedImage;
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023:
024: import javax.imageio.ImageIO;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.commons.lang.StringUtils;
029: import org.apache.struts.action.ActionForm;
030: import org.apache.struts.action.ActionForward;
031: import org.apache.struts.action.ActionMapping;
032: import org.apache.struts.upload.FormFile;
033:
034: import dlog4j.formbean.UploadFlashForm;
035: import dlog4j.formbean.UploadImageForm;
036:
037: /**
038: * 日记附件上传
039: *
040: * @author Liudong
041: */
042: public class DlogUploadAction extends DlogActionBase {
043:
044: // --------------------------------------------------------- Instance Variables
045: public final static String UPLOADDIR_KEY = "uploadDir";
046: public final static String FILEPATH_KEY = "filePath";
047: public final static String IMAGESTRING_KEY = "imageString";
048: static String uploadDir = null;
049: static String dirName = null;
050:
051: // --------------------------------------------------------- Methods
052: /**
053: * 上传图片
054: * @param mapping
055: * @param form
056: * @param request
057: * @param response
058: * @return
059: * @throws Exception
060: */
061: public ActionForward uploadImage(ActionMapping mapping,
062: UploadImageForm form, HttpServletRequest request,
063: HttpServletResponse response) throws Exception {
064: FormFile picture = form.getUploadFile();
065: String fn = getUniqueFileName(getFileExtendName(picture
066: .getFileName()));
067: StringBuffer imgPath = new StringBuffer(uploadDir);
068: imgPath.append(File.separator);
069: imgPath.append(fn);
070: File imgFile = new File(imgPath.toString());
071: FileOutputStream fos = new FileOutputStream(imgFile);
072: try {
073: //写入到目录
074: fos.write(picture.getFileData());
075: } finally {
076: fos.close();
077: }
078:
079: //计算图像的大小
080: int width = 0, height = 0;
081: try {
082: BufferedImage bi = ImageIO.read(imgFile);
083: width = bi.getWidth();
084: height = bi.getHeight();
085: } catch (Exception e) {
086: getServlet().log("uploadImage process image failed.", e);
087: }
088: String img = dirName + '/' + fn;
089: request.setAttribute(FILEPATH_KEY, img);
090: StringBuffer imgstr = new StringBuffer(128);
091: imgstr.append("<img src='");
092: imgstr.append(img);
093: imgstr.append("'");
094: if (StringUtils.isNotEmpty(form.getTxtAlign())) {
095: imgstr.append(" align='");
096: imgstr.append(form.getTxtAlign());
097: imgstr.append("'");
098: }
099: 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
126: && (height > 450 || StringUtils.isNotEmpty(form
127: .getTxtHeight()))) {
128: imgstr.append(" height='");
129: int nHeight = getValue(450, form.getTxtHeight());
130: imgstr.append(nHeight);
131: imgstr.append("'");
132: if (nHeight == 450) {
133: int nWidth = (nHeight * width) / height;
134: imgstr.append(" width='");
135: imgstr.append(nWidth);
136: imgstr.append("'");
137: request.setAttribute("width", String.valueOf(nWidth));
138: request.setAttribute("height", String.valueOf(nHeight));
139: }
140: }
141: imgstr.append("/>");
142: request.setAttribute(IMAGESTRING_KEY, imgstr.toString());
143: return mapping.getInputForward();
144: }
145:
146: protected int getValue(int pvalue, String rvalue) {
147: int rv = Integer.MAX_VALUE;
148: try {
149: rv = Integer.parseInt(rvalue);
150: } catch (Exception e) {
151: }
152: return Math.min(pvalue, rv);
153: }
154:
155: /**
156: * 上传flash
157: * @param mapping
158: * @param form
159: * @param request
160: * @param response
161: * @return
162: * @throws Exception
163: */
164: public ActionForward uploadFlash(ActionMapping mapping,
165: UploadFlashForm form, HttpServletRequest request,
166: HttpServletResponse response) throws Exception {
167: FormFile picture = form.getUploadFile();
168: String fn = getUniqueFileName(getFileExtendName(picture
169: .getFileName()));
170: FileOutputStream fos = new FileOutputStream(uploadDir
171: + File.separator + fn);
172: try {
173: //写入到目录
174: fos.write(picture.getFileData());
175: String flash = dirName + '/' + fn;
176: request.setAttribute(FILEPATH_KEY, flash);
177: } finally {
178: fos.close();
179: }
180: return mapping.getInputForward();
181: }
182:
183: /**
184: * 附件上传
185: */
186: public ActionForward doDefault(ActionMapping mapping,
187: ActionForm form, HttpServletRequest request,
188: HttpServletResponse response) throws Exception {
189: if (uploadDir == null)
190: uploadDir = getUploadDir();
191: if (form instanceof UploadImageForm)
192: return uploadImage(mapping, (UploadImageForm) form,
193: request, response);
194: if (form instanceof UploadFlashForm)
195: return uploadFlash(mapping, (UploadFlashForm) form,
196: request, response);
197: return mapping.getInputForward();
198: }
199:
200: /**
201: * 获取上传文件保存的目录全路径
202: * @return
203: */
204: protected String getUploadDir() {
205: if (dirName == null) {
206: dirName = servlet.getInitParameter(UPLOADDIR_KEY);
207: if (dirName == null)
208: dirName = "uploads";
209: }
210: String webpath = servlet.getServletContext().getRealPath(
211: dirName);
212: if (webpath.endsWith(File.separator))
213: webpath += File.separator;
214: return webpath;
215: }
216:
217: /**
218: * 得到一个唯一的文件名
219: * @param extName
220: * @return
221: */
222: protected String getUniqueFileName(String extName) {
223: final SimpleDateFormat sdf = new SimpleDateFormat(
224: "yyyyMMddhhmmssSSSS.");
225: String fn = null;
226: do {
227: fn = sdf.format(new Date()) + extName;
228: if (new File(uploadDir + fn).exists())
229: continue;
230: break;
231: } while (true);
232: return fn;
233: }
234:
235: /**
236: * 获取文件的扩展名
237: * @param file
238: * @return
239: */
240: protected static String getFileExtendName(String file) {
241: int idx = file.lastIndexOf('.');
242: return (idx == -1 || idx == (file.length() - 1)) ? "" : file
243: .substring(idx + 1).toLowerCase();
244: }
245:
246: public static void main(String[] args) {
247: String str = null;
248: System.out.println(StringUtils.isEmpty(str));
249: }
250:
251: }
|