当前位置 : 主页 > 网页制作 > Nodejs >

webservice 文件上传 与 fastdfs 整合

来源:互联网 收集:自由互联 发布时间:2021-06-24
使用fastdfs 存储文件,Server端只做文件处理 不做存储。 卡在DataSource上了,把DataSource 实现重点展示一下 先上图 文件上传 文件下载 关键代码 实现DataSource接口 package com.bizfty.file.service

使用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(), 0, 0, callback);

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

     }

网友评论