java - GWT doGet() servlet 将图像字节数组或图像返回给客户端

标签 java gwt

我使用 GWT 文件上传小部件来上传图像,然后使用 http servlet 将图像获取到数据库中。

我正在使用 formPanel FormPanel.METHOD_GET 调用我的servlet 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 的字节。

关于java - GWT doGet() servlet 将图像字节数组或图像返回给客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54013727/

相关文章:

gwt - 可排序 CellTable 列的标题没有响应

javascript - GWT 在内容可编辑的 div 上设置插入符号位置

java - 使用 hough 进行瞳孔检测的最佳参数? java opencv

java - 自动捕获 BlazeDS 远程方法抛出的所有异常?

java - java中如何获取上一个 session id

java - 上下移动项目

java - GWT - 服务器端 - 如何使用 eclipse 将外部库添加到 war 文件

java - AutoValue 的 GWT 序列化异常

java - 如何使我的 FXML 文件显示在窗口上?

java - Eclipse:有没有快捷键实现 "Fill method arguments and show guessed arguments"的功能