在将jquery.ajax POST到Servlet之后,浏览器未加载HTML响应 [英] Browser not loading HTML response after doing jquery.ajax POST to a servlet

查看:92
本文介绍了在将jquery.ajax POST到Servlet之后,浏览器未加载HTML响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行多个文件的jquery.ajax发布后,我无法加载servlet返回的某些html.

I am having trouble loading some html that is returned by a servlet after doing a jquery.ajax post of mutliple files.

我可以上载文件(图片).我有一个Servlet,它对照片进行一些处理并从照片中提取一些元数据.处理完照片后,我想将一些提取的元数据传递到JSP上,最终将其返回给浏览器.

I can upload the files (pictures) just fine. I have a servlet that does some processing of the photos and extracts some metadata from the pictures. After processing the photos I want to pass some of the extracted metadata on to a JSP to eventually be returned to the browser.

所有这些都很好.我用数据加载了请求对象,并将其重定向到我的JSP.但是...每当JSP对浏览器作出响应时,都不会加载该页面.

All of this works fine. I load up the request object with my data and I redirect it to my JSP. However... whenever the JSP responds to the browser, the page doesn't get loaded.

使用firebug,我可以看到JSP可以按照我想要的方式正确生成HTML,并且浏览器在响应中获取html文本...只是不加载页面.该页面保持原样,我可以在Firebug的响应中简单地查看HTML文本.

Using firebug I can see that the JSP properly generates the HTML the way I want it to, and the browser gets the html text in the response... it just doesn't load the page. The page stays where it was and I can simply view the HTML text in the response in Firebug.

任何想法可能是什么问题?这是一些相关代码...

Any ideas what might be the problem? Here is some of the relevant code...

将文件上传到servlet的Javascript函数...

Javascript function which uploads the files to the servlet...

function uploadPictures() {
    var input = document.getElementById('filesToUpload');
    var fileList = [];
    var files = new FormData();

    for(var i = 0; i < input.files.length; i++) {       
        files.append("file", input.files[i]);
    }

    $.ajax({
        type        :   "POST",
        url         :   "/uploadPhotos",
        data        :   files,
        dataType    :   "HTML",
        processData :   false,
        contentType :   false
    });
}

Servlet代码

@MultipartConfig
public class UploadPhotosServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String url = "/uploadForm.jsp";
        RequestDispatcher dispatcher = 
            getServletContext().getRequestDispatcher(url);
        String basePath = getInitParameter("photosRoot");
        HashMap<String,String> files = new HashMap<String,String>();

        java.util.Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            String filename = getFilename(part);
            InputStream fileContent = part.getInputStream();

            OutputStream outFile = new FileOutputStream(new File(basePath + "screen/" + filename));
            int read = 0;
            byte[] bytes = new byte[1024];

            while((read = fileContent.read(bytes)) != -1) {
                outFile.write(bytes, 0, read);
            }

            fileContent.close();
            outFile.flush();
            outFile.close();

            File img = new File(basePath + "screen/" + filename);
            File thumb = new File(basePath + "screen/thumbnails/" + filename);

            try {
                Metadata metadata = ImageMetadataReader.readMetadata(img);

                for(Directory directory : metadata.getDirectories()) {
                    if(directory.containsTag(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)) {
                        Date date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        files.put(filename, sdf.format(date));
                        break;
                    } else if(directory.containsTag(ExifIFD0Directory.TAG_DATETIME)) {
                        Date date = directory.getDate(ExifIFD0Directory.TAG_DATETIME);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        files.put(filename, sdf.format(date));
                        break;
                    }
                }

                BufferedImage buffImg = ImageIO.read(img);
                BufferedImage buffThumb = Scalr.resize(buffImg, 150);

                ImageIO.write(buffThumb, "jpg", thumb);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ImageProcessingException e) {
                e.printStackTrace();
            }
        }

        request.setAttribute("files", files);
        dispatcher.forward(request, response);
    }

    private static String getFilename(Part part) {
        for (String cd : part.getHeader("content-disposition").split(";")) {
            if (cd.trim().startsWith("filename")) {
                String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
                return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
            }
        }
        return null;
    }
}

如上所述,重定向到(uploadForm.jsp)的JSP生成得很好. Firebug会在从对Servlet的调用的响应中向我显示HTML代码...但是,浏览器停留在上载表单的页面上.

As I stated above, the JSP that the redirects to (uploadForm.jsp) is generated just fine. Firebug will show me the HTML code in the response from the call to the servlet... However, the browser stays on the page that the upload form is on.

感谢您的帮助.

推荐答案

看起来您真正想要的东西比您目前正在做的事情要简单得多.

It looks like what you really want is much simpler than what you're currently doing.

<form action="/uploadPhotos" method="post" enctype="multipart/form-data">
    <input type="file" multiple name="filesToUpload" />
    <button type="submit">Upload</button>
</form>

这篇关于在将jquery.ajax POST到Servlet之后,浏览器未加载HTML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