特殊的输出

1.输出文件或图片

File file = new File("F:fzxm1.jpg");

hoContext.httpResponse.resFilePrint(file,0,file.length());

2.bufferedImage输出

BufferedImage image = createImage();

hoContext.httpResponse.printImage((BufferedImage)image);

3.字节输出

byte[] a= null; hoContext.httpResponse.printBytes(a);

4.长连接输出

前后台建立连接后保持连接,可双向收发消息,用来做后台长时间任务处理时向前端发送任务完成进度消息(前端实现进度条显示),即时通信等,使用方式如下

1)创建模块和项目(参照2.3.2 JAVA高阶API开发方法)

2)模块类形参设置为WsContext()

3)WsContext定义如下表所示

datas成员变量/map结构前端传递的数据
DataActions成员变量数据库操作对象
resPrint(String res)成员方法用来输出数据到浏览器
isActive()成员方法用来判断长连接是否保持(返回boolean类型;true:是、false:否)
用例

package dynamic;
import microbee.http.apps.dynamic.WsContext;
import microbee.http.annotation.Controller;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

@Controller
public class Testws {
    public void test (WsContext wsContext){
        // 接收参数
        String name = (String)wsContext.datas.get("name");
        // 长连接返回
        wsContext.resPrint("1");
        try {
            // 5秒
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        wsContext.resPrint("2");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        wsContext.resPrint("3");
        // 监听页面是否保持长连接,若已断开,为防止资源浪费直接return
        if (!wsContext.isActive()) {
            return;
        }
    }
}

4)API测试方法:浏览器访问/wc.html进行测试

5)特别说明:该方法实现的接口不能通过http协议访问,只能通过wc协议访问,且定义url不能与http接口冲突。为避免资源浪费,请使用wsContext中的内部方法isActive来进行判断是否进行return操作。

 

 

 

本文档来自—microbee-http 丨 软件开发框架平台