web项目使用fastdsf上传|下载文件

在上传代码中添加一下代码

suffix=suffix.substring(1);
fast.FastDFSFile file = new fast.FastDFSFile(mFile.getBytes(), suffix);
NameValuePair[] meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("fileName", mFile.getOriginalFilename());
meta_list[1] = new NameValuePair("fileLength", String.valueOf(mFile.getSize()));
meta_list[2] = new NameValuePair("fileExt", suffix);
meta_list[3] = new NameValuePair("fileAuthor", "WangLiang");
String filePath = fast.FileManager.upload(file, meta_list);
System.out.println(filePath);

public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageClient1 storageClient = null;
private StorageServer storageServer = 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);

}

public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}

public String uploadFile(String fileName) throws Exception {
String result = storageClient.upload_file1(fileName, null, null);
return result;
}

public String uploadFile(String fileName, String extName) throws Exception {
String result = storageClient.upload_file1(fileName, extName, null);
return result;
}
}

public class FastDFSFile {

private static final long serialVersionUID = 1L;

private byte[] content;
private String name;
private String ext;
private String length;

public FastDFSFile(byte[] content, String ext) {
this.content = content;
this.ext = ext;
}

public FastDFSFile(byte[] content, String name, String ext) {
this.content = content;
this.name = name;
this.ext = ext;
}

public FastDFSFile(byte[] content, String name, String ext, String length
) {
this.content = content;
this.name = name;
this.ext = ext;
this.length = length;
}

public byte[] getContent() {
return content;
}

public void setContent(byte[] content) {
this.content = content;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getExt() {
return ext;
}

public void setExt(String ext) {
this.ext = ext;
}

public String getLength() {
return length;
}

public void setLength(String length) {
this.length = length;
}

}

public class FileManager implements FileManagerConfig {

private static final long serialVersionUID = 1L;
private static TrackerClient trackerClient;
private static TrackerServer trackerServer;
private static StorageServer storageServer;
private static StorageClient storageClient;

static {
try {
String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath();

String fdfsClientConfigFilePath = classPath + File.separator + CLIENT_CONFIG_FILE;
ClientGlobal.init(fdfsClientConfigFilePath);

trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();

storageClient = new StorageClient(trackerServer, storageServer);

} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 文件上传
* @param file
* @param valuePairs
* @return
*/
public static String upload(FastDFSFile file, NameValuePair[] valuePairs) {
String[] uploadResults = null;
try {
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), valuePairs);
} catch (Exception e) {
e.printStackTrace();
}
String groupName = uploadResults[0];
String remoteFileName = uploadResults[1];

String fileAbsolutePath = PROTOCOL + TRACKER_NGNIX_ADDR + SEPARATOR + groupName + SEPARATOR + remoteFileName;
return fileAbsolutePath;
}

/**
* 文件下载
* @param groupName
* @param remoteFileName
* @param specFileName
* @return
*/
public static ResponseEntity<byte[]> download(String groupName, String remoteFileName, String specFileName) {
byte[] content = null;
HttpHeaders headers = new HttpHeaders();
try {
content = storageClient.download_file(groupName, remoteFileName);
headers.setContentDispositionFormData("attachment",
new String(specFileName.getBytes("UTF-8"), "iso-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
}
}

public interface FileManagerConfig extends Serializable {

public static final String PROTOCOL = "http://";

public static final String SEPARATOR = "/";

public static final String TRACKER_NGNIX_ADDR = "192.168.111.136";

public static final String TRACKER_NGNIX_PORT = "";

public static final String CLIENT_CONFIG_FILE = "client.conf";
}

tracker_server=192.168.111.136:22122

下载

根据之前上传保存的物理路径 截取

/**
* 下载 截取物理路径下载
* @param awardModel
* @param request
* @param response
* @param createtime
* @param endtime
* @param act_id
* @throws Exception
*/
/*@RequestMapping(value = "/{username}/download", method = RequestMethod.GET)
public ResponseEntity<byte[]> download(@PathVariable String username, Model model,HttpServletResponse response) throws IOException, MyException {
User u = users.get(username);
String filePath = u.getFilePath();
String substr = filePath.substring(filePath.indexOf("group"));
String group = substr.split("/")[0];
String remoteFileName = substr.substring(substr.indexOf("/")+1);
String specFileName = username + substr.substring(substr.indexOf("."));
return FileManager.download(group, remoteFileName,specFileName);
}*/

时间: 2024-10-13 01:08:49

web项目使用fastdsf上传|下载文件的相关文章

WebService中实现上传下载文件

不多说,直接看代码: /*上传文件的WebService*/ using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using System.IO; /// <summ

C#实现http协议支持上传下载文件的GET、POST请求

C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Collections; using System.IO; using System.Text.RegularExpressions; using RE = System.Text.Regula

SFTP上传下载文件

secureCRT SFTP上传/下载文件 远程登陆IP secureCRT会话中点击SFTP 3.cd  /home/dowload       linux平台切换到/home/dowload目录 4.cd d:\   windows平台切换到d盘 5.put 文件名           上传 /home/dowload目录下 6.get 文件名   下载文件到windows平台 d盘

Linux上传下载文件

2种方式:xftp(工具).lrzsz xftp:协议--SFTP.端口号--22 lrzsz: rz,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具. 优点就是不用再开一个sftp工具登录上去上传下载文件. sz(下载):将选定的文件发送(send)到本地机器 rz(上传):运行该命令会弹出一个文件选择窗口,从本地选择文件上传到Linux服务器 安装命令:yum install lrzsz 从服务端发送文件到客户端:sz filename 从客户端上传文件到服务

向云服务器上传下载文件方法汇总(转)

转载于:https://yq.aliyun.com/articles/64700 摘要: 一.向Windows服务器上传下载文件方式 方法有很多种,此处介绍远程桌面的本地资源共享方法. 1.运行mstsc,连接远程桌面的时候,点"选项>>" 2."本地资源"-->详细信息. 3."磁盘驱动器"前面打钩. 一.向Windows服务器上传下载文件方式 方法有很多种,此处介绍远程桌面的本地资源共享方法. 1.运行mstsc,连接远程桌

向linux服务器上传下载文件方式收集

向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ssh,并且和ssh 使用相同的认证方式,提供相同的安全保证 . 命令格式: scp [参数] <源地址(用户名@IP地址或主机名)>:<文件路径> <目的地址(用户名 @IP 地址或主机名)>:<文件路径> 举例: scp /home/work/source.

rz和sz上传下载文件工具lrzsz

######################### rz和sz上传下载文件工具lrzsz ############################################################ 1 rpm -qa |grep lrzsz 如果没有用RPM安装即可: 2 rpm -ivh lrzsz-0.12.20-27.1.el6.x86_64.rpm (去光盘里找) 或者 yum install -y lrzsz 即可. ###########################

python 实现ssh远程执行命令 上传下载文件

使用密码远程执行命令 [[email protected] script]# cat daramiko_ssh.py  #!/usr/bin/env python #_*_coding:utf-8 _*_ __author__ = 'gaogd' import paramiko import sys,os host = sys.argv[1] user = 'root' password = 'ddfasdsasda2015' cmd = sys.argv[2] s = paramiko.SSH

linux下lrzsz安装过程,SecureCRT上传下载文件工具

linux下lrzsz安装过程,SecureCRT上传下载文件工具 1.从下面的地址下载 lrzsz-1.12.20.tar.gz http://down1.chinaunix.net/distfiles/lrzsz-0.12.20.tar.gz 2.查看里面的INSTALL文档了解安装参数说明和细节 3.解压文件 tar zxvf lrzsz-1.12.20.tar.gz 4.进入目录 cd lrzsz-1.12.20 5../configure --prefix=/usr/local/lrz