我使用 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/