我正在尝试在Java中创建图像上传资源。 我的功能如下:
Response fun(@FormDataParam("json") Employee emp,
@FormDataParam("file") MultipartFile file){
}
如果我使用邮递员进行文件输入,则文件的值原来为空。
如果我以InputStream
作为参数来代替MultipartFile
,它将按预期工作。
我在这里想念东西吗?
答案 0 :(得分:0)
请求类别:
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
public class UserDataForm {
private String name;
private List<MultipartFile> file;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<MultipartFile> getFile() {
return file;
}
public void setFile(List<MultipartFile> file) {
this.file = file;
}
@Override
public String toString() {
return "UserDataForm [name=" + name + ", file=" + file + "]";
}
}
第二个配置:
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
//resolver.setMaxInMemorySize(10000); //Define your max or min file size
resolver.setDefaultEncoding("utf-8");
return resolver;
}
控制器层:
/*you can also remove @ModelAttribute that works same*/
@RequestMapping(value = "userInfoTest" , method = RequestMethod.POST)
public ResponseJson getInfo(@ModelAttribute UserDataForm user) {
response.setResponse("Class Info" + user);
return response;
}