Java tutorial
package com.webbfontaine.valuewebb.action.tt; import com.webbfontaine.valuewebb.model.Props; import com.webbfontaine.valuewebb.model.TtDoc; import com.webbfontaine.valuewebb.model.TtGen; import com.webbfontaine.valuewebb.model.util.Utils; import com.webbfontaine.valuewebb.validation.ErrorHandling; import com.webbfontaine.valuewebb.validation.InfoHandling; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.web.RequestParameter; import org.jboss.seam.log.Log; import org.jboss.seam.log.Logging; import org.richfaces.event.UploadEvent; import org.richfaces.model.UploadItem; import javax.faces.application.FacesMessage; import java.util.List; import java.util.regex.Pattern; /** * Copyrights 2002-2012 Webb Fontaine * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. * Developer: nigiyan * Date: 17/01/2012 */ @Name("attDocFileHandler") public class AttDocFileHandler { private static final Pattern PATTERN = Pattern.compile("[\\W]|_"); @RequestParameter String itmNum; //need when attached document is requested to download private static final Log LOGGER = Logging.getLog(AttDocFileHandler.class); public void uploadAttachedFile(UploadEvent event) { UploadItem item = event.getUploadItem(); try { TtGenHome ttGenHome = TtGenHome.getComponentInstance(true); Props props = Utils.loadProps(); TtDoc ttDoc = ttGenHome.getInstance().getTtDocs().get(Integer.parseInt(itmNum)); if (props.getAttDocSize() != null && item.getFileSize() > props.getAttDocSize()) { ttDoc.setBytes(null); ttDoc.setContentType(null); ErrorHandling.addFacesMessage("ttDocs:" + itmNum + ":bytes", Utils.translate("Failed to upload file. File is too large. Maximum allowed size in bytes") + ": " + props.getAttDocSize(), FacesMessage.SEVERITY_ERROR); } else { String fileName = item.getFileName(); byte[] bytes = extractBytes(item); if (bytes == null || bytes.length == 0) { ttDoc.setContentType(null); // attached doc validator will check for this field in order to avoid loadinf bytes from DB for already saved attachments } else { ttDoc.setContentType( fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length()).toLowerCase()); ttDoc.setBytes(bytes); ErrorHandling.getInstance().removeErrorByFieldIdAndErrorMessage("ttDocs:" + itmNum + ":bytes", Utils.translate( "Failed to upload file. File is too large. Maximum allowed size in bytes") + ": " + props.getAttDocSize()); ErrorHandling.getInstance().removeErrorByFieldIdAndErrorMessage("ttDocs:" + itmNum + ":bytes", Utils.translate("Please attach document")); } } } catch (Exception e) { LOGGER.error("Failed to process uploaded file: {0}", e, item.getFile().getAbsolutePath()); } finally { removeFile(item); } } private static byte[] extractBytes(UploadItem item) throws Exception { return FileUtils.readFileToByteArray(item.getFile()); } private static void removeFile(UploadItem item) { try { FileUtils.forceDelete(item.getFile()); } catch (Exception e) { LOGGER.error("Failed to remove uploaded file {0}", e, item.getFile().getAbsolutePath()); } } public String downloadAttachedFile() { TtGen ttGen = TtGenHome.getComponentInstance(true).getInstance(); List<TtDoc> ttDocs = ttGen.getTtDocs(); int itmNumInt = Integer.parseInt(itmNum); if (itmNumInt < ttDocs.size()) { TtDoc ttDoc = ttDocs.get(itmNumInt); String filename = StringUtils.isEmpty(ttDoc.getName()) ? "" : StringUtils.substring(PATTERN.matcher(ttDoc.getName()).replaceAll("_"), 0, 60); Utils.sendBytes(ttDoc.getBytes(), filename + '.' + ttDoc.getContentType(), ttDoc.getContentType()); } else { InfoHandling.getInstance().setInfoList(Utils.translate("File does not exist")); } return ""; } }