0

I am new to Spring MVC.

I am trying to create a file upload through my website for my project. I am using this example.

/**
 * Upload single file using Spring Controller
 */
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody
String uploadFileHandler(@RequestParam("fileN") MultipartFile file) {
 
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
 
            // Creating the directory to store file
            String rootPath = System.getProperty("catalina.home");
            File dir = new File(rootPath + File.separator + "tmpFiles");
            if (!dir.exists())
                dir.mkdirs();
 
            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath()
                    + File.separator + name);
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();
 
            logger.info("Server File Location="
                    + serverFile.getAbsolutePath());
 
            return "You successfully uploaded file=" + name;
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name
                + " because the file was empty.";
    }
}

But the controller servlet above doesn't receive the web request no matter what. My website code is a simple form which is:

<form  id="uploadform" method="POST" enctype="multipart/form-data" action="uploadFile">

<table width="100%">
<tr><td style="width: 17%; height:450px; background: #007dc6" valign="top">
<a href="LandingPage.ftl" style="position: relative; top: 40px; left: 20px; font-size: 15pt; font-weight: BOLD; color: yellow;">Home</a>
<br/>
<a href="bussLandingPage.ftl" style="position: relative; top: 40px; left: 20px; font-size: 14pt; font-weight: BOLD; color: yellow;">Back</a>
<a href="${rc.contextPath}/logout" style="position: relative; top: 40px; left: 20px; font-size: 15pt; font-weight: BOLD; color: yellow;">Logout</a>
</td>

<td align="center" style="background:#e6e6e6">

<div id="mainSection" >


Template download link: <button id="download">Download Template</button>
<br/>
<br/>
Upload new DC setup request: <input type="file" id="fileN"></input>

<button id="submit">Submit</button>

<p color="red">${error}</p>

</div>


</td>
</tr>
</table>


</form>

If I modify the above controller servlet to this below type I receive my web request

@RequestMapping(value = "/uploadFile" , method = RequestMethod.POST)
    public @ResponseBody
    String uploadFileHandler(HttpServletRequest req) {

Why isn't the first code working for my project? what is the difference? If you need any other information I will be happy to provide.

Thanks

Community
  • 1
  • 1

1 Answers1

0

Seem the problem is in HTML:

<input type="file" id="fileN"></input>

As written in this post: "only tags with a name attribute are sent to the server".

Replace wtih:

<input type="file" id="fileN" name="fileN"/>
Community
  • 1
  • 1
ursa
  • 3,680
  • 1
  • 19
  • 36
  • Wow, awesome that worked like a charm!! Thanks a lot for helping. But why only tags with name attribute are sent to the server? What is the significance? – Aditya Kumar Ravikanti Nov 16 '14 at 21:36
  • It works according the HTML spec: http://www.w3.org/TR/html401/interact/forms.html#h-17.2 – ursa Nov 16 '14 at 21:40