我正在尝试从Android上传一个文件到Spring MVC,并且我一直在获取HTTP 400 - 客户端发送的请求在语法上是不正确的错误。我观察到的是当我只发送文件时它上传了没有错误,但当我将文件名与文件关联时,我收到此错误.Below是我的代码。
RestTemplate newRestTemplate = new RestTemplate();
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
formHttpMessageConverter.setCharset(Charset.forName("UTF8"));
List<HttpMessageConverter<?>> msgConverters = new ArrayList<HttpMessageConverter<?>>();
msgConverters.add(formHttpMessageConverter);
msgConverters.add(new StringHttpMessageConverter());
msgConverters.add(new MappingJackson2HttpMessageConverter());
newRestTemplate.setMessageConverters(msgConverters);
newRestTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
protected boolean hasError(HttpStatus statusCode) {
return false;
}
});
File image = new File(imagePath, upFile.getFileName());
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "bearer " + accessToken);
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("file", new FileSystemResource(image));
map.add("name", "filename");
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> imageEntity = new HttpEntity<MultiValueMap<String, Object>>(map, headers);
ResponseEntity<String> restRes = newRestTemplate.exchange(MainActivity.strUrl +"/uploadFile", HttpMethod.POST, imageEntity, String.class);
上面是android代码。现在弹簧部分如下。
servelet.xml:
<context:annotation-config />
<context:component-scan base-package="com.example.myproject" >
<context:exclude-filter type="assignable" expression="com.example.myproject.controller.MyController"/>
</context:component-scan>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<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="1000000000" />
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<!-- Hibernate Session Factory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>com.example.myproject</value>
</array>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
控制器:
@RequestMapping(value="/uploadFile",method=RequestMethod.POST)
public @ResponseBody String uploadFile(@RequestParam("file") MultipartFile srcFile,@PathVariable("name") String filename){
try{
byte[] bytes = srcFile.getBytes();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("/home/ejacob/Templates/"+filename)));
bos.write(bytes);
bos.close();
return "Success";
}catch(Exception e){
e.printStackTrace();
return "Failed";
}
}