Dubbo+RESTEasy实现文件上传

Dubbo+RESTEasy实现文件上传

  •  9个月前
  •  429
  •  Java Dubbo RESTEasy 

介绍两种Dubbo结合RESTEasy上传文件的方式:

  • 使用MultipartFormDataInput
  • 使用@MultipartForm

Maven依赖

<!-- skip dubbo dependency -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>3.0.19.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.0.19.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-multipart-provider</artifactId>
    <version>3.0.19.Final</version>
</dependency>

后端代码

  • 接口

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;

@Path("resteasy")
@Produces({ContentType.APPLICATION_JSON_UTF_8})
public interface RestEasyUploadService {

    @POST
    @Path("/upload/1")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    Response uploadViaMultipartFormDataInput(MultipartFormDataInput input);

    @POST
    @Path("/upload/2")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    Response uploadViaMultipartForm(@MultipartForm UploadForm uploadForm);

}

  • 实现类

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MultivaluedMap;

import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
import org.springframework.stereotype.Service;


@Service("restEasyUploadService")
public class RestEasyUploadServiceImpl implements RestEasyUploadService {

    @Override
    public Response uploadViaMultipartFormDataInput(MultipartFormDataInput input) {
        Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
        List<InputPart> inputParts = uploadForm.get("uploadFile");
        for (InputPart inputPart : inputParts) {
            try {
                // get file name from headers
                MultivaluedMap<String, String> headers = inputPart.getHeaders();
                // ...

                InputStream inputStream = inputPart.getBody(InputStream.class, null);
                // deal with input stream
                // ...
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Response.success();
    }


    @Override
    public Response uploadViaMultipartForm(UploadForm uploadForm) {
        byte[] data = uploadForm.getData();
        // deal with data
        // ...
        return Response.success();
    }
}

  • UploadForm

import javax.ws.rs.FormParam;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
import lombok.Data;

@Data
public class UploadForm implements Serializable {

    @FormParam("uploadFile")
    @PartType("application/octet-stream")
    private byte[] data;

}

使用rest协议暴露服务

<!-- rest protocol -->
<dubbo:protocol name="rest" server="servlet" port="${server.port}"/>

<!-- rest provider -->
<dubbo:service interface="com.demo.RestEasyUploadService" ref="restEasyUploadService" protocol="rest" version="2.0"/>

前端提交代码

<el-form ref="uploadForm">
    <el-upload ref="upload" action="" :limit="1" :auto-upload="false" :file-list="fileList">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button size="small" type="success" @click="submitUpload">上传</el-button>
    </el-upload>
</el-form>

<script>
    export default {
        data() {
            return {
                fileList: []
            }
        },
        methods: {
            submitUpload() {
                let form = this.$refs['uploadForm'].$el;
                let file = this.$refs.upload.uploadFiles[0].raw;
                // fromData
                let formData = new FormData(form);
                formData.append('uploadFile', file);
                // header
                let config = {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                };
                this.$http.post('http://localhost:7979/resteasy/upload/1', formData, config).then(resp => {
                    if (resp.code === 200) {
                        this.$notify.success('成功!');
                    } else {
                        this.$notify.error(resp.message);
                    }
                })
            }
        }
    }
</script>


扫一扫分享到微信

已有 条评论
写评论