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

简介: 在上传代码中添加一下代码 suffix=suffix.substring(1); fast.FastDFSFile file = new fast.FastDFSFile(mFile.

 

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

 

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

目录
相关文章
|
2月前
|
机器学习/深度学习 人工智能 前端开发
机器学习PAI常见问题之web ui 项目启动后页面打不开如何解决
PAI(平台为智能,Platform for Artificial Intelligence)是阿里云提供的一个全面的人工智能开发平台,旨在为开发者提供机器学习、深度学习等人工智能技术的模型训练、优化和部署服务。以下是PAI平台使用中的一些常见问题及其答案汇总,帮助用户解决在使用过程中遇到的问题。
|
3月前
|
数据可视化 Shell Linux
shell+crontab+gitlab实现ecs服务器文件的web展示
本文通过把ecs服务器上的文件定时上传至gitlab,实现文件的页面可视化和修改历史。技术点:shell、crontab、gitlab。
54 3
|
3天前
|
Go
golang学习3,golang 项目中配置gin的web框架
golang学习3,golang 项目中配置gin的web框架
|
9天前
|
前端开发 JavaScript Python
使用Python读取本地行情csv文件,做出web网页画出K线图实现案例
【5月更文挑战第4天】使用Python绘制K线图的步骤:1) 安装pandas, matplotlib和Flask;2) 用pandas读取CSV文件并处理数据;3) 创建Flask应用,渲染包含K线图数据的HTML;4) 编写HTML,使用ECharts库绘制K线图。
26 0
|
12天前
|
缓存 前端开发 Java
15:Servlet 3.0文件上传与下载-Java Web
15:Servlet 3.0文件上传与下载-Java Web
27 5
|
1月前
|
Java Maven Spring
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)2
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
26 2
|
1月前
|
Java Maven 开发工具
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)1
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
27 2
|
2月前
|
前端开发 JavaScript 数据管理
描述一个使用Python开发Web应用程序的实际项目经验,包括所使用的框架和技术栈。
使用Flask开发Web应用,结合SQLite、Flask-SQLAlchemy进行数据管理,HTML/CSS/JS(Bootstrap和jQuery)构建前端。通过Flask路由处理用户请求,模块化代码提高可维护性。unittest进行测试,开发阶段用内置服务器,生产环境可选WSGI服务器或容器化部署。实现了用户注册登录和数据管理功能,展示Python Web开发的灵活性和效率。
16 4
|
2月前
|
运维 JavaScript 前端开发
发现了一款宝藏学习项目,包含了Web全栈的知识体系,JS、Vue、React知识就靠它了!
发现了一款宝藏学习项目,包含了Web全栈的知识体系,JS、Vue、React知识就靠它了!