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/

相关文章:

java - 如何迭代列出的元素数组,每次完成一组操作

google-app-engine - 使用 GAE 进行开发 - 我对 Google 的依赖程度如何?

php - 使用 GWT 从 url 获取内容

Eclipse Tomcat 资源可用列表不包含 Gwt 2.7.0 maven 应用程序

java - 命中 javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated even imported cert

java - 如何在数组中保存运行计数?

java - 从 Java 调用 GWT RPC 服务,RPC 策略问题

java - SmartGWT 替代品

java - 即使类存在于 jar 文件中也会抛出 java.lang.ClassNotFoundException

java - 除了日志级别之外如何过滤log4j日志?