2012-09-29 119 views
2

我有点有以下情形丢失:为什么图像不显示JSP页面上

  1. 用户上传的图像 - upload.jsp(多/表单数据)
  2. 的servlet做所有肮脏的工作(保存图像,得到名称,保存的名称,在display.jsp重定向到display.jsp)
  3. ,只是上传的图片应提交

不幸的是,display.jsp页面是空的。当我在firefox下查看源代码页面时,一切似乎都很顺利,提供了有效的图像链接。

<img src="/UploadTest/avatar/55_445194458350473498.png" border=0 width="48px" height="48px"/> 

但在媒体的信息,我可以看到一些奇怪的相关信息:

Location: http://localhost:8084/UploadTest/avatar/55_445194458350473498.png 
Type:  text/html 
Size:  Unknown (not cached) 
Dimensions: 0px x 0px (scaled to 0px x 16px) 

这是用于上传,处理和显示图片代码:

upload.jsp

<form action="Upload" method="post" enctype="multipart/form-data"> 
    <label for="file">File:</label> 
    <input type="file" id="file" name="file"> 
    <input type="submit" value="submit"> 
</form> 

Upload.java

(该MultipartMap的servlet属于BalusC,http://balusc.blogspot.com.au/2009/12/uploading-files-in-servlet-30.html

package test; 

import java.io.File; 
import java.io.IOException; 
import java.util.Arrays; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.MultipartConfig; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpSession; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import test.MultipartMap; 


@WebServlet(urlPatterns = { "/Users/Thomas/NetBeansProjects/UploadTest/web/Upload" }) 
@MultipartConfig(location = "/Users/Thomas/NetBeansProjects/UploadTest/web/avatar", maxFileSize = 10485760L) // 10MB. 
public class UploadServlet extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    } 

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

    MultipartMap map = new MultipartMap(request, this); 

    File file = map.getFile("file"); 

    String filename = file.getName(); 

    HttpSession session = request.getSession(); 
    session.setAttribute("filename", filename); 

    request.getRequestDispatcher("/display.jsp").forward(request, response); 
    } 
} 

display.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
    </head> 
    <body> 
     <div> 
      <img src="${pageContext.request.contextPath}/avatar/${filename}" border=0 width="48px" height="48px"/> 
     <div> 
    </body> 
</html> 

如果我更换,在display.jsp,$ {文件名}静态名之前上传的特定图像,显示没有问题,所以我想图像正确处理只是在前端丢失了一些东西?顺便说一下:当调试器处于活动状态时,一切正常,但是当关闭时问题又回来了。

干杯,

托马斯

+0

使用''。无需采用contextpath – Parth

+0

在您的注释中,您有location =“/ Users/Thomas/NetBeansProjects/UploadTest/web/avatar”。但在你的链接中,你有src =“/ UploadTest/avatar/55_445194458350473498.png”。这是一个错字吗? Web文件夹中的头像文件夹? – rickz

+0

@rickz是的,头像文件夹位于Web文件夹中。 –

回答

-1

您可以查看在PIC观众上传的图片?

文件大小是否正确,数据是否损坏?

您的服务器是否在端口8084上运行?

我想知道为什么你得到类型文本/ HTML时,它应该是图像/ PNG。

+0

The images before upload and after upload are perfectly correct. I can see them in the browser but only if real name is given not ${filename} variable. –

+0

What debugger do you use? Do you use Apache Tomcat? –

+0

@MagnusStrand: is it your answer? I only see some comments here – Parth

0

很好的解释。我经历你的问题并创建它的样本。我面临同样的问题。 解决方法是把下面的行display.jsp文件:

<%@page isELIgnored="false" %> 

我想用EL和页面的问题是不能够正确评估。 以下是代码: upload.jsp和upload.java与您的相同。

显示。JSP:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@page isELIgnored="false" %> 
<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
    </head> 
    <body> 
     <div> 
      <img src="${pageContext.request.contextPath}/images/${filename}" border=0 width="48px" height="48px" alt="Image Not found"/> 
     <div> 
    </body> 
</html> 

希望这会为你工作也

谢谢

+0

Thank you bud, but problem is somewhere else. I changed a bit upload.jsp to be sure but result is the same. In the upload.jsp added 2 lines: String filename1 = file.getName(); String filename2 = "55_4518625602858925634.png"; in Display.jsp: 显示$ {filename2}但不显示$ {filemame1 –