gpt4 book ai didi

java - 使用 Spring MVC 上传文件

转载 作者:太空宇宙 更新时间:2023-11-04 07:46:07 25 4
gpt4 key购买 nike

更新:我观察到传递空值的原因是 enctype="multipart/form-data" ,但是需要该部分才能将文件传递到 Controller 。下面的问题仍然存在。

<小时/>

我一直在尝试设置一个 Spring MVC 页面,允许用户上传文档。我环顾四周,似乎对应该如何实现这个有一个总体的想法(使用“commons-fileupload”和“commons-io”),所以以下是我的 View 、 Controller 和模型。

查看:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page session="false" %>
<html>
<head>
<title>Upload Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

</script>
<script type="text/javascript" src="resources/rulesextractor.js"></script>
<link rel="stylesheet" type="text/css" href="./resources/css/rulesextractor.css" />
</head>
<body>
<c:import url = "./header.jsp">
<c:param name = "title" value = "Upload Document"/>
<c:param name = "region" value = "${region}"/>
</c:import>
<br>
Please choose a Document to upload
<br>

<form:form method="POST" modelAttribute="uploadForm" commandName="uploadForm" name="uploadForm" id="uploadForm" enctype="multipart/form-data">
<form:input path="fileData" name="fileData" type="file"/>
<br>
<input type="submit" name="submit" value="Submit" id="subbtn"/>
<input type="button" value="Main Menu" onclick="back();"/>
</form:form>

</body>
</html>

Controller :

@Controller
public class UploadController extends BaseController {
private static final Logger logger = LoggerFactory.getLogger(FlowController.class);

//@Autowired
//private UserPreferences userPreferences;

@Autowired
GenericSearchBO genericSearchBO;

@RequestMapping(value = "upload", method = RequestMethod.GET)
public String getUploadPage(Model m){
logger.info("UploadController: Getting Upload page");
updateRegionOnModel(m);
m.addAttribute(new UploadForm());
return "/pages/upload";
}
@RequestMapping(method = RequestMethod.POST)
public String create(@Valid UploadForm UploadForm, BindingResult result){
if (result.hasErrors())
{
for(ObjectError error : result.getAllErrors())
{
System.err.println("Error: " + error.getCode() + " - " + error.getDefaultMessage());
}
return "/pages/upload";
}

// Some type of file processing...
System.err.println("-------------------------------------------");
//.getOriginalFilename()
System.err.println("Test upload: " + UploadForm.getFileData());
System.err.println("-------------------------------------------");

return "redirect:/pages/upload";
}
}

型号:

public class UploadForm {

private CommonsMultipartFile fileData;

public CommonsMultipartFile getFileData() {
System.out.println("REACHED GET METHOD");
return fileData;
}
public void setFileData(CommonsMultipartFile fileData) {
System.out.println("REACHED SET METHOD");
this.fileData = fileData;
}
}

我是 Spring 和 MVC 的新手,但我正在慢慢掌握它的窍门。这里的问题是我实际上正在输入 Controller 的 public String create(@Valid UploadForm UploadForm, BindingResult result)方法,但 UploadForm 被作为空值传递,我一生都无法弄清楚它,这变得非常令人沮丧。任何帮助或提示将不胜感激。谢谢!

最佳答案

在 servlet.xml 中,您必须添加:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>

关于java - 使用 Spring MVC 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15261749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com