java在Spring4和thymeleaf中作为表单的一部分上传文件
我必须创建公司,其中一个必填字段是公司徽标。我创建了以下表单:
@NotNull
@Size(min = 2, max = 256, message = "{validation.length}")
private String name;
@Size(min = 6, max = 32, message = "{validation.length}")
private String companyId;
@NotNull
private String country;
@NotNull
private String city;
@NotNull
private String state;
@NotNull
private String postalCode;
@NotNull
private String address1;
private String address2;
@NotNull
private String phone1;
private String phone2;
private String fax;
@Email(message = "{validation.invalidChars}")
private String email;
private String website;
private MultipartFile companyLogo;
private boolean status;
我想做的是创建公司并上传公司徽标作为表单的一部分,这是我的thymeleaf表单
<form action="#" th:object="${createCompanyForm}" method="post" id="save">
<input type="text" name="name" th:field="*{name}" th:value="${createCompanyForm.name}" th:class="form-control" data-rule-required="true"/>
<input type="text" name="companyId" th:field="*{companyId}" th:value="${createCompanyForm.companyId}" />
<input type="file" name="companyLogo" id="logo" th:field="*{companyLogo}" th:value="${createCompanyForm.companyLogo}"/>
</form>
此表单是通过jquery函数提交的
function save() {
var formData = $('#save').serializeArray();
$.ajax({
url : "create",
type: "POST",
timeout:5000,
data: formData,
contentType: 'application/x-www-form-urlencoded',
cache: false,
success:function(response){
alert(response.message);
},
error: function(response){
alert(response.message);
}
});
};
这是我的ajax控制器
@RequestMapping(
value = "/create",
method = RequestMethod.POST,
consumes = "application/x-www-form-urlencoded",
produces = "application/json")
@ResponseBody
public JsonFormResponse<String> postCreateCompany(@Valid CreateCompanyForm form, BindingResult result,
Locale locale, HttpSession session)
{
JsonFormResponse<String> response = new JsonFormResponse<>();
String message;
if (result.hasErrors())
{
message = getLocalizedMessage(FLASH_CREATE_COMPANY_ERROR, null, locale);
response.setMessage(message);
response.setErrors(result);
response.setStatus(JsonResponseStatus.ERROR);
return response;
}
try
{
String filename = form.getCompanyLogo().getOriginalFilename();
Company company = getCompanyService().create(getCurrentUser(session), form);
if (company == null) { throw new Exception("Error creating the company."); }
User user = getCurrentUser();
user.getCompanies().add(company);
getUserService().update(user);
message = getLocalizedMessage(FLASH_CREATE_COMPANY_SUCCESS, new String[]
{
form.getName()
}, locale);
response.setMessage(message);
response.setStatus(JsonResponseStatus.OK);
return response;
}
catch (Exception ex)
{
LOGGER.error(ex.getMessage(), ex);
message = getLocalizedMessage(FLASH_CREATE_COMPANY_ERROR, null, locale);
response.setMessage(message);
response.setErrors(result);
response.setStatus(JsonResponseStatus.ERROR);
return response;
}
}
正如您所看到的,我正在使用contentType:“application/x-www-form-urlencoded”,我尝试使用多部分/表单数据作为内容类型,但出现以下错误:
Caused by: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
我已经在servlet上下文中添加了多部分配置。xml:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:maxUploadSize="5242880" />
但我还是有例外
有人知道我错过了什么吗?,或者是否有人知道在哪里可以找到作为表单一部分上载文件的示例,我已经找到了很多示例,但是只有一个字段文件字段。我真的很感激你给我的建议
共 (0) 个答案