Java tutorial
/* * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved. * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Created on 2014-3-11 */ package mobile.service.core; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import mobile.service.result.ServiceVOResult; import mobile.util.MobileUtil; import mobile.vo.result.CommonVO; import org.apache.commons.lang.StringUtils; import play.Logger; import play.Logger.ALogger; import scalax.io.support.FileUtils; import com.fasterxml.jackson.databind.node.ObjectNode; import controllers.attachment.AttachUploadApp; import controllers.attachment.AttachmentApp; import controllers.base.ObjectNodeResult; /** * * * @ClassName: FileUploadService * @Description: ? * @date 2014-3-11 ?2:37:23 * @author ShenTeng * */ public class FileService { private static final ALogger LOGGER = Logger.of(FileService.class); /** * ? * * @param sourceFile ? * @param destFile */ public static void copyFile(String sourceFile, String destFile) { File dest = new File(destFile); if (!dest.exists()) {// ? dest.getParentFile().mkdirs(); } try (FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(destFile)) { FileUtils.copy(in, out); } catch (Exception e) { LOGGER.error("fail to copyFile. sourceFile = " + sourceFile + " ,destFile = " + destFile, e); } } /** * * * @param sourceFile * @param fileName ?? * @param attachType * @return CommonVO - relPathString urlStringattachIdattachId */ public static ServiceVOResult<CommonVO> uploadAttatch(File sourceFile, String fileName, AttatchType attachType) { String suffix = FileService.getFileNameSuffix(fileName); if (null == suffix) { return ServiceVOResult.error("100005", "???"); } if (!AttachUploadApp.checkFileTypes("." + suffix, attachType.getAttachType())) { return ServiceVOResult.error("108", "??"); } ObjectNodeResult nodeResult = new ObjectNodeResult(); AttachUploadApp.save(sourceFile, fileName, nodeResult, MobileUtil.getCurrentUser(), attachType.getAttachType()); if (!nodeResult.isSuccess()) { return ServiceVOResult.error("108", ""); } else { ObjectNode objectNode = nodeResult.getObjectNode(); ServiceVOResult<CommonVO> result = ServiceVOResult.success(); CommonVO vo = CommonVO.create(); vo.set("relPath", objectNode.path("path").asText()); vo.set("url", objectNode.path("pathsource").asText()); vo.set("attachId", objectNode.path("attachId").asLong()); result.setVo(vo); return result; } } public static enum AttatchType { SERVICE("service"), REQUIRE("require"); private String attachType; AttatchType(String attachType) { this.attachType = attachType; } public String getAttachType() { return attachType; } } /** * ????????null * * @param filename ??? * @return */ public static String getFileNameSuffix(String filename) { if (StringUtils.isBlank(filename)) { return null; } int indexOf = filename.indexOf('.'); return indexOf < 0 ? null : filename.substring(indexOf + 1).toLowerCase(); } /** * ??/?? */ public static String getAbsUploadPath() { return AttachmentApp.getUploadPath(); } /** * ?/?? */ public static String getRelUploadPath() { return "topx/uploadfile/"; } /** * ??/?? */ public static String getMobileAbsUploadPath() { return AttachmentApp.getUploadPath() + "mobile" + File.separator; } /** * ?/?? */ public static String getMobileRelUploadPath() { return "topx/uploadfile/mobile/"; } }