2013-10-04 35 views
1

外服務器目錄這個問題是延續我剛纔的問題Accessing External Files Into Our Web Application,其實我使用Struts標籤<html:file property="file" />如何顯示的圖像是在strtus

上傳文件,但現在我想表明從該位置上傳的圖片,但我得到的src位置爲http://localhost:9443/D:/resources/images/img1.jpg,這不是該圖像的有效路徑。

如何訪問我的服務器目錄之外的圖像。

這是怎麼了發送Ajax響應與絕對路徑圖像

public ActionForward getAjaxUploadedFiles(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
    { 

     String imagePath = "D:/resources/images/"; 
     ArrayList<String> path = new ArrayList<String>(); 

     File imageFile = new File(imagePath); 
     File imageFiles[] = imageFile.listFiles(); 

     for (int i = 0; i < imageFiles.length; i++) { 
      path.add(imageFiles[i].getAbsolutePath()); 
     } 

     PrintWriter out = response.getWriter(); 
     response.setContentType("text/xml"); 
     response.setHeader("Cache-Control", "no-cache"); 
     response.setStatus(HttpServletResponse.SC_OK); 

     StringBuffer strXMl = new StringBuffer(); 
     strXMl.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
     strXMl.append("<start>"); 


     for (String imagePth : path) { 
      strXMl.append("<imagePath>"); 
      strXMl.append(imagePth); 
      strXMl.append("</imagePath>"); 
     } 

     strXMl.append("</start>"); 

     if(strXMl != null){ 
      String Xml = strXMl.toString(); 
      out.write(Xml); 
      System.err.println("XMl Reponse is: " + Xml); 
     } 
     else { 
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 
     } 
     out.flush(); 

     return mapping.findForward(null); 
    } 

這是我如何在JSP

$(response).find("imagePath").each(function() { 
      row = tblReportList.insertRow(0); 
      row.className="TableBordergray"; 
      row.style.width="100%"; 

      var imagePath = $(this).text(); 

      cell = row.insertCell(0); 
      cell.innerHTML="<img src='" + imagePath + "' alt='" + imagePath + "' height='42' width='42'>"; 
     }); 

渲染圖像,但在img標籤我得到的圖像路徑爲http://localhost:9443/D:/resources/images/img1.jpg

+0

什麼是src位​​置? –

+0

感謝您的快速回復src的位置是D:/ resources/images /即在服務器目錄之外。 – Vishrant

+0

發表評論的代碼,你得到這個。 –

回答

1

您無法以這種方式呈現圖像。 Web服務器將您的圖像路徑視爲相對的,並在服務器上添加合格的URL位置。您應該創建一個動作以服務圖像,例如

<action path="/image" ... scope="request" validate="false"/> 

然後,像

cell.innerHTML="<img src='" + '/image?path=' + imagePath + "' alt='" + imagePath + "' height='42' width='42'>"; 

渲染HTML現在,創建寫二進制圖像數據的響應輸出流的作用。在允許您查找二進制輸出文件的操作中採用參數path。沖洗輸出後返回null,所以struts不應該進一步轉發該動作。您還可以添加標題以關閉Cache-Control以確保從服務器檢索圖像。

0

你好下面是回答我的問題,我已經爲顯示圖像創建ImageServlet,步驟執行:

1.你需要添加在web.xml文件映射:

<servlet-name>ImageServlet</servlet-name> 
    <url-pattern>/ImageServlet/*</url-pattern> 

2.創建ImageServlet

public class ImageServlet extends HttpServlet { 

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

     //Setting image path 
     ImageLocationService locationService = new ImageLocationService(); 

     try { 
      String imageCategory = request.getParameter("imageCategory"); 
      if (imageCategory != null) { 
       this.imagePath = locationService.getImageLocation(imageCategory); 
      }else{ 
       this.imagePath = ConfigConstants.imageLocation; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // Get requested image by path info. 
     String requestedImage = request.getPathInfo(); 

     // Check if file name is actually supplied to the request URI. 
     if (requestedImage == null) { 
      // Do your thing if the image is not supplied to the request URI. 
      // Throw an exception, or send 404, or show default/warning image, or just ignore it. 
      response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. 
      return; 
     } 

     // Decode the file name (might contain spaces and on) and prepare file object. 
     File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8")); 

     // Check if file actually exists in filesystem. 
     if (!image.exists()) { 
      // Do your thing if the file appears to be non-existing. 
      // Throw an exception, or send 404, or show default/warning image, or just ignore it. 
      response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. 
      return; 
     } 

     // Get content type by filename. 
     String contentType = getServletContext().getMimeType(image.getName()); 

     // Check if file is actually an image (avoid download of other files by hackers!). 
     // For all content types, see: http://www.w3schools.com/media/media_mimeref.asp 
     if (contentType == null || !contentType.startsWith("image")) { 
      // Do your thing if the file appears not being a real image. 
      // Throw an exception, or send 404, or show default/warning image, or just ignore it. 
      response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. 
      return; 
     } 

     // Init servlet response. 
     response.reset(); 
     response.setBufferSize(DEFAULT_BUFFER_SIZE); 
     response.setContentType(contentType); 
     response.setHeader("Content-Length", String.valueOf(image.length())); 
     response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\""); 

     // Prepare streams. 
     BufferedInputStream input = null; 
     BufferedOutputStream output = null; 

     try { 
      // Open streams. 
      input = new BufferedInputStream(new FileInputStream(image), DEFAULT_BUFFER_SIZE); 
      output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE); 

      // Write file contents to response. 
      byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 
      int length; 
      while ((length = input.read(buffer)) > 0) { 
       output.write(buffer, 0, length); 
      } 
     } finally { 
      // Gently close streams. 
      close(output); 
      close(input); 
     } 
    } 

    private static void close(Closeable resource) { 
     if (resource != null) { 
      try { 
       resource.close(); 
      } catch (IOException e) { 
       // Do your thing with the exception. Print it, log it or mail it. 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

3.在JSP方面,你需要添加映射在步驟1中的img標籤即輸入類型=「形象」:

<input type="image" alt='No image found' src='../ImageServlet/append image name that you want to display' /> 

你甚至可以創建Action類,並使用execute方法做相同。