CXF REST/JSON文件上传

使用CXF REST/JSON WebService实现UploadFile接口. 网上搜了一堆(英文),主要是讲用Miltipart的上传,而这个主要是给WebSite使用的,对于WebService,研究了很长时间,下面这个是可以走通的。这里只把实现列出来,原理以后再完善。


  1. REST服务端
  • 接口定义


@Path("/media")
@Produces("application/json")
public interface mediaRest {

@POST
    @Path("/UploadFile")
    @Produces("application/json")
    @Consumes("application/json")
    UploadInfoResponse UploadFile(FileUpload file);

}


  • 接口实现

@Override
    public UploadInfoResponse UploadFile(FileUpload file) {
        UploadInfoResponse res = new UploadInfoResponse();
        DataHandler handler = file.getDataHandler();
        try {
            InputStream is = handler.getInputStream();

            OutputStream os = new FileOutputStream(new File("D:/uploads/"
                + file.getFilename()));
            byte[] b = new byte[100000];
            int bytesRead = 0;
            while ((bytesRead = is.read(b)) != -1) {
                os.write(b, 0, bytesRead);
            }
            os.flush();
            os.close();
            is.close();
            
            res.setResponseCode(ResponseCode.SUCCESS);
            res.setResponseMessage("Success");
            
            Upload ui = new Upload();
            ui.setTotalSize(file.getTotalSize());
            ui.setMediaId(file.getMediaId());
            ui.setStatus(MediaStatus.eStatus.done.ordinal());
            ui.setExpireDate(Util.GetDate(new Date(), Calendar.DAY_OF_MONTH, 90));
            
            res.setUploadInfo(ui);

        } catch (IOException e) {
            res.setResponseCode(ResponseCode.ERROR_INTERNAL_ERROR);
            res.setResponseMessage(e.getMessage());
            //e.printStackTrace();
        }
        
        return res;
    }


  • POJO定义:

@XmlRootElement(name = "FileUpload")
public class FileUpload {
    private int totalSize;
    private int uploadPos;
    private int userId;
    private int mediaId;
    private String filename;
    private DataHandler dataHandler;
    public DataHandler getDataHandler() {
        return dataHandler;
    }
    public void setDataHandler(DataHandler dataHandler) {
        this.dataHandler = dataHandler;
    }
    public int getTotalSize() {
        return totalSize;
    }
    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }
    public int getUploadPos() {
        return uploadPos;
    }
    public void setUploadPos(int uploadPos) {
        this.uploadPos = uploadPos;
    }
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public int getMediaId() {
        return mediaId;
    }
    public void setMediaId(int mediaId) {
        this.mediaId = mediaId;
    }
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }
}


2. 客户端

public class mydemo {

    static WebClient client;
    public static void main(String[] args) {
        
        client = WebClient.create("http://localhost:8080/media/services/rest/");
        client.header("Content-Type", "application/json");

        post();
    }
    
    public static void post()
    {
        String errMsg;
        try
        {
            FileUpload fu = new FileUpload();
            
            
            FileDataSource ds = new FileDataSource("D:\\assets\\2.vs");
            
            fu.setFilename(ds.getFile().getName());
            fu.setMediaId(2);
            fu.setUploadPos(0);
            fu.setUserId(3);
            fu.setTotalSize(ds.getInputStream().available());
            fu.setDataHandler(new DataHandler(ds));
            //client.post(fu);
            client.path("media/UploadFile").accept("application/json").post(fu);

        }
        catch(Exception ex)
        {
            errMsg = ex.getMessage();
        }
        
        MediaQueryOption query = new MediaQueryOption();
        query.setStartPos(0);
        query.setEndPos(100);
        
        client.path("media/MyMedias").post(query);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值