Jsp Spring MVC显示上载到外部文件夹的图像
我正在使用SpringMVC创建一个图像上传应用程序。我成功地将图像保存在外部文件夹中。保存图像的代码如下所示:Jsp Spring MVC显示上载到外部文件夹的图像,jsp,spring-mvc,image-uploading,Jsp,Spring Mvc,Image Uploading,我正在使用SpringMVC创建一个图像上传应用程序。我成功地将图像保存在外部文件夹中。保存图像的代码如下所示: MultipartFile productImage = product.getProductImage(); path= Paths.get("/oms/images/"+product.getProductName()+".jpg"); System.out.println(path.toString()); if(prod
MultipartFile productImage = product.getProductImage();
path= Paths.get("/oms/images/"+product.getProductName()+".jpg");
System.out.println(path.toString());
if(productImage!=null && !productImage.isEmpty()){
try {
productImage.transferTo(new File(path.toString()));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Product Image Saving Failed ",e);
}
}
<img src="/images/${product.productName}.jpg" alt="image"/>
图像已成功保存到该外部文件夹中。但是当我试图将相同的图像显示到jsp文件中时,我最终得到了这个错误
http://localhost:8081/oms/images/Ss.jpg Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8081/oms/images/pup.jpg Failed to load resource: the server responded with a status of 404 (Not Found)
显示图像的代码如下所示
<img src="<c:url value="/oms/images/${product.productName}.jpg"/>"/>
“/>
我看到了更多关于同一问题的链接,但大多数问题都像是将图像保存到不同于webapp的目录中(显然)
我是否遗漏了什么?如何向Spring中添加外部资源?我应该如何将用户添加的上传图像显示到jsp页面?您可以使用servlet在webcontext之外检索图像,因为jsp代码只能在web上下文中工作。 例如
@WebServlet("/FetchFile")
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String filename = request.getParameter("productName");
File file = new File("/oms/images/"+filename);
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
Files.copy(file.toPath(), response.getOutputStream());
}
}
并使用
<img scr="FetchFile?productName=${product.productName}.jpg">
您可以使用servlet在webcontext之外检索图像,因为jsp代码只能在web上下文中工作。 例如
@WebServlet("/FetchFile")
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String filename = request.getParameter("productName");
File file = new File("/oms/images/"+filename);
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
Files.copy(file.toPath(), response.getOutputStream());
}
}
并使用
<img scr="FetchFile?productName=${product.productName}.jpg">
上述解决方案对我不起作用。如果我尝试从jsp页面调用@WebServlet(“/FetchFile”),@WebServlet(“/FetchFile”)甚至没有被调用。我在谷歌上搜索了它,找到了一个解决方案,在spring mvc dispatcher-servlet.xml中,我们可以通过以下方式添加外部资源:
<mvc:resources mapping="/images/**" location="file:///${fullPath}/oms/images/" />
${fullPath}您应该在此处提供完整路径:
然后,从jsp页面可以如下方式调用jsp文件:
MultipartFile productImage = product.getProductImage();
path= Paths.get("/oms/images/"+product.getProductName()+".jpg");
System.out.println(path.toString());
if(productImage!=null && !productImage.isEmpty()){
try {
productImage.transferTo(new File(path.toString()));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Product Image Saving Failed ",e);
}
}
<img src="/images/${product.productName}.jpg" alt="image"/>
一整天之后,我终于得到了解决方案,它对我有效。上述解决方案对我无效。如果我尝试从jsp页面调用@WebServlet(“/FetchFile”),@WebServlet(“/FetchFile”)甚至没有被调用。我在谷歌上搜索了一下,找到了一个解决方案,在spring mvc dispatcher-servlet.xml中,我们可以通过以下方式添加外部资源:
<mvc:resources mapping="/images/**" location="file:///${fullPath}/oms/images/" />
${fullPath}您应该在此处提供完整路径:
然后,从jsp页面可以如下方式调用jsp文件:
MultipartFile productImage = product.getProductImage();
path= Paths.get("/oms/images/"+product.getProductName()+".jpg");
System.out.println(path.toString());
if(productImage!=null && !productImage.isEmpty()){
try {
productImage.transferTo(new File(path.toString()));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Product Image Saving Failed ",e);
}
}
<img src="/images/${product.productName}.jpg" alt="image"/>
一整天之后,我终于得到了解决方案,它对我起了作用。你是说我应该将此方法添加到spring controller,然后从jsp页面调用此方法?是的!这将是Nature的ajax调用我做了…我复制了类文件,并试图通过…访问它,但对我不起作用…我还放了syso在该类中,该文件看起来也没有被调用…怎么了?我也在使用tomcat 7和tomcat 8…结果相同。@WebServlet(“/FetchFile”)会有什么问题没有被调用。我在浏览器控制台中没有收到任何错误。你能检查一下吗?谢谢@Rizstien..这对我来说终于起作用了…很抱歉,出现了一些问题,可能是我无法查看你的完整评论。问题是servlet映射技术,可能是它不起作用您的配置。想法是制作一个servlet并从中下载资源。您的意思是我应该将此方法添加到spring controller,然后从jsp页面调用此方法?是的!这将是Nature的ajax调用我做了…我复制了类文件,并试图通过…访问它,但不适用于我。。。我把syso也放在那个类文件中,看起来那个文件也没有被调用…怎么了?我也在使用tomcat 7和tomcat 8…结果相同。@WebServlet(“/FetchFile”)会有什么问题没有被调用。我在浏览器控制台中没有收到任何错误。你能检查一下吗?谢谢@Rizstien..这对我来说终于起作用了…很抱歉,出现了一些问题,可能是我无法查看你的完整评论。问题是servlet映射技术,可能是它不起作用我们的想法是制作一个servlet并从中下载资源