Spring MVC 将上传的图像显示到外部文件夹中
Spring MVC display uploaded image into a External Folder
我正在使用 Spring mvc 创建一个图片上传应用程序。我成功地将图像保存在外部文件夹中。图片保存代码如下:
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);
}
}
图像已成功保存到该外部文件夹中。但是当我试图将相同的图像显示到 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 在 web 上下文之外检索图像,因为 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 文件,如下所示:
<img src="/images/${product.productName}.jpg" alt="image"/>
一整天后,我终于找到了解决方案,它对我有用。
我正在使用 Spring mvc 创建一个图片上传应用程序。我成功地将图像保存在外部文件夹中。图片保存代码如下:
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);
}
}
图像已成功保存到该外部文件夹中。但是当我试图将相同的图像显示到 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 在 web 上下文之外检索图像,因为 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 文件,如下所示:
<img src="/images/${product.productName}.jpg" alt="image"/>
一整天后,我终于找到了解决方案,它对我有用。