我在Spring Boot和Reactjs的前端提供了服务,但是我的服务没有提供任何东西(没有结果)
我正在上传带有文件图像或音频的表单数据,但我的休息服务处没有收到任何文件。
我的服务等级:
@CrossOrigin
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ServiceResponse save(MultipartHttpServletRequest request)
{
String event_name=request.getParameter("event_name");
logger.debug("event name: "+event_name);
logger.debug("Has a file :"+request.getFileNames().hasNext()); // getting false
if(request.getFileNames().hasNext()) {
// Working here
try{}
catch(Exception e){
return ServiceResponse.createFailureResponse("Request
failed!");
}
}
return ServiceResponse.createSuccessResponse();
}
需要依赖项:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
创建的Bean:
@Configuration
@ComponentScan
@EntityScan("com.mphasis.springreact.services.admin")
@EnableJpaRepositories("com.mphasis.springreact.services.admin")
@PropertySource("classpath:config.properties")
public class DBConfiguration {
public DBConfiguration() {
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource)
{
return new JdbcTemplate(dataSource);
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/DummyApp");
dataSource.setUsername("postgres");
dataSource.setPassword("root");
return dataSource;
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver createMultipartResolver() {
CommonsMultipartResolver resolver=new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
return resolver;
}
@Bean
public MultipartResolver filterMultipartResolver()
{
return new StandardServletMultipartResolver();
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
这是我的前端部分,尽管服务本身不起作用:
var blob = new Blob(this.state.chunks, {type: 'audio/mp3;codec=opus'});
var formData=new FormData();
formData.append("event_name",event);
formData.append("file",new File([blob], event, {type: 'audio/mp3;codec=opus', lastModified: Date.now()}));
fetch('http://localhost:6020/uploadFile', {
method: "POST",
mode: "cors",
cache: "no-cache",
redirect: "follow",
referrer: "no-referrer",
body: formData,
})
.then(response => response.json())
.then(function(data){ console.log( JSON.stringify( data ) ) });
自从我进行休息呼叫以来,我一直未在服务类中错过网络依赖,因此我猜我不需要它,但是在大多数教程中,都提到它可以用于多部分文件