GWT doGet() servlet return 图像字节数组或图像到客户端

GWT doGet() servlet return image byte array or image to client

我正在使用 GWT 文件上传小部件上传图像,然后使用 http servlet 将图像导入数据库。

我正在使用 formPanel FormPanel.METHOD_GET 调用我的 servelet doGet() 并且我正在获取图像并将其存储到字节数组中。我不确定如何将其返回给客户?

客户代码

downloadPanel = new FormPanel();
downloadPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
downloadPanel.setMethod(FormPanel.METHOD_GET);
downloadPanel.setAction(GWT.getModuleBaseURL()+"downloadfile" + "?brandID="+ accountIdStr);
downloadPanel.submit();

downloadPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
            @Override
            public void onSubmitComplete(FormPanel.SubmitCompleteEvent submitCompleteEvent) {

                image.setUrl(" http://www.tutorialspoint.com/images/gwt-mini.png");

               // image.setUrl(submitCompleteEvent.getResults());
                Window.alert(submitCompleteEvent.getResults());

            }
        });

服务器代码

public class FileDownload extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");
    UserAccount adminUserAccount = getAdminUserAccount(request);
    int accountId = adminUserAccount.getAccountID();

    byte[] b =  API.GetAccountManager().getCompanyLogo(accountId);

字节数组中有图像。如何将字节数组或图像返回给客户端?

使用响应Writer将响应写回客户端,像这样:

byte[] b =  API.GetAccountManager().getCompanyLogo(accountId);
respose.getOutputStream().write(b);

它会将字节写回客户端,在客户端,您需要解析最有可能来自 InputStream 的字节。