2015-02-24 64 views
0

我试图获取存储在本地文件系统中的映像。在那之后,我需要用JavaScript中的这个图像来挂载一个html。从本地文件系统从javascript获取图像

到目前为止,我得到的图像与servlet和它的实际工作,因为我可以在我的JSP显示图像<img width="120" src="<%=IncVars.getParameter("ruta0")%>myImageServlet">

public class MyImageServlet extends HttpServlet { 

protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 
    //get the file image 
    File image = IncFunctions.getLogoFile(); 

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

    // Init servlet response. 
    response.reset(); 
    response.setContentType(contentType); 
    response.setHeader("Content-Length", String.valueOf(image.length())); 
    // Write image content to response. 
    Files.copy(image.toPath(), response.getOutputStream()); 
} 
//... 
} 

我试图做同样的安装HTML从一个js函数,但我找不到我的js函数来产生这种图像的HTML的方式:

function mountTicket(text, idControl, servletPath){ 
    document.getElementById(idControl).innerHTML = parseTicket(texto, imagePath); 
    window.print(); 
} 

function parseTicket(texto, servletPath){ 

var textHtml = ""; 

//Call the servlet 
$.get(servletPath, function(data) { 
    textoHtml = "<img width='120' src=" + data + "/>"; 
    textoHtml += "<br />"; 
});  

    //.. 


    textoHtml += "<div"; 
    if (estilo != ""){ 
    textoHtml += " style=\"" + estilo + "\""; 
    }   

    textoHtml += ">" + contenido + "</div>"; 

    return textoHtml; 
} 

数据VAR从servlet得到的是字节,看来JS不知道如何解释它。我也试图传递图像的路径,比如说“C:\ myDirectory \ myImage.png”,但正如我所料,它找不到它并显示它。

任何帮助,将不胜感激。提前致谢。

回答

0

答案比我想的要容易。我不需要从我的js代码调用servlet,我只需要将图像添加到servlet路径。 事情是这样的:

textoHtml = "<div><img src='" + servletPath + "'> </div>" ; 

,浏览器将显示图像。

希望它可以帮助有类似“问题”的人

相关问题