从servlet访问映像名称并在JSP页面中显示该映像

时间:2016-02-16 07:11:19

标签: jsp servlets image-upload

我正在home.jsp中进行图片上传部分。上传图像后,图像应显示在另一个jsp页面(imageView.jsp)中。图像存储在项目路径内的文件夹中。

针对home.jsp

<form action="UploadServlet" method="post" class=" form" role="form" enctype="multipart/form-data">
            <div id = "head" class="row " >
                <h4 class="text-center textTitle">Upload an Image to be extracted</h4>
            </div>
            <div id =" imageDisplay " class="row">
                <div class=" col-md-6">


                    <span class="btn btn-image  btn-file pull-right">Upload
                        <input type="file" name="image" id="extractimage">

                    </span>
 </div>
<div  id =" imageDisplay " class=" col-md-6">

<input type="submit" class= "btn btn-image pull-left">Preview</a>
</div>
            </div>
        </form>

然后将用户定向到执行图像上传过程的UploadSErvlet.java。 UploadServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();
 Part uploadedImage = request.getPart("image");

        String filename = uploadedImage.getSubmittedFileName();

        out.println(filename);
        InputStream filecontent = uploadedImage.getInputStream();
        imageUpload image = new imageUpload();
        String extnsn = image.getExtensn(filename);
        out.println(extnsn);
        boolean flag = image.imageInputstream(filecontent, extnsn, filename);
        if (flag == true) {
            out.println("<html><body onload=\"alert('Image uploaded successful')\"></body></html>");
        } else {
            out.println("<html><body onload=\"alert('Image not uploaded successful')\"></body></html>");
        }

        //returning name of the image to another jsp page
        request.setAttribute(" name ", filename);
        request.getRequestDispatcher("imageView.jsp").forward(request, response);

    }
}

在获取上传图像的文件名后,应将其传递给imageView.jsp页面,并在div内显示该图像。应将user重定向到imageView.jsp页面

imageView.jsp

 <%
                String fname = (String) request.getAttribute(" name ");
            %>

            <div class="row">
                <div  id = "display" class="col-lg-8 center-block "><img src="C:\Users\User\Documents\GitHub\Linguist\uploadedImage\<%=fname%>" width="300" height="500" alt="image"/>
在网址栏中显示它仍在UploadServlet中。但是imageView.jsp用户界面显示在屏幕上。但图像不会按预期显示在div中。提前谢谢。

0 个答案:

没有答案