0
我是Spring MVC的新手。文件上傳控制器servlet沒有收到請求
我想通過我的網站爲我的項目創建一個文件上傳。我正在使用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.";
}
}
但是上面的控制器servlet無論如何都沒有收到web請求。 我的網站的代碼是一個簡單的形式是:
<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>
如果我修改上面的控制器servlet這種類型下面我收到我的web請求
@RequestMapping(value = "/uploadFile" , method = RequestMethod.POST)
public @ResponseBody
String uploadFileHandler(HttpServletRequest req) {
爲什麼不是第一個代碼工作我的項目?有什麼不同?如果您需要任何其他信息,我將很樂意提供。
感謝
哇,真棒這工作就像一個魅力! 非常感謝您的幫助。 但爲什麼只有名稱屬性的標籤被髮送到服務器?什麼意義? –
它符合HTML規範:http://www.w3.org/TR/html401/interact/forms.html#h-17.2 – ursa