加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice 文件上传 与 fastdfs 整合

发布时间:2020-12-16 22:06:00 所属栏目:安全 来源:网络整理
导读:使用fastdfs 存储文件,Server端只做文件处理 不做存储。 ? 卡在DataSource上了,把DataSource 实现重点展示一下 ? 先上图 ? 文件上传 ? 文件下载 ? 关键代码 ? 实现DataSource接口 ? package com.bizfty.file.service; ? import com.bizfty.file.model.File

使用fastdfs 存储文件,Server端只做文件处理 不做存储。

?

卡在DataSource上了,把DataSource 实现重点展示一下

?

先上图

?

文件上传


?

文件下载


?

关键代码

?

实现DataSource接口

?

package com.bizfty.file.service;

?

import com.bizfty.file.model.FileInfo;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.activation.DataSource;

?

/**

?*

?* @author johnson

?*/

public class FileDataSource implements DataSource {

?

? ? private final FileService service;

? ? ? ?

? ? private final FileInfo fileInfo;

? ??

? ? public FileDataSource(FileService service,FileInfo fileInfo){

? ? ? ? this.service = service;

? ? ? ? this.fileInfo = fileInfo;

? ? }

?

? ? @Override

? ? public InputStream getInputStream() throws IOException {

? ? ? ? return service.download(fileInfo.getPath());

? ? }

?

? ? @Override

? ? public OutputStream getOutputStream() throws IOException {

? ? ? ? //return service.upload(fileInfo,status);

? ? ? ? return null;

? ? }

?

? ? @Override

? ? public String getContentType() {

? ? ? ? return fileInfo.getMimeType();

? ? }

?

? ? @Override

? ? public String getName() {

? ? ? ? return fileInfo.getName();

? ? }

}

?

?

----------------------------------------------------------------------------------------

?

? ? @Override

? ? public UploadFileResponse uploadFile(UploadFileRequest request) throws BusinessException,TechnicalException {

? ? ? ? UploadFileResponse response = new UploadFileResponse();

? ? ? ? try {

? ? ? ? ? ? String path = fileService.upload(request.getFileHandler().getInputStream(),request.getFileInfo().getSize(),request.getFileInfo().getName());

? ? ? ? ? ? request.getFileInfo().setPath(path);

? ? ? ? } catch (IOException ex) {

? ? ? ? ? ? logger.error("IO exception",ex);

? ? ? ? }

? ? ? ? response.setFileInfo(request.getFileInfo());

? ? ? ? return response;

? ? }

?

? ? @Override

? ? public DownloadFileResponse downloadFile(DownloadFileRequest request) throws BusinessException,TechnicalException {

?

? ? ? ? DownloadFileResponse response = new DownloadFileResponse();

? ? ? ??

? ? ? ? response.setFileHandler(new DataHandler(new FileDataSource(fileService,request.getFileInfo())));

? ? ? ??

? ? ? ? return response;

? ? }

?

----------------------------------------------------------------------------------------

? ? @Override

? ? public String upload(InputStream inputStream,String fileName,String fileExtName,long fileSize,String groupName) {

? ? ? ? StorageNode client = getTrackerClient().getStoreStorage(groupName);

? ? ? ? StorageUploadFileCommand command = new StorageUploadFileCommand(client.getStoreIndex(),inputStream,

? ? ? ? ? ? ? ? fileExtName,fileSize,false);

? ? ? ? StorePath storePath = getConnectionManager().executeFdfsCmd(client.getInetSocketAddress(),command);

? ? ? ? logger.debug("上传的 store path:{}",storePath.toString());

? ? ? ? return storePath.toHexString();

? ? }

?

? ? @Override

? ? public InputStream download(String path) {

? ? ? ? StorePath storePath = new StorePath();

? ? ? ? storePath.formHexString(path);

? ? ? ? StorageNodeInfo client = trackerClient.getFetchStorage(storePath.getGroup(),storePath.getPath());

? ? ? ? DownloadInputStream callback = new DownloadInputStream();

? ? ? ? StorageDownloadFileCommand<InputStream> command = new StorageDownloadFileCommand<InputStream>(storePath.getGroup(),storePath.getPath(),callback);

? ? ? ? return connectionManager.executeFdfsCmd(client.getInetSocketAddress(),command);

? ? ?}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读