TFS今天刚刚做好的。

TFS今天刚刚做好的。

Mark一下,坐个记录


1. 搭建TFS服务器.

详见google

TFS服务器环境:

Centos

Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013


2. 开发环境:spring+springmvc+mybatis

3. 引入Jar包:
直接下载http://code.taobao.org/p/tfs-client-java/file/404/tfs-javaclient-2.1.1.jar
下载http://code.taobao.org/p/tair-client-java/file/40/tair-client-2.3.1.jar
还要用到一个叫apache-mina的包。
然后在本地的Nexus上加依赖.
最后在parent的pom.xml里面加上


<!-- TFS的相关依赖 -->
<dependency>
	<groupId>com.taobao.common.tair</groupId>
	<artifactId>common-tair</artifactId>
	<version>2.3.1</version>
</dependency>
<dependency>
	<groupId>com.taobao.common.tfs</groupId>
	<artifactId>tfs-javaclient</artifactId>
	<version>2.1.1</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.apache.mina/mina-core -->
<dependency>
	<groupId>org.apache.mina</groupId>
	<artifactId>apache-mina</artifactId>
	<version>2.0.6</version>
</dependency>
<!-- TFS的相关依赖 Over -->



然后在core里面的pom.xml里面依赖。

3. 做xml配置
TFS.xml文件

<?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
        default-lazy-init="true">
    <bean id="tfsManager" class="com.taobao.common.tfs.DefaultTfsManager" init-method="init" >  
      <!-- 整个进程中系统最多等待多少个请求,取决于你有多少个线程并发的请求TFS -->  
       <property name="maxWaitThread">  
        <value>100</value>
      </property>  
      <!-- 单个请求最大的等待时间(ms) 超过这个时间放弃这次请求-->  
      <property name="timeout">  
        <value>2000</value>  
      </property>
      <!-- Tfs master nameserver ip address -->  
      <property name="nsip">
        <value>10.0.1.228:8666</value>  
      </property>
      <!-- TFS 集群的编号,这个编号只是一种参考,系统初始化的时候会从ns上取,取不到才用本地设置的.!-->  
      <property name="tfsClusterIndex">  
        <value>1</value>
      </property>
      <!-- TFS在读取文件的时候会缓存block所在的数据服务器ip,这个参数配置了最多缓存的记录个数!-->  
      <property name="maxCacheItemCount">
        <value>10000</value>
      </property>
      <!-- 上一项缓存最大有效的时间(ms)!-->
      <property name="maxCacheTime">
        <value>5000</value>
      </property>
   </bean>
</beans>

4. 写了一个TFSUtil.java的工具类


package com.esteel.until;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.util.IOUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.taobao.common.tfs.TfsManager;
import com.taobao.common.tfs.packet.FileInfo;

public class TFSUtil {

	static ApplicationContext context = new ClassPathXmlApplicationContext("config_spring/TFS.xml");

	private static TfsManager getTfSManager() {
		return (TfsManager) context.getBean("tfsManager");
	}

	private static String getFileExt(String fileName) {
		return fileName.substring(fileName.lastIndexOf("."));
	}

	/*存储文件后返回在TFS中存储的文件码*/
	public static String saveTfsByteFile(byte[] fileBytes, String fileName) {
		TfsManager tfsManager = getTfSManager();
		String fileExt = getFileExt(fileName);
		String tfsfileName = tfsManager.saveFile(fileBytes, null, fileExt, true);
		return tfsfileName;
	}
	
