Java文件上传问题
Servlet JSPJava文件上传问题,java,html,forms,servlets,file-upload,Java,Html,Forms,Servlets,File Upload,Servlet JSP 上传 选择要上载的文件 $(“.jUiButton”).button() 布尔和输入总是验证为false/null,我不知道为什么。遵循本指南: 在web inf/lib中,我们有commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar 有什么想法吗?您实际上并没有提交表单。您正在导航到带有GET请求的页面 将“提交”锚替换为提交按钮: <form enctype="multipart/form-
上传
选择要上载的文件
$(“.jUiButton”).button()
布尔和输入总是验证为false/null,我不知道为什么。遵循本指南:
在web inf/lib中,我们有commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar
有什么想法吗?您实际上并没有提交表单。您正在导航到带有
GET
请求的页面
将“提交”锚替换为提交按钮:
<form enctype="multipart/form-data" method="post" action="apply">
<fieldset>
<br/>
<legend>Upload</legend>
<br/>
<label>Select file to upload</label>
<input type="file" name="file" /><br />
<br/>
<a href="apply" class="jUiButton">Submit</a>
</fieldset>
</form>
<script>$(".jUiButton").button()</script>
提交
您可以保留
,但是您必须使用JavaScript手动提交表单。您不应该事先阅读HttpServletRequest#getInputStream()
。它只能读取一次。如果您事先已经阅读了Commons FileUpload,则它将无法再阅读。删除servlet中的所有行,直到ServletFileUpload#isMultipartContent()
line。您所遵循的指南已过时(2008年)。如果这是一个新项目,您可能希望从基于注释的方法开始。或许最好遵循(2010年)。然后,文件上载控制器将如下所示:
<button type="submit" class="jUiButton">Submit</button>
这只是控制器,您需要添加适当的Spring配置。如果你想走这条路,我可以进一步帮助你。JavaScript改变了这条路。他的servlet的
doPost()
和on还被调用了吗?@BalusC:因为他的doGet()
调用了doPost()
。该博客的介绍性文字是剽窃的。谢谢你提到这个链接。
<button type="submit" class="jUiButton">Submit</button>
@Controller
public class FileUploadContoller {
@RequestMapping(value = "/fileupload", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public String ingest(@RequestParam("file") final MultipartFile file) throws Exception {
if (file.isEmpty()) {
System.out.println("empty");
} else {
System.out.println("not empty");
}
// do something with file.getBytes()
return("ok");
}
}