Java Spring MVC未解析存储在webroot外部的映像路径
我正在上传图像和视频,并为视频和音频创建相应的标签,以显示在查看页面上,但不知何故,图像和视频路径并没有得到解决 这是我的控制器Java Spring MVC未解析存储在webroot外部的映像路径,java,spring,spring-mvc,file-upload,Java,Spring,Spring Mvc,File Upload,我正在上传图像和视频,并为视频和音频创建相应的标签,以显示在查看页面上,但不知何故,图像和视频路径并没有得到解决 这是我的控制器 @RequestMapping(value = "/contentUpload", headers = "content-type=multipart/*", method = RequestMethod.POST) public @ResponseBody String uploadImage(@RequestParam("fileData") MultipartF
@RequestMapping(value = "/contentUpload", headers = "content-type=multipart/*", method = RequestMethod.POST)
public @ResponseBody
String uploadImage(@RequestParam("fileData") MultipartFile multipartFile, HttpServletRequest request )
{
String jsonResponse = null;
boolean isImage = false;
boolean isVideo = false;
try
{
String path = request.getServletContext().getRealPath("/");
File directory = null;
if (multipartFile.getContentType().contains("image"))
{
directory = new File(path + "/uploads/images/");
isImage = true;
}
else
{
directory = new File(path + "/uploads/videos/");
isVideo = true;
}
byte[] bytes = null;
File file = null;
bytes = multipartFile.getBytes();
if (!directory.exists()) directory.mkdirs();
file = new File(directory.getAbsolutePath() + System.getProperty("file.separator") + multipartFile.getOriginalFilename());
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
stream.write(bytes);
stream.close();
if (isImage)
jsonResponse = "<br /><img src=\"" + file.getAbsolutePath() + "\" />";
else if (isVideo)
jsonResponse = "<video>" + "<source src=\"" + file.getAbsolutePath() + "\" type=\"" + multipartFile.getContentType() + "\">" + "</video>";
}
catch (Exception e)
{
}
return jsonResponse;
}
请建议我所需的更改。'//'三重斜杠解决了我的问题。现在我的资源映射是这样的
<mvc:resources mapping="/uploads/**" location="file:///home/govi/uploads/" />
任何面临此问题的人,请浏览JB Nizet的评论,并浏览此链接使用下面的代码行,它可能会对您有所帮助
<mvc:resources mapping="/uploads/**" location="/WEB-INF/projectImg/uploads/" />
永远不要尝试在webapp目录下存储任何内容。顺便说一句,如果部署war文件,该目录将根本不存在。上传的图像是数据。它们不是应用程序的一部分。它们属于数据库或应用程序外部的数据目录。您不希望所有这些数据在重新部署应用程序时永远丢失。那么,我应该将内容存储在哪里?如果可能的话,请给我举一些例子。我是网络开发新手。为文件系统中的图像创建一个空的、专用的目录,并将图像存储在那里<代码> /home /GOVI/上载的图像。就像JB Nizet建议的那样,根据您的场景,如果有许多视频和图像而不是保存在数据文件夹上并保存在服务器上,我会考虑使用存储服务并保存数据并从Web访问它,这对最后一种方法有很多好处。
<mvc:resources mapping="/uploads/**" location="file:///home/govi/uploads/" />
<mvc:resources mapping="/uploads/**" location="/WEB-INF/projectImg/uploads/" />