Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make download function in spring mvc?
    primarykey
    data
    text
    <p>I have made an upload function and it runs well. It stores the uploaded file to "C:/apache-tomcat-6.0.36/webapps/Upload/" But, now I want to make this app be able to download the file.</p> <p>I have been searching in google but I cannot find.</p> <p>This is the code for the model:</p> <pre><code>public class UploadForm { private String name = null; private CommonsMultipartFile file = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public CommonsMultipartFile getFile() { return file; } public void setFile(CommonsMultipartFile file) { this.file = file; this.name = file.getOriginalFilename(); } </code></pre> <p>}</p> <p>This is the controller:</p> <pre><code>@Controller </code></pre> <p>@RequestMapping(value="/FileUploadForm.htm") public class UploadFormController implements HandlerExceptionResolver{</p> <pre><code>private String filePath = "C:/apache-tomcat-6.0.36/webapps/Upload/"; @RequestMapping(method=RequestMethod.GET) public String showForm(ModelMap model){ UploadForm form = new UploadForm(); model.addAttribute("FORM", form); return "FileUploadForm"; } @RequestMapping(method=RequestMethod.POST) public String processForm(@ModelAttribute(value="FORM") UploadForm form, HttpServletRequest request,@RequestParam CommonsMultipartFile[] file) throws Exception { System.out.println(filePath); if(file != null &amp;&amp; file.length &gt; 0) { for(CommonsMultipartFile aFiles : file) { if(!aFiles.getOriginalFilename().equals("")) { aFiles.transferTo(new File(filePath + aFiles.getOriginalFilename())); } } return "success"; }else{ return "FileUploadForm"; } } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception exception) { Map&lt;Object, Object&gt; model = new HashMap&lt;Object, Object&gt;(); if (exception instanceof MaxUploadSizeExceededException) { model.put("errors", "File size should be less then "+((MaxUploadSizeExceededException)exception).getMaxUploadSize()+" byte."); } else { model.put("errors", "Unexpected error: " + exception.getMessage()); } model.put("FORM", new UploadForm()); return new ModelAndView("/FileUploadForm", (Map) model); } </code></pre> <p>}</p> <p>This is the FileUploadForm.jsp page:</p> <pre><code> &lt;form:form commandName="FORM" enctype="multipart/form-data" method="POST"&gt; &lt;table&gt; &lt;tr&gt;&lt;td colspan="2" style="color: red;"&gt;&lt;form:errors path="*" cssStyle="color : red;"/&gt; ${errors} &lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Choose File:&lt;/td&gt; &lt;td&gt;&lt;form:input type="file" name="file" path="file" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;input type="submit" value="Upload File" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form:form&gt; </code></pre> <p>Last, this is the Success.jsp page:</p> <pre><code> &lt;h3 style="color: green;"&gt;File has been uploaded successfully.&lt;/h3&gt; &lt;br&gt; File Name : ${FORM.name}. &lt;/br&gt;&lt;a href="..."&gt;Download this file here&lt;/a&gt; </code></pre> <p>Any ideas or solutions is greatly appreciated and welcomed.</p> <p>Best Regards, Yunus</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload