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

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);
}*/

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

  1. java web service 上传下载文件

    1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...

  2. 【WCF】利用WCF实现上传下载文件服务

    引言     前段时间,用WCF做了一个小项目,其中涉及到文件的上传下载.出于复习巩固的目的,今天简单梳理了一下,整理出来,下面展示如何一步步实现一个上传下载的WCF服务. 服务端 1.首先新建一个名 ...

  3. WebSSH画龙点睛之lrzsz上传下载文件

    本篇文章没有太多的源码,主要讲一下实现思路和技术原理 当使用Xshell或者SecureCRT终端工具时,我的所有文件传输工作都是通过lrzsz来完成的,主要是因为其简单方便,不需要额外打开sftp之 ...

  4. SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例

    本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...

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

    C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...

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

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

  7. linux上很方便的上传下载文件工具rz和sz

    linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...

  8. shell通过ftp实现上传/下载文件

    直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...

  9. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

随机推荐

  1. ClickOnce是什么?如何使用?

    ClickOnce是一种部署方式,主要解决C/S软件版本更新后,所有客户端都要重新安装的问题. 场景:假如,你的客户有1000个,你的服务端更新后,这1000个客户都要重新安装,难道要员工一个一个的电 ...

  2. Android中对Apk加固(加壳)续篇之---对Native层(so文件)进行加固

    有人说Android程序用Java代码写的,再怎么弄都是不安全的,很容易破解的,现在晚上关于应用加固的技术也很多了,当然这些也可以用于商业发展的,梆梆加密和爱加密就是很好的例子,当然这两家加固的Apk ...

  3. 【HDOJ6638】Snowy Smile(线段树)

    题意:一个二维平面上有n个点,每个点的坐标是(x[i],y[i]),权值是w[i] 求一个矩形使得其中所有点的权值和最大,输出权值和 n<=2e3,x[i],y[i],w[i]的绝对值<= ...

  4. 当执行一条查询语句时,MySQL内部经历了什么?

    假如说我们有一张表 T ,表里只有一个字段 ID,当我们执行下边这条SQL语句时: mysql> select * fron T where ID=10; 在我们眼中能看到的只是输入一条 SQL ...

  5. 批量执行SQL脚本

    新建文件夹all_sql,并将需要执行的sql脚本放入其中. 新建bat脚本,执行即可,ORACLE 也可改Mysql,按需:如下 ::echo off :: @echo off echo 开始执行数 ...

  6. Golang通过反射获取结构体的标签

    Golang通过反射获取结构体的标签 例子: package main import ( "fmt" "reflect" ) type resume struc ...

  7. WPF数据模板中绑定事件不触发问题

    今天比较闲,做一个练手的项目,结果在xaml中写了一个用户的数据模板后,在其中的某个Canvas上绑定了一个鼠标左击的事件,结果调试的时候,无论怎么点击都不跳到断点那里,百思不得其解. 之后尝试不绑定 ...

  8. 【PP系列】SAP 取消报工后修改日期

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[PP系列]SAP 取消报工后修改日期   前言 ...

  9. PHPer面试指南-laravel 篇

    简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件. 注册类文件自动加载器 : Laravel通过 composer ...

  10. mybatis动态注解sql编写注意事项

    最近在编写mybatis的动态注解sql遇到了不少的坑,在网上看到一篇讲的比较详细的文章,记录一下: https://mbd.baidu.com/newspage/data/landingshare? ...