Java tutorial
/* * Copyright (C) 2014-2016 Kagucho <kagucho.net@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package tsuboneSystem.original.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.apache.struts.upload.FormFile; import org.seasar.framework.container.SingletonS2Container; import org.seasar.struts.util.ResponseUtil; import org.seasar.struts.util.UploadUtil; import tsuboneSystem.code.ActorKindCode; import tsuboneSystem.code.FilePathCode; import tsuboneSystem.code.ImageFilePurposeCode; import tsuboneSystem.entity.TAdmin; import tsuboneSystem.entity.TImageUpload; import tsuboneSystem.entity.TLeaders; import tsuboneSystem.entity.TMember; import tsuboneSystem.entity.TSubmit; import tsuboneSystem.form.SubmitForm; import tsuboneSystem.service.TAdminService; import tsuboneSystem.service.TImageUploadService; import tsuboneSystem.service.TLeadersService; import tsuboneSystem.service.TSubmitService; /** * *?????Util * * */ public class TsuboneSystemUtil { /** * ???? * * @param TMember * @return ActorKindCode * @author Hiroaki * * */ public static String actorKind(TMember member) { String actorKind = null; //?????????????????? List<TLeaders> leaderList = new ArrayList<TLeaders>(); TLeadersService tLeadersService = SingletonS2Container.getComponent(TLeadersService.class); leaderList = tLeadersService.findByMemberIdOrderKindList(member.id); //?????????????????? TAdminService tAdminService = SingletonS2Container.getComponent(TAdminService.class); TAdmin tAdmin = tAdminService.findByMemberId(member.id); //????admin if (tAdmin != null) { actorKind = ActorKindCode.ADMIN.getCode(); return actorKind; } //???leaders if (leaderList.size() > 0) { actorKind = ActorKindCode.LEADERS.getCode(); return actorKind; } else { actorKind = ActorKindCode.MEMBER.getCode(); return actorKind; } } /** * Upload????? * * @param FormFile formFile, String kind * @return ????????false * @author Hiroaki * * */ public static boolean isFileKindCheck(FormFile formFile, String[] kinds) { // ???Check if (formFile.getFileSize() > 0) { String fileName = new String(formFile.getFileName()); int point = fileName.lastIndexOf("."); String fileContentType = new String(fileName.substring(point + 1)); fileContentType = fileName.substring(point + 1); if (fileContentType != null) { //???????? if (!Arrays.asList(kinds).contains(fileContentType)) { return true; } else { return false; } } else { //?????????? return true; } } else { //?Upload???????? return true; } } /** * Upload???? * * @param FormFile formFile,int size * @return ??(byte)?????true * @author Hiroaki * * */ public static boolean isFileSizeCheck(FormFile formFile, int size) { if (formFile.getFileSize() > size) { return true; } return false; } /** * String(yyyy/mm/dd)Date??? * * @param String(yyyy/mm/dd) * @return Date * @author Hiroaki * * */ public static Date parseDate(String strDate) { if (StringUtils.isNotEmpty(strDate)) { Date date = new Date(); try { // ? date = new SimpleDateFormat("yyyy/MM/dd").parse(strDate); } catch (ParseException e) { //???? e.printStackTrace(); } return date; } else { return null; } } /** * TImageUpload????????TImageUpload? * ????true * * @param TImageUpload * @return boolean * @author Hiroaki * * */ public static boolean deleteFile(TImageUpload tImageUpload) { if (tImageUpload != null) { File file = new File(tImageUpload.filePath); if (file.exists()) { if (file.delete()) { TImageUploadService tImageUploadService = SingletonS2Container .getComponent(TImageUploadService.class); tImageUploadService.delete(tImageUpload); } else { return true; } } else { return true; } } else { return true; } return false; } /** * ??(DateString yyyy-MM-dd) * * */ static public final String DATE_PATTERN = "yyyy-MM-dd"; public static String parsDateToString(Date date) { String str; if (date == null) { str = null; } else { str = new SimpleDateFormat(DATE_PATTERN).format(date); } return str; } /** * ??(DateString yyyy/MM/dd) * * */ static public final String DATE_PATTERN_SLASH = "yyyy/MM/dd"; public static String parsDateToStringSLASH(Date date) { String str; if (date == null) { str = null; } else { str = new SimpleDateFormat(DATE_PATTERN_SLASH).format(date); } return str; } /** * ??(DateString HH:mm:ss) * * */ static public final String TIME_PATTERN = "HH:mm:ss"; public static String parsTimeToString(Date time) { String str; if (time == null) { str = null; } else { str = new SimpleDateFormat(TIME_PATTERN).format(time); } return str; } /** * ? * ??????TImageUpload?id * ?????null * * @param FormFile imageFilePurposeCode * @return Integer * @author Hiroaki * * */ public static Integer createImageFile(FormFile file, String imageFilePurposeCode) { Integer rtnInt = null; //? String rm = RandomStringUtils.randomAlphabetic(10); //????(DB?????????) String path = FilePathCode.HONBAN_IMAGE.getName() + rm + file.getFileName(); //?????????? UploadUtil.write(path, file); //???DB? TImageUpload imageUpload = new TImageUpload(); imageUpload.fileName = rm + file.getFileName(); imageUpload.filePath = path; imageUpload.ImageFilePurpose = Integer.valueOf(imageFilePurposeCode); TImageUploadService tImageUploadService = SingletonS2Container.getComponent(TImageUploadService.class); tImageUploadService.insert(imageUpload); if (imageUpload.id != null) { rtnInt = imageUpload.id; } return rtnInt; } /** * ? * ??????TImageUpload?id * ?????null * * @param FormFile imageFilePurposeCode * @return Integer * @author Hiroaki * * */ public static void createSubmitFile(SubmitForm submitForm) { // ?? submitForm.submitCaptionImageId = createImageFile(submitForm.submitCaptionImageFile, ImageFilePurposeCode.SUBMIT_CAPTION.getCode()); //? String rm = RandomStringUtils.randomAlphabetic(10); //????(DB?????????) String path = FilePathCode.HONBAN_SUBMIT.getName() + rm + submitForm.submitFile.getFileName(); //?????????? UploadUtil.write(path, submitForm.submitFile); //???DB? submitForm.submitProductFileName = submitForm.submitFile.getFileName(); submitForm.submitProductFilePath = path; } /** * ??? * * @param SubmitForm * @param deleteFlag * @return * @author Hiroaki * * */ public static void deleteSubmitFile(SubmitForm submitForm, boolean deleteFlag) { // ?? TImageUploadService tImageUploadService = SingletonS2Container.getComponent(TImageUploadService.class); if (!deleteFile(tImageUploadService.findById(submitForm.submitCaptionImageId))) { submitForm.submitCaptionImageId = null; } // ??? TSubmitService tSubmitService = SingletonS2Container.getComponent(TSubmitService.class); TSubmit tSubmit = tSubmitService.findById(submitForm.id); if (tSubmit != null) { File file = new File(tSubmit.submitProductFilePath); if (file.exists()) { if (file.delete()) { submitForm.submitProductFilePath = null; if (deleteFlag) { tSubmit.submitProductFilePath = null; tSubmit.deleteFlag = true; tSubmitService.update(tSubmit); } } } } } /** * ??Download? * * @param Integer submitId * @return null * @author Hiroaki * * */ public static String submitDownload(Integer submitId) { // ???? TSubmitService tSubmitService = SingletonS2Container.getComponent(TSubmitService.class); TSubmit tSubmit = tSubmitService.findById(submitId); // if (tSubmit != null) { downloadCommon(tSubmit.submitProductFilePath, tSubmit.submitName); } return null; } /** * Download? * * @param filePath * @param fileName * @return null * @author Hiroaki * * */ public static void downloadCommon(String filePath, String fileName) { // ??? FileInputStream fis = null; InputStreamReader isr = null; HttpServletResponse res = ResponseUtil.getResponse(); // ???? OutputStream os = null; OutputStreamWriter osw = null; try { // ?File? File file = new File(filePath); if (!file.exists() || !file.isFile()) { // ??????? } // ?? res.setContentType("application/octet-stream"); res.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("Windows-31J"), "ISO-8859-1")); // ???? fis = new FileInputStream(file); isr = new InputStreamReader(fis, "ISO-8859-1"); // ????? os = res.getOutputStream(); osw = new OutputStreamWriter(os, "ISO-8859-1"); // IO??? int i; while ((i = isr.read()) != -1) { osw.write(i); } } catch (FileNotFoundException e) { // ? } catch (UnsupportedEncodingException e) { // ? } catch (IOException e) { // ? } finally { try { // ? if (osw != null) { osw.close(); } if (os != null) { os.close(); } if (isr != null) { isr.close(); } if (fis != null) { fis.close(); } } catch (IOException e) { // ? } } } }