我正在使用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);
字节数组中包含图像。如何将字节数组或图像返回给客户端?
答案 0 :(得分:1)
使用响应Writer
将响应写回到客户端,如下所示:
byte[] b = API.GetAccountManager().getCompanyLogo(accountId);
respose.getOutputStream().write(b);
它将把字节写回到客户端,在客户端,您将需要解析最有可能来自InputStream
的字节。