01: package ru.emdev.EmForge.wiki.web.bean;
02:
03: import java.io.FileInputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06:
07: import javax.faces.component.UIParameter;
08:
09: import org.apache.commons.io.FilenameUtils;
10: import org.apache.commons.io.IOUtils;
11: import org.apache.commons.lang.StringUtils;
12: import org.apache.commons.logging.Log;
13: import org.apache.commons.logging.LogFactory;
14: import org.emforge.AttachmentService;
15: import org.richfaces.event.UploadEvent;
16: import org.richfaces.model.UploadItem;
17:
18: public class AttachmentUploadController {
19: protected final Log logger = LogFactory.getLog(getClass());
20:
21: private AttachmentService m_attachmentService;
22:
23: public void setAttachmentService(
24: AttachmentService i_attachmentService) {
25: m_attachmentService = i_attachmentService;
26: }
27:
28: public void upload(UploadEvent event) throws IOException {
29: try {
30: UploadItem item = event.getUploadItem();
31:
32: // get page name
33: String pageName = null;
34: for (Object uiObject : event.getComponent().getChildren()) {
35: if (uiObject instanceof UIParameter) {
36: final UIParameter param = (UIParameter) uiObject;
37: if (param.getName().equals("page")) {
38: pageName = param.getValue().toString();
39: break;
40: }
41: }
42: }
43:
44: String fileName = item.getFileName();
45: fileName = StringUtils.replaceChars(fileName, "#?\"'",
46: "____");
47:
48: // IE 6.0 give us full file name.
49: // We should get only name from it
50: //FileUtils fileUtils = new FileUtils();
51: fileName = FilenameUtils.getName(fileName);
52:
53: InputStream is = new FileInputStream(item.getFile());
54: byte[] content = IOUtils.toByteArray(is);
55: m_attachmentService.addAttachment(pageName, fileName,
56: content, null);
57: } catch (Exception pex) {
58: logger.error("Cannot upload file", pex);
59: }
60: }
61: }
|