FAST DFS 流上传
Posted IT的鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FAST DFS 流上传相关的知识,希望对你有一定的参考价值。
@RequestMapping(value = "/fastDFSUpload", method = RequestMethod.POST)
@ResponseBody
public void fastDFSUpload(@RequestParam("file") MultipartFile file) throws IOException
try // 初始化全局配置。加载一个配置文件。
ClientGlobal.init("D:\\\\dfs.conf");
// 创建一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
// 创建一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 声明一个StorageServer对象,null。
StorageServer storageServer = null;
// 获得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
InputStream inputStream = file.getInputStream();
String name=file.getOriginalFilename();
String exatName=name.substring(name.lastIndexOf(".") + 1, name.length());
int fileLength =inputStream.available();//获取文件大小
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName",name);
metaList[1] = new NameValuePair("fileExtName",exatName );
metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));
UploadFileSender us = new UploadFileSender(inputStream);
String[] strings = storageClient.upload_file(null,fileLength,us, "txt", metaList);
for (String string : strings)
System.out.println(string);
catch (IOException e)
e.printStackTrace();
catch (MyException e)
e.printStackTrace();
package com.bianq.learn.demo.message;
import org.csource.fastdfs.UploadCallback;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
class UploadFileSender implements UploadCallback
private InputStream inStream;
public UploadFileSender(InputStream inStream)
this.inStream = inStream;
public int send(OutputStream out) throws IOException
byte[] bs = new byte[1024];
int i=0;
try
while ((i = inStream.read(bs)) != -1)
out.write(bs, 0, i);
out.flush();
catch (Exception e)
e.printStackTrace();
return 0;
以上是关于FAST DFS 流上传的主要内容,如果未能解决你的问题,请参考以下文章
为啥通过内存流上传 JSON 时 BlobClient.UploadAsync 会挂起?