Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java程序分享区 » 工具  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
话题被移动
该话题已被移动 - tzutolin , 2005-12-13 14:12
如果您尚不清楚该话题被移动的原因,请参考论坛规则以及本版公告或者联系本版版主。
作者 Re:color4j [Re:文夕]
ditty

负资产小资

CJSDN高级会员


发贴: 1038
于 2004-04-22 20:20 user profilesend a private message to usersend email to dittyreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不错不错~~~

001 package com.jivesoftware.forum.action;
002 
003 import com.jivesoftware.base.UnauthorizedException;
004 import com.jivesoftware.base.JiveGlobals;
005 import com.jivesoftware.forum.*;
006 import com.jivesoftware.util.ByteFormat;
007 import com.jivesoftware.util.MultipartParamUtils;
008 import com.jivesoftware.webwork.action.CommandDriven;
009 
010 import java.io.IOException;
011 import java.io.InputStream;
012 import java.util.*;
013 import javax.servlet.http.HttpSession;
014 
015 public class AttachAction extends PostAction
016         implements CommandDriven {
017 
018     public AttachAction() {
019         maxRequestSize = 0;
020         attachID = -1L;
021     }
022 
023     public String getDoAttachFiles() {
024         return doAttachFiles;
025     }
026 
027     public void setDoAttachFiles(String doAttachFiles) {
028         if (doAttachFiles != null && !"".equals(doAttachFiles.trim()))
029             this.doAttachFiles = "true";
030     }
031 
032     public void addErrorMessage(String message) {
033         super.addErrorMessage(message);
034     }
035 
036     public int[] getDeleteAttachIDs() {
037         return deleteAttachIDs;
038     }
039 
040     public void setDeleteAttachIDs(int deleteAttachIDs[]) {
041         this.deleteAttachIDs = deleteAttachIDs;
042     }
043 
044     public long getAttachID() {
045         return attachID;
046     }
047 
048     public void setAttachID(long attachID) {
049         this.attachID = attachID;
050     }
051 
052     public String getEncSubject() {
053         return encSubject;
054     }
055 
056     public void setEncSubject(String encSubject) {
057         if (encSubject != null && !"".equals(encSubject))
058             this.encSubject = encSubject;
059     }
060 
061     public String getEncBody() {
062         return encBody;
063     }
064 
065     public void setEncBody(String encBody) {
066         if (encBody != null && !"".equals(encBody))
067             this.encBody = encBody;
068     }
069 
070     public AttachmentManager getAttachmentManager() {
071         return getForumFactory().getAttachmentManager();
072     }
073 
074     public String getName() {
075         return (String) getServletRequest().getSession().getAttribute("jive.post.name." + getSessionSuffix());
076     }
077 
078     public String getEmail() {
079         return (String) getServletRequest().getSession().getAttribute("jive.post.email." + getSessionSuffix());
080     }
081 
082     public String getSubject() {
083         return (String) getServletRequest().getSession().getAttribute("jive.post.subject." + getSessionSuffix());
084     }
085 
086     public String getBody() {
087         return (String) getServletRequest().getSession().getAttribute("jive.post.body." + getSessionSuffix());
088     }
089 
090     public boolean isReply() {
091         Boolean replyVal = (Boolean) getServletRequest().getSession().getAttribute("jive.post.reply." + getSessionSuffix());
092         return replyVal != null && replyVal.booleanValue();
093     }
094 
095     public String getDoCancel() {
096         String doCancel = params.getParameter("doCancel");
097         if (doCancel != null && !"".equals(doCancel.trim()))
098             return "true";
099         else
100             return null;
101     }
102 
103 /*    public boolean isEdit()
104     {
105         return false;
106     }
107 */
108 
109     protected void doValidation() {
110         AttachmentManager manager = getAttachmentManager();
111         int requestSize = getServletRequest().getContentLength();
112         if (requestSize > maxRequestSize) {
113             ByteFormat byteFormat = new ByteFormat();
114             List args = new ArrayList();
115             args.add(byteFormat.format(new Long(manager.getMaxAttachmentSize() * 1024)));
116             addErrorMessage(getText("attach.error_generic", args));
117             return;
118         }
119         boolean hasValid = false;
120         int i = 0;
121         do {
122             if (i >= manager.getMaxAttachmentsPerMessage())
123                 break;
124             String contentType = params.getParameterContentType("attachFile" + (i + 1));
125             int attachSize = params.getParameterSize("attachFile" + (i + 1));
126             if (manager.isValidType(contentType)) {
127                 hasValid = true;
128                 break;
129             }
130             if (attachSize < manager.getMaxAttachmentSize() * 1024) {
131                 hasValid = true;
132                 break;
133             }
134             i++;
135         } while (true);
136         if (!hasValid)
137             addErrorMessage(getText("attach.error_content_type"));
138     }
139 
140     public String doDefault() {
141         try {
142             if (!loadJiveObjects()) {
143                 return ERROR;
144             }
145         } catch (UnauthorizedException e) {
146             setLoginAttributes();
147             addErrorMessage(getText("attach.error_unauth"));
148             return LOGIN;
149         }
150         if (!getCanAttach(getForum())) {
151             setLoginAttributes();
152             addErrorMessage(getText("attach.error_unauth"));
153             return LOGIN;
154         }
155         return INPUT;
156     }
157 
158     public String doDelete() {
159         try {
160             if (!loadJiveObjects()) {
161                 return ERROR;
162             }
163         } catch (UnauthorizedException e) {
164             setLoginAttributes();
165             addErrorMessage(getText("attach.error_unauth"));
166             return LOGIN;
167         }
168 
169         if (this.attachID == -1L) {
170             addErrorMessage(getText("attach.error_unable_delete"));
171             return "error";
172         }
173         ForumMessage msg = getMessage();
174         Attachment attachment = null;
175         Iterator attachments = msg.getAttachments();
176         long attachID;
177 
178         do {
179             if (!attachments.hasNext())
180                 break;
181             attachment = (Attachment) attachments.next();
182             attachID = attachment.getID();
183         } while (this.attachID != attachID);
184         try {
185             msg.deleteAttachment(attachment);
186         } catch (AttachmentException ae) {
187             addErrorMessage(ae.getMessage());
188             return ERROR;
189         } catch (UnauthorizedException ue) {
190             addErrorMessage(ue.getMessage());
191             return ERROR;
192         }
193         getServletRequest().setAttribute("partialURL", getPartialURL());
194         return "success-delete";
195     }
196 
197     public String doExecute() {
198         try {
199             if (loadJiveObjects() == false) {
200                 return "error";
201             }
202         } catch (UnauthorizedException e) {
203             setLoginAttributes();
204             addErrorMessage(getText("attach.error_unauth"));
205             return "login";
206         }
207         if ("true".equals(getDoCancel())) {
208             request.setAttribute("tid", getTid());
209             if (isReply()) {
210                 return "cancel-reply";
211             }
212             return "cancel-topic";
213         }
214         if (getCanAttach(getForum()) == false) {
215             setLoginAttributes();
216             addErrorMessage(getText("attach.error_unauth"));
217             return "login";
218         }
219         if (isNothingSelected()) {
220             addErrorMessage(getText("attach.error_at_least_one"));
221             request.setAttribute("tid", getTid());
222             return "input";
223         }
224         if (!isEdit()) {
225             /*try{
226             addAttachments();
227             }catch(UnauthorizedException ue){
228                 ue.printStackTrace();
229                 addErrorMessage(getText("attach.error_unauth"));
230                 return "error";
231             }*/
232             addAttachments();
233             if (getHasErrors()) {
234                 return ERROR;
235             }
236             request.setAttribute("tid", getTid());
237             if (isReply())
238                 return "success-reply";
239             request.setAttribute("tid", getTid());
240             if (isReply())
241                 return "success-reply";
242             else
243                 return "success-topic";
244 
245         } else {
246             try {
247                 addAttachments(getMessage());
248                 if (getHasErrors())
249                     return "error";
250                 else
251                     return "success-edit";
252             } catch (UnauthorizedException ue) {
253                 ue.printStackTrace();
254                 addErrorMessage(getText("attach.error_unauth"));
255                 return ERROR;
256             }
257         }
258     }
259 
260     protected int getExtraParamCount() {
261         return 0;
262     }
263 
264     protected boolean loadJiveObjects()
265             throws UnauthorizedException {
266         boolean success = true;
267         AttachmentManager manager = getAttachmentManager();
268         maxRequestSize = (manager.getMaxAttachmentsPerMessage() * manager.getMaxAttachmentSize() + manager.getMaxAttachmentsPerMessage() * 16 + getExtraParamCount() * 16) * 1024;
269         try {
270             params = new MultipartParamUtils(getServletRequest(), maxRequestSize);
271         } catch (IOException ioe) {
272             addErrorMessage(ioe.getMessage());
273             success = false;
274         }
275         if (success)
276             return super.loadJiveObjects();
277         else
278             return success;
279     }
280 
281     protected void addAttachments() {
282         AttachmentManager manager;
283         HttpSession session;
284         String key;
285         List attachIDs;
286         manager = getAttachmentManager();
287         session = request.getSession();
288         String suffix = getSessionSuffix();
289         key = "jive.post.tempAttachIDs" + suffix;
290         attachIDs = (List) session.getAttribute(key);
291         if (attachIDs == null) attachIDs = new ArrayList(5);
292         for (int i = 0; i < manager.getMaxAttachmentsPerMessage(); i++) {
293 
294             String attachmentName;
295             String contentType;
296             InputStream in;
297             attachmentName = params.getParameter("attachFile" + (i + 1));
298             contentType = params.getParameterContentType("attachFile" + (i + 1));
299             in = null;
300             in = params.getParameterData("attachFile" + (i + 1));
301             try {
302                 try {
303                     if (attachmentName != null && contentType != null && in != null) {
304                         attachmentName = getCanonicalName(attachmentName);
305                         Attachment attach = manager.createTempAttachment(attachmentName, contentType, in);
306                         attachIDs.add(new Long(attach.getID()));
307                     }
308                     in.close();
309                 } catch (AttachmentException ae) {
310                     if (ae.getErrorType() == 0)
311                         addError("attachFile" + (i + 1), getText("attach.error_too_large_field"));
312                     else if (ae.getErrorType() == 2)
313                         addError("attachFile" + (i + 1), getText("attach.error_content_type"));
314                     else if (ae.getErrorType() == 1)
315                         addError("attachFile" + (i + 1), getText("attach.error_too_many"));
316                     else
317                         addError("attachFile" + (i + 1), getText("attach.error_no_read_perm"));
318                 }
319             } catch (Exception e) {
320                 e.printStackTrace();
321             }
322             if (attachIDs.size() > 0) session.setAttribute(key, attachIDs);
323         }
324 
325     }
326 
327     protected void addAttachments(ForumMessage forumMessage) throws UnauthorizedException {
328         AttachmentManager manager = getAttachmentManager();
329         for (int i = 0; i < manager.getMaxAttachmentsPerMessage(); i++) {
330             String attachmentName = params.getParameter("attachFile" + (i + 1));
331             String contentType = params.getParameterContentType("attachFile" + (i + 1));
332             InputStream in = null;
333             try {
334                 // Try adding the attachment to the message
335                 in = params.getParameterData("attachFile" + (i + 1));
336                 if (attachmentName != null && contentType != null && in != null) {
337                     // Adjust the name (we only want the canonical name of the file)
338                     attachmentName = getCanonicalName(attachmentName);
339                     forumMessage.createAttachment(attachmentName, contentType, in);
340                 }
341             }
342                     /*catch(UnauthorizedException ue)*/ catch (AttachmentException ae) {
343                 if (ae.getErrorType() == AttachmentException.TOO_LARGE) {
344                     addError("attachFile" + (i + 1), getText("attach.error_too_large_field"));
345                 } else if (ae.getErrorType() == AttachmentException.BAD_CONTENT_TYPE) {
346                     addError("attachFile" + (i + 1), getText("attach.error_content_type"));
347                 } else if (ae.getErrorType() == AttachmentException.TOO_MANY_ATTACHMENTS) {
348                     addError("attachFile" + (i + 1), getText("attach.error_too_many"));
349                 } else {
350                     addError("attachFile" + (i + 1), getText("attach.error_no_read_perm"));
351                 }
352             } finally {
353                 try {
354                     in.close();
355                 } catch (Exception e) {
356                 }
357             }
358         }
359     }
360 
361     private boolean isNothingSelected() {
362         boolean nothing = true;
363         AttachmentManager manager = getAttachmentManager();
364         for (int i = 0; i < manager.getMaxAttachmentsPerMessage(); i++) {
365             String attachmentName = params.getParameter("attachFile" + (i + 1));
366             if (attachmentName != null)
367                 nothing = false;
368         }
369 
370         return nothing;
371     }
372 
373 /*    private String getCanonicalName(String filename) {
374         int forwardSlash = filename.lastIndexOf("/");
375         int backwardSlash = filename.lastIndexOf("\\");
376         if (forwardSlash != -1 && forwardSlash > backwardSlash)
377             filename = filename.substring(forwardSlash + 1, filename.length());
378         else if (backwardSlash != -1 && backwardSlash >= forwardSlash)
379             filename = filename.substring(backwardSlash + 1, filename.length());
380         return filename;
381     }
382 */
383     private String getCanonicalName(String filename)
384     {
385         int forwardSlash = filename.lastIndexOf("/");
386         int backwardSlash = filename.lastIndexOf("\\");
387         if(forwardSlash != -1 && forwardSlash > backwardSlash)
388             filename = filename.substring(forwardSlash + 1, filename.length());
389         else
390         if(backwardSlash != -1 && backwardSlash >= forwardSlash)
391             filename = filename.substring(backwardSlash + 1, filename.length());
392         if(JiveGlobals.getCharacterEncoding().equalsIgnoreCase("UTF-8") && JiveGlobals.getLocale().getCountry().equalsIgnoreCase("CN"))
393             try
394             {
395                 filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8");
396             }
397             catch(Exception e) { }
398         return filename;
399     }
400 
401     public static String SUCCESS_TOPIC = "success-topic";
402     public static String SUCCESS_MESSAGE = "success-message";
403     protected MultipartParamUtils params;
404     private int maxRequestSize;
405     private int deleteAttachIDs[];
406     private long attachID;
407     private String doAttachFiles;
408     private String encSubject;
409     private String encBody;
410 }



