I am uploading images and videos and creating respective tags for video and audio to display on view page but somehow image and video path are not getting resolved.
Here is my controller.
@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;
}
I have tried resources settings in dispatcher.
<mvc:resources mapping="/uploads/**" location="/#{servletContext.contextPath}/uploads/" />
uploaded image path.
/home/govi/demo/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/projectImg/uploads/images/batman.jpg
please suggest me the changes required.