简体   繁体   中英

file upload Controller servlet does not receive the request

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

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM