0

I have a variable inputfilepath in my controller class and this variable holds the path of file that is uploaded. I want to use the path in my JSP page for some purpose. Please tell me how do I do this. Below is the controller method code:

@RequestMapping(value="/UploadFile", method = RequestMethod.POST)
public @ResponseBody
ModelAndView uploadFileHandler(@RequestParam("file") MultipartFile file) throws IOException, ParseException{
String fileName=file.getOriginalFilename();
byte[] bytes = file.getBytes();
File serverFile = new File(dir.getAbsolutePath()+ File.separator + fileName);
BufferedOutputStream st = new BufferedOutputStream(new FileOutputStream(serverFile));
st.write(bytes);
st.close();
String inputFilePath = serverFile.getAbsolutePath(); \\want to use this variable in uploadfile.jsp

Below is code for jsp page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File upload Demo</title>
</head>
<body>
<form method ="post" action="UploadFile" enctype="multipart/form-data">
File to upload: <input id="file" type="file" name="file" />
<input type="submit" value="upload" />
 //want to add a javascript function here, which uses the value held by inputFilePath variable from controller
</form>
</body>
</html>
  • for your first question, use JSTL Tags – Gholamali Irani Dec 01 '17 at 21:06
  • use jstl tag and take the uploaded file name from server side code then store into any directory location get the current path then store the same path into variable and use the same variable into JSP – Anand Dwivedi Dec 02 '17 at 08:34
  • So my code in controller is like - String inputfilepath = file.getabsolutepath() I want to use inputfilepath in my JSP so how do I use jstl tags here... – Shreya Srinivas Dec 02 '17 at 08:40
  • @g.irani could you pls show me an example code – Shreya Srinivas Dec 02 '17 at 08:42
  • see: [example 1](https://stackoverflow.com/questions/29360959/passing-parameter-from-controller-to-jsp-in-spring) and [example 2](http://forum.spring.io/forum/spring-projects/web/65718-accessing-model-attributes-in-jsp) – Gholamali Irani Dec 02 '17 at 10:11

0 Answers0