【问题标题】:How to format the response from the servlet, so the dojo does not return an exception如何格式化来自 servlet 的响应,使 dojo 不返回异常
【发布时间】:2019-01-28 13:02:50
【问题描述】:

根据 dojo 教程和这里的几个示例,我正在尝试发送多个多文件帮助。 servlet的文件会到达目录,但是dojo会返回异常

我使用 dojo 1.10 和 javax.servlet.http.HttpServlet v3.0.1

        PrintWriter out = response.getWriter();
        try {

            if (ServletFileUpload.isMultipartContent(request)) {
                try {
                    @SuppressWarnings("unchecked")
                    List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                    for (FileItem item : multiparts) {
                        if (!item.isFormField()) {
                            String name = new File(item.getName()).getName();
                            item.write(new File("/tmp/eshop/" + File.separator + name));
                        }
                    }
                    out.print("[{uploadresult:'Upload is ok!'}]");

                } catch (Exception ex) {
                    out.print("{uploadresult: 'File upload failed due to : '" + ex+"}");
                }
            } else {
                out.print("{uploadresult:'Sorry this servlet only handles file upload request.'}");

            }
             out.close();


        } catch (Exception e) {
            logger.error(e);
        }

抛出错误:

/dojo/v1.10/dojox/form/uploader/_HTML5.js:80 解析服务器错误 结果:SyntaxError:位置 2 处 JSON 中的意外标记 u 在 JSON.parse() 在 Object.eval (/dojo/v1.10/dojox/form/uploader/_HTML5.js:76) 在 XMLHttpRequest。 (dojo.js: 15) (匿名) @ /dojo/v1.10/dojox/form/uploader/_HTML5.js:80 /dojo/v1.10/dojox/form/uploader/_HTML5.js:81 [{uploadresult: '上传 没问题!'}]

【问题讨论】:

    标签: exception dojo return


    【解决方案1】:

    首先,设置响应为json格式

    首先添加response.setContentType("application/json");

    您必须通过在键中添加引号来正确格式化 json,例如:

    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {
    
        if (ServletFileUpload.isMultipartContent(request)) {
            try {
                @SuppressWarnings("unchecked")
                List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                for (FileItem item : multiparts) {
                    if (!item.isFormField()) {
                        String name = new File(item.getName()).getName();
                        item.write(new File("/tmp/eshop/" + File.separator + name));
                    }
                }
                out.print("[{'uploadresult':'Upload is ok!'}]");
    
            } catch (Exception ex) {
                out.print("{'uploadresult': 'File upload failed due to : '" + ex+"}");
            }
        } else {
            out.print("{'uploadresult':'Sorry this servlet only handles file upload request.'}");
    
        }
         out.close();
    
    
    } catch (Exception e) {
        logger.error(e);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-05
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多