2012-09-29 120 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 –