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.
I have been searching in google but I cannot find.
This is the code for the model:
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();
}
}
This is the controller:
@Controller
@RequestMapping(value="/FileUploadForm.htm") public class UploadFormController implements HandlerExceptionResolver{
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 && file.length > 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<Object, Object> model = new HashMap<Object, Object>();
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);
}
}
This is the FileUploadForm.jsp page:
<form:form commandName="FORM" enctype="multipart/form-data" method="POST">
<table>
<tr><td colspan="2" style="color: red;"><form:errors path="*" cssStyle="color : red;"/>
${errors}
</td></tr>
<tr>
<td>Choose File:</td>
<td><form:input type="file" name="file" path="file" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Upload File" /></td>
</tr>
</table>
</form:form>
Last, this is the Success.jsp page:
<h3 style="color: green;">File has been uploaded successfully.</h3> <br>
File Name : ${FORM.name}.
</br><a href="...">Download this file here</a>
Any ideas or solutions is greatly appreciated and welcomed.
Best Regards, Yunus