我正在上传图片和视频,并为视频和音频创建相应的标签,以便在视图页面上显示,但不知何故图片和视频路径无法解析。
这是我的控制器。
@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="/#{servletContext.contextPath}/uploads/" />
上传的图片路径。
/home/govi/demo/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/projectImg/uploads/images/batman.jpg
请告诉我所需的更改。
答案 0 :(得分:1)
使用下面的代码行,它可能对您有帮助。
<mvc:resources mapping="/uploads/**" location="/WEB-INF/projectImg/uploads/" />
答案 1 :(得分:0)
&#39; ///&#39;三重斜线解决了我的问题。现在我的资源映射是这样的。
<mvc:resources mapping="/uploads/**" location="file:///home/govi/uploads/" />
任何人如果遇到这个问题,请查看JB Nizet的评论,并通过此链接How to retrieve and display images from a database in a JSP page?