(上传文件)fastDFSClient----客户端工具类

package cn.kgc.core.util;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDFSClient {

	private TrackerClient trackerClient = null;
	private TrackerServer trackerServer = null;
	private StorageServer storageServer = null;
	private StorageClient1 storageClient = null;

	public FastDFSClient(String conf) throws Exception {
		if (conf.contains("classpath:")) {
			conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
		}
		ClientGlobal.init(conf);
		trackerClient = new TrackerClient();
		trackerServer = trackerClient.getConnection();
		storageServer = null;
		storageClient = new StorageClient1(trackerServer, storageServer);
	}

	/**
	 * 上传文件方法
	 * <p>Title: uploadFile</p>
	 * <p>Description: </p>
	 * @param fileName 文件全路径
	 * @param extName 文件扩展名,不包含(.)
	 * @param metas 文件扩展信息
	 * @return
	 * @throws Exception
	 */
	public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
		String result = storageClient.upload_file1(fileName, extName, metas);
		return result;
	}
	//  这个比较好用
	public String uploadFile(byte[] file, String fileName, long fileSize) throws Exception {
		NameValuePair[] metas = new NameValuePair[3];
		metas[0] = new NameValuePair("fileName", fileName);
		metas[1] = new NameValuePair("fileSize", String.valueOf(fileSize));
		metas[2] = new NameValuePair("fileExt", FilenameUtils.getExtension(fileName));
		String result = storageClient.upload_file1(file, FilenameUtils.getExtension(fileName), metas);
		return result;
	}

	public String uploadFile(String fileName) throws Exception {
		return uploadFile(fileName, null, null);
	}

	public String uploadFile(String fileName, String extName) throws Exception {
		return uploadFile(fileName, extName, null);
	}

	/**
	 * 上传文件方法
	 * <p>Title: uploadFile</p>
	 * <p>Description: </p>
	 * @param fileContent 文件的内容,字节数组
	 * @param extName 文件扩展名
	 * @param metas 文件扩展信息
	 * @return
	 * @throws Exception
	 */
	public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {

		String result = storageClient.upload_file1(fileContent, extName, metas);
		return result;
	}

	public String uploadFile(byte[] fileContent) throws Exception {
		return uploadFile(fileContent, null, null);
	}

	public String uploadFile(byte[] fileContent, String extName) throws Exception {
		return uploadFile(fileContent, extName, null);
	}
}

工具类放在common(公共模块)下面

  

原文地址:https://www.cnblogs.com/Hubert-dzl/p/11563955.html

时间: 2024-07-30 19:01:56

(上传文件)fastDFSClient----客户端工具类的相关文章

FastDFS的配置、部署与API使用解读(2)以字节方式上传文件的客户端代码

1.下载FastDFS的API FastDFS提供Java和PHP等语言的客户端API.可以到FastDFS在Google Code的项目主页 http://code.google.com/p/fastdfs/downloads/list 下载.本文以Java API为例. 2.调用API的上传接口 通过Servlet得到InputStream.文件名称和文件长度,然后通过调用FastDFS提供的Java API把文件上传到FastDFS服务器.下段代码中的getFileBuffer可参考本博客

网络编程之使用HttpClient上传文件的客户端和服务器

1.1客户端: HttpClient常用HttpGet和HttpPost这两个类,分别对应Get方式和Post方式. 无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访问HTTP资源. 1.创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象. 2.使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST请求,并返回HttpResponse对象. 3.通过HttpResp

FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码(转)

调用的API为: String[] upload_file( String group_name,//组名,不指定则可设为null long file_size,//文件大小,必须制定 UploadCallback callback,//回调 String file_ext_name, NameValuePair[] meta_list ) 1 /** 2 * Upload File to DFS, directly transferring java.io.InputStream to jav

使用XWAF框架(2)——上传文件

XWAF提供了上传文件的HttpFileUploader工具类,具备强大的多文件上传.文件类型过滤.文件大小限制.存储目录设置.文件名称更改等功能,简化了Web应用开发的编程工作. 它能同时解析表单参数和文件域,不仅能够将文件保存到服务器上,还能同时提取表单参数的值. 如果要在保存上传文件之前提取参数值,就需要先调用"parseRequest()"方法,并使用该方法返回的对象替换request变量,然后再使用getParameter(name) 方法提取参数值. 参考代码如下: pri

ASP.NET上传文件的几种方法

//上传文件实例 if (fileDealer.HasFile)//判断文件是否存在        {            string filepath = "";            try            {                string path = fileDealer.FileName;                string filename = path.Split('.')[0] + "_" + DateTime.Now

JAVA代码实现上传文件至文件服务器(远程服务器、非项目当前所在服务器)

步骤一:添加依赖 <!--sftp文件上传--> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency> 步骤二:编写工具类 package com.example.vue.vuedemo; import com.jcraft.js

上传文件的响应

package com.lovobook.RandomCodeServlet; import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.Collection; import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax.servlet.

上传文件的表单

1.使用Apache 的 Commons FileUpload FileUpload下载地址: http://commons.apache.org/fileupload/ 下载:commons-fileupload-1.2.2-bin.zip    得到:commons-fileupload-1.2.2.jar http://commons.apache.org/io/ 下载:commons-io-1.4-bin.zip       得到:commons-io-1.4.jar 2.web.xml

asp.net中fileupload上传文件的方法

FileUpload 控件显示一个文本框控件和一个浏览按钮,使用户可以选择客户端上的文件并将它上载到 Web 服务器.用户通过在控件的文本框中输入本地计算机上文件的完整路径(例如,C:\MyFiles\test.txt)来指定要上载的文件.用户也可以通过单击“浏览”按钮,然后在“选择文件”对话框中定位文件来选择文件. 用户选择要上载的文件后,FileUpload 控件不会自动将该文件保存到服务器.您必须显式提供一个控件或机制,使用户能提交指定的文件.例如,可以提供一个按钮,用户单击它即可上载文件

Webform之FileUpload(上传按钮控件)简单介绍及下载、上传文件时图片预览

1.FileUpload上传控件:(原文:http://www.cnblogs.com/hide0511/archive/2006/09/24/513201.html) FileUpload 控件显示一个文本框控件和一个浏览按钮,使用户可以选择客户端上的文件并将它上载到 Web 服务器.用户通过在控件的文本框中输入本地计算机上文件的完整路径(例如,C:\MyFiles\TestFile.txt)来指定要上载的文件.用户也可以通过单击“浏览”按钮,然后在“选择文件”对话框中定位文件来选择文件.