FileIoLocal.java 4.5 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
package com.boco.nbd.wios.downloadfile.fileio;

import com.boco.nbd.wios.downloadfile.mapper.def.CptDataInfoMapper;
import com.boco.nbd.wios.downloadfile.model.CptDataInfo;
import com.boco.nbd.wios.downloadfile.service.DataStoreService;
import com.ihidea.core.support.exception.ServiceException;
import com.ihidea.core.util.DateUtilsEx;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

/**
 * 存储本地文件IO
 * @author TYOTANN
 */
@Component
public class FileIoLocal implements IFileIo {

	private final Log logger = LogFactory.getLog(FileIoLocal.class);

	@Autowired
	private DataStoreService dataStoreService;

	@Resource
	private CptDataInfoMapper dataInfoDao;

	/**
	 * 保存到存储路径
	 */
	@Override
	public void save(FileIoEntity entity) {

		saveFile(entity.getDataInfo().getId(), entity.getContent(),
				getPath(entity.getDataInfo().getStoreName(), entity.getDataInfo().getCreateTime()));

	}

	/**
	 * 保存到备份存储路径
	 */
	@Override
    public void saveBak(FileIoEntity entity) {

		saveFile(entity.getDataInfo().getId(), entity.getContent(),
				getBakPath(entity.getDataInfo().getStoreName(), entity.getDataInfo().getCreateTime()));

	}

	/**
	 * 持久化
	 */
	private void saveFile(String id, byte[] content, String storePath) {

		File file = new File(storePath + File.separator + id);

		if (file.exists()) {
			throw new ServiceException("文件:" + file.getPath() + "已经存在!");
		}

		// 文件夹不存在的话则创建
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}

		try {
			FileUtils.writeByteArrayToFile(file, content);
		} catch (IOException e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public boolean remove(FileIoEntity entity) {

		String path = getPath(entity.getDataInfo().getStoreName(), entity.getDataInfo().getCreateTime());

		File file = new File(path + File.separator + entity.getDataInfo().getId());

		// file.exists()==true?file.delete():true;
		return file.delete();
	}

	@Override
	public byte[] get(String id) {

		CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);

		String path = getPath(dataInfo.getStoreName(), dataInfo.getCreateTime());

		try {

			File downloadFile = new File(path + File.separator + id);

			if (downloadFile.exists()) {
				return FileUtils.readFileToByteArray(downloadFile);
			} else {
				return null;
			}

		} catch (IOException e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public void execute(FileIoEntity fileIoEntity, IFileInputStream fileInputStreamImpl) throws Exception {

		String filePath = getPath(fileIoEntity.getDataInfo().getStoreName(), fileIoEntity.getDataInfo().getCreateTime()) + File.separator
				+ fileIoEntity.getDataInfo().getId();
		File downloadFile = new File(filePath);

		if (downloadFile.exists()) {

			FileInputStream fis = null;

			try {
				fis = FileUtils.openInputStream(downloadFile);
				fileInputStreamImpl.execute(fileIoEntity, fis);
			} catch (ClientAbortException e) {
			} catch (IOException e) {
				throw new ServiceException(e);
			} finally {
				if (fis == null) {
					IOUtils.closeQuietly(fis);
				}
			}
		} else {
			logger.info("文件不存在:" + filePath);
			fileInputStreamImpl.execute(null, null);
		}

	}

	private String getPath(String storeName, Date createDate) {

		String storePath = dataStoreService.getInfoByName(storeName).getPath();

		return storePath + DateUtilsEx.formatToString(createDate, DateUtilsEx.DATE_FORMAT_DAY).replace(".", File.separator);
	}

	private String getBakPath(String storeName, Date createDate) {

		String storePath = dataStoreService.getInfoByName(storeName).getBakPath();

		return storePath + DateUtilsEx.formatToString(createDate, DateUtilsEx.DATE_FORMAT_DAY).replace(".", File.separator);
	}

	@Override
	public void updateContent(String id, byte[] content) {

		CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);

		String path = getPath(dataInfo.getStoreName(), dataInfo.getCreateTime());

		File file = new File(path + File.separator + id);

		file.deleteOnExit();
		try {
			FileUtils.writeByteArrayToFile(file, content);
		} catch (IOException e) {
			throw new ServiceException(e);
		}
	}
}