📄 fileattachmentmodule.java
字号:
/* CRMS, customer relationship management system Copyright (C) 2003 Service To Youth Council This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For further information contact the SYC ICT department on GPL@syc.net.au 98 Kermode Street North Adelaide South Australia SA 5006 +61 (0)8 8367 0755 *//* * FileAttachmentModule.java * * Created on 5 May 2003, 17:18 */package crms.module;import org.apache.commons.fileupload.*;import crms.util.*;import java.util.*;import crms.vo.FileAttachment;import java.io.*;import java.net.*;import crms.vo.*;import org.apache.log4j.Logger;import crms.dao.*;/** * * @author dmurphy */public class FileAttachmentModule { static Logger logger = Logger.getLogger(FileAttachmentModule.class); String repositoryPath = null; public static String PREFIX = "attachment."; public static String COMMAND_GET_ATTACHMENTS = "attachment.view"; public static String COMMAND_DELETE_ATTACHMENT = "attachment.delete"; public static String PARAM_DESCRIPTION = "description"; public static String PARAM_ATTACHED_BY = "attached-by"; public static String PARAM_TYPE = "type"; public static String PARAM_REF_ID="ref-id"; public static String PARAM_ATTACHMENT ="attachment"; private FileAttachmentDAO attachDAO = DAOFactory.getInstance().getFileAttachmentDAO(); /** Creates a new instance of FileAttachmentModule */ public FileAttachmentModule(String repository) { this.repositoryPath = repository; } public FileAttachmentModule() { } public void attachFile(List uploadItems) { try { FileAttachment attach = new FileAttachment(); Iterator it = uploadItems.iterator(); int newID = 1; while (it.hasNext()) { FileItem item = (FileItem) it.next(); if (item.isFormField()) { String fieldName = item.getFieldName(); String value = item.getString(); String contentType = item.getContentType(); if (fieldName.equals(PARAM_ATTACHED_BY)) { attach.setAttachedBy(value); } else if (fieldName.equals(PARAM_DESCRIPTION)) { attach.setDescription(value); } else if (fieldName.equals(PARAM_REF_ID)) { attach.setReferenceID(value); } else if (fieldName.equals(PARAM_TYPE)) { attach.setAttachmentType((EntityType)EntityType.decode(value, EntityType.class)); } } else { // The form fields will most likely carry // authentication information (from squirrelmail) attach.setLocation(item.getFieldName()); FileAttachment complete = createAttachmentRecord(attach); File newFile = new File(repositoryPath, attach.getAttachmentType().getCode() + "-" + complete.getAttachmentID()); logger.debug("Attempting to write file to " + newFile.getAbsolutePath()); item.write(newFile.getAbsolutePath()); logger.debug("File written to " + newFile.getAbsolutePath()); } } } catch (Exception ex) { logger.error("Error processing file upload: ", ex); } } public synchronized FileAttachment createAttachmentRecord(FileAttachment attach) { return attachDAO.insertFileAttachment(attach); } public ServerResponse processCommand(ServerCommand command) throws Exception { ServerResponse sr = new ServerResponse(); String user = command.getUser(); if (command.getKey().toLowerCase().equals(COMMAND_GET_ATTACHMENTS)) { String typeStr = (String) command.getParameterValue(PARAM_TYPE); EntityType type = (EntityType) AbstractCode.decode(typeStr, EntityType.class); String refID = (String) command.getParameterValue(PARAM_REF_ID); ArrayList attachments = (ArrayList) attachDAO.getFileAttachments(type, refID); sr.addPart("attachments", attachments); return sr; } else if (command.getKey().toLowerCase().equals(COMMAND_DELETE_ATTACHMENT)) { FileAttachment attach = (FileAttachment) command.getParameterValue(PARAM_ATTACHMENT); logger.debug(repositoryPath); File delFile = new File(repositoryPath, attach.getAttachmentType().getCode() + "-" + attach.getAttachmentID()); logger.debug("DELETING ATTACHMENT: " + attach.getAttachmentID() + " file: " + delFile.getAbsolutePath()); // only delete the DB entry if the file was successfully deleted if (delFile.delete()) { attachDAO.deleteFileAttachment(attach); } else { sr.addPart("fail", "fail"); } } return sr; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -