JBoss.orgCommunity Documentation

第 12 章 文件存储

12.1. 概述
12.2. 接口文档
12.3. 代码示例

文件存储服务为开发者提供了存储文件等分布式文件系统存储的接口,使用时开发者无需关心文件存储基础设施存储方式。

请参考 apidocs

存储文件

 //存储扩展名为zip的文件 
FileInputStream in = new FileInputStream(file);
FileMeta fileMeta = new FileMeta();
fileMeta.put("fileName", fileName);
ServiceResult<DFSFile> dfsfile = 
 DFileSystemAPI.getInstance().uploadFile("zip",fileMeta, mfile.getSize(), in);
if (dfsfile.isSucceed() && null != dfsfile.getData()) {
//返回存储成功以及文件存储的文件ID
String dFileId = dfsfile.getData().getFileId();
}
            

删除文件:

 //删除文件ID为dFileId的文件
String dFileId = "...";
DFileSystemAPI.getInstance().deleteFile(dFileId);
            

下载文件:

/*
* Copyright (c) 2012-2014, EpicSaaS Yuan Xin technology Co., Ltd.
* 
* All rights reserved.
 */
package com.epicsaas.app.developer.controller.pc;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.epicpaas.sdk.core.api.ServiceResult;
import com.epicpaas.sdk.core.api.dfs.FileMeta;
import com.epicpaas.sdk.core.api.logging.Logger;
import com.epicpaas.sdk.core.api.logging.LoggerFactory;
import com.epicsaas.api.dfs.DFileSystemAPI;

/**
* 下载中心
* @author ghg
*/
@Controller
@RequestMapping(value = "/pc/download")
public class DownLoadController {

	private static Logger LOG = LoggerFactory
			.getLogger(DownLoadController.class);

	@RequestMapping(value = "", method = { RequestMethod.GET,
			RequestMethod.POST })
	public void hello(Model model, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		LOG.info("有访问来自,IP: %s USER-AGENT: %s", request.getRemoteAddr(),
				request.getHeader("user-agent"));
		LOG.info("SessionId %s", request.getSession().getId());
		response.setContentType("text/html;charset=UTF-8");

		String fieldId = request.getParameter("fieldId");

		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		InputStream in = DFileSystemAPI.getInstance().downLoadFile(fieldId);
		ServiceResult<FileMeta> ret = DFileSystemAPI.getInstance().getFileMeta(
				fieldId);

		if (ret.isSucceed() && null != ret.getData()) {
			FileMeta fileMeta = ret.getData();

			String fileName = (String) fileMeta.get("fileName");
			// fileName = "sss.png";
			if (null != fileName) {
				response.setHeader("Content-disposition",
						"attachment; filename=".concat(fileName));
			}
			String fileLength = (String) fileMeta.get("fileLength");
			// fileName = "100";
			if (null != fileLength) {
				response.setHeader("Content-Length", String.valueOf(fileLength));
			}
		}

		try {
			bos = new BufferedOutputStream(response.getOutputStream());
			bis = new BufferedInputStream(in);
			byte[] buff = new byte[1024 * 10];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (IOException e) {
			LOG.error(e, "%s", e.getMessage());
		} finally {
			try {

				if (null != bis) {
					bis.close();
				}
				if (null != bos) {
					bos.flush();
					// 清空输出缓冲流
					bos.close();
				}

			} catch (IOException e) {
         LOG.error(e, "%s", e.getMessage());
			}
		}
	}

}