	/*存储文件后返回在TFS中存储的文件码*/
	public static String saveTfsFile(InputStream inputStream, String fileName) {
		try {
			TfsManager tfsManager = getTfSManager();
			byte[] fileBytes = IOUtils.toByteArray(inputStream);
			String fileExt = getFileExt(fileName);
			String tfsfileName = tfsManager.saveFile(fileBytes, null, fileExt, true);
			return tfsfileName;
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static OutputStream getTfsFile(String tfsFileName) {
		TfsManager tfsManager = getTfSManager();
		OutputStream outputStream = new ByteArrayOutputStream();
		boolean result = tfsManager.fetchFile(tfsFileName, "", outputStream);
		if (result) {
			return outputStream;
		} else {
			return null;
		}
	}

	public static void deleteTfsFile(String tfsFileName) {
		TfsManager tfsManager = getTfSManager();
		boolean flag = tfsManager.unlinkFile(tfsFileName, null);
		System.out.println(flag);
	}

	public void fetchTfsFile(String tfsFileName, String newFileName) {
		TfsManager tfsManager = getTfSManager();
		boolean flag = tfsManager.fetchFile(tfsFileName, null, newFileName);
		System.out.println(flag);
	}

	public void hideTfsFile(String tfsFileName, int isHidden) {
		TfsManager tfsManager = getTfSManager();
		boolean flag = tfsManager.hideFile(tfsFileName, null, isHidden);
		System.out.println(flag);
	}

	public FileInfo stateTfsFile(String tfsFileName) {
		TfsManager tfsManager = getTfSManager();
		FileInfo fileInfo = tfsManager.statFile(tfsFileName, null);
		return fileInfo;
	}

}

5. 调用工具类的类


package com.esteel.until;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import com.esteel.until.TFSUtil;

public class UpLoadUtil6 {

	/*存储文件返回格式为"/file/upload/"+picture.getOriginalFilename()+"/"+uploadMethod6(picture)+"/open"*/
	public static String uploadMethodWithPath(MultipartFile picture)
			throws IOException {
		/*String dateString = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
		indentity=indentity + File.separator
				+ dateString + File.separator + UUID.randomUUID().toString().replaceAll("-", "");*/
		return "/file/upload/"+picture.getOriginalFilename()+"/"+uploadMethod6(picture)+"/open";
	}

	/*存储文件返回格式tfsFileName*/
	public static String uploadMethod6(MultipartFile picture)
			throws IOException {
		String tfsFileName = "";
		String fileName="";
		if (!picture.isEmpty()) {
			/*String path = request.getSession().getServletContext().getRealPath("/") + File.separator + "upload"
					+ File.separator + indentity;*/
			fileName = picture.getOriginalFilename();
			tfsFileName = TFSUtil.saveTfsByteFile(picture.getBytes(), fileName);
			/*pathnew = File.separator + "upload" + File.separator + indentity + File.separator + fileName;
			System.out.println(fileName);
			File targetFile = new File(path, fileName);
			if (!targetFile.exists()) {
				targetFile.mkdirs();
			}
			picture.transferTo(targetFile);*/
		}
		return tfsFileName;
	}
	
	/*删除文件,格式为/file/upload/"+picture.getOriginalFilename()+"/"+uploadMethod6(picture)+"/open*/
	public static void deleteFileWithPath(String fileName) {
		/*String fileFullPath = request.getSession().getServletContext().getRealPath("/") + filePath;
		(new File(fileFullPath)).delete();
		String folderName = fileFullPath.substring(0, fileFullPath.lastIndexOf(File.separator));
		deleteDir(new File(folderName));*/
		String[] temp = fileName.split("file/upload/");
		String[] temp1 = temp[1].split("/");
		TFSUtil.deleteTfsFile(temp1[1]);
	}
	
	/*删除文件,格式为tfsFileName*/
	public static void deleteFile(String fileName) {
		/*String fileFullPath = request.getSession().getServletContext().getRealPath("/") + filePath;
		(new File(fileFullPath)).delete();
		String folderName = fileFullPath.substring(0, fileFullPath.lastIndexOf(File.separator));
		deleteDir(new File(folderName));*/
		TFSUtil.deleteTfsFile(fileName);
	}

}

6. 然后controller里面读文件


package com.esteel.system.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.esteel.exception.EsteelException;
import com.esteel.until.TFSUtil;

@Controller
public class TFSFileController extends BaseController {

	@RequestMapping(value = "/file/upload/{originName}/{fileName}/open")
	public void tbBasBedSrtList(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			@PathVariable String fileName,
			@PathVariable String originName) throws EsteelException, IOException {
		response.setCharacterEncoding("UTF-8");
		System.out.println(fileName);
		ByteArrayOutputStream fosByte = (ByteArrayOutputStream) TFSUtil.getTfsFile(fileName);
		byte[] bytes = fosByte.toByteArray();
		ByteArrayInputStream fis = new ByteArrayInputStream(bytes);
        byte[] bytesRead = new byte[1024*1024];
        int length = 0;
        while((length=fis.read(bytesRead))!=-1){
            response.getOutputStream().write(bytesRead,0,length);
        }
	}

}

7. 保存文件




	@RequestMapping(value = "/system/tbPactContracttpl/insert", method = RequestMethod.POST)
	public ModelAndView insert(@RequestParam(value = "tempFile", required = false) MultipartFile tempFile,
			TbPactContracttpl tbPactContracttpl, HttpServletRequest request) throws IOException {
		if (tempFile != null && tempFile.getSize() > 0) {
			String dateString = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
			String pathnew = UpLoadUtil6.uploadMethodWithPath( tempFile);
			tbPactContracttpl.setTempPath(pathnew);
		}
		try {
			tbPactContracttplService.insertTbPactContracttpl(setDateAndUpdateUser(tbPactContracttpl));
		} catch (EsteelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ajaxDoneTbPactContracttpl(200, "操作成功", "");
	}



大工告成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值