mogilefs文件系统TestCase
去年架设了mogilefs文件系统,采用主从式双机(程序只访问主机,主机会自动将文件同步到从机上),运行一年多以来比较稳定。于是,前期有个涉及附件(主要是word等office文档)上传、下载的项目也打算采用mogilefs文件系统。由于mogilefs文件系统服务器进行了严格的IP地址和端口控制,因此为了访问mogilefs文件系统服务器就必须开通IP地址和端口访问权限。为此,我给几个开发人员申请了相应的访问权限,但是发布成web应用测试的时候,老是程序报错,无奈又给写了一个最简单的mogilefs文件TestCase。
/** * bighrose */ package com.mogilefs;
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream;
import org.junit.Test;
import com.guba.mogilefs.PooledMogileFSImpl; import com.guba.mogilefs.MogileFS;
public class TestMogileFS{
private String domain = "tstdomain"; private String server = "ip:6001"; private String storageClass = "tstclass"; private String fileName = "d:\\ADempiere_new.pdf";
// @Test //上传文件 public void testUpload(){ try{ MogileFS mfs = new PooledMogileFSImpl(domain, new String[] { server }, 0, 1, 10000); File file = new File("d:\\ADempiere.pdf"); if(file.exists()){ String fileKey = "ADempiere"+System.currentTimeMillis()+".pdf"; OutputStream out = mfs.newFile(fileKey, storageClass, file.length()); FileInputStream in = new FileInputStream(file); byte[] buffer = new byte[1024]; int count = 0; while ((count = in.read(buffer)) >= 0) { out.write(buffer, 0, count); } in.close(); out.close(); System.out.println("fileKey: "+fileKey); } System.out.println("upload file sucessed!"); }catch(Exception e){ e.printStackTrace(); } }
@Test public void testGetFile(){ try{ MogileFS mfs = new PooledMogileFSImpl(domain, new String[] { server }, 0, 1, 10000); byte[] content = mfs.getFileBytes("ADempiere1264139404773.pdf"); FileOutputStream fos=null; try{ fos=new FileOutputStream(fileName,true); fos.write(content); fos.flush(); }catch(Exception e){ e.printStackTrace(); } finally{ try{ fos.close(); }catch(IOException iex){} } System.out.println("Get file sucessed! "+fileName); }catch(Exception e){ e.printStackTrace(); } } } |