内忧外患的时代,洗心革面,阿咪豆腐~

话题树型展开
人气 标题 作者 字数 发贴时间
15430 [精华] color4j Jove 4926 2004-03-17 14:03
13166 Re:color4j Jove 0 2004-03-17 14:04
12629 Re:color4j chenyajun5 2218 2005-01-26 10:59
12578 Re:color4j chenyajun5 14 2005-01-26 11:00
13231 Re:color4j alexyu2000 24 2005-01-26 11:14
13101 Re:color4j dapan 52 2004-03-17 14:59
13175 Re:color4j Jove 102 2004-03-17 15:08
12994 Re:color4j rainman 23 2004-03-17 15:14
13171 Re:color4j Jove 214 2004-03-17 15:19
12938 Re:color4j rainman 18 2004-03-17 15:46
13288 Re:color4j freeman_z 34868 2004-03-25 04:30
13058 Re:color4j nothing 5 2004-03-25 05:31
13060 Re:color4j Starcraft 13 2004-04-11 12:13
12944 Re:color4j alexyu2000 179 2004-04-13 09:06
12874 Re:color4j 文夕 572 2004-04-22 19:58
13006 Re:color4j ditty 61863 2004-04-22 20:20
12715 Re:color4j fnhufoz 2 2004-05-22 00:05
12637 Re:color4j littledeer1974 60 2005-01-06 12:39

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923