我在同一个主题上看过几个qts.但是我没有发现此错误的任何线索.
如上面的教程中所述,在独立模式下(春季嵌入式Tomcat),它运行良好.
但是我想将其部署为Web应用程序.因此,我创建了一个单独的SpringMVC项目并添加了以下控制器.
控制器文件
@Controller
public class FileUploadController {
@RequestMapping(value="/upload", method=RequestMethod.GET)
public @ResponseBody String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
我已经编写了以下客户端(因为我不想在这里使用RestTemplate).
服务客户
private static final String URL_GET = "http://localhost:8080/SpringMVC/upload";
static String URL = "http://localhost:8080/SpringMVC/upload";
public static void main(String[] args) throws Exception {
PropertyConfigurator.configure("C:/DevEnvProject/eclipse/workspace_exp/OCR/log4j.properties");
testGet();
testPOST();
}
private static void testGet() throws ClientProtocolException, IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(URL_GET);
HttpResponse response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
}
static void testPOST() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(URL);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("name", new StringBody("testIcon.png"));
entity.addPart("file", new FileBody(new File("C:/testIcon.png")));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
} catch (Exception e) {
e.printStackTrace();
}
}
我无法成功调用POST端点.每次,我都会遇到以下异常.
400错误的请求-客户端发送的请求在语法上不正确
“ GET”通话正常.我将“ POST”请求的日志与我在春季教程中提到的使用独立方法进行测试时得到的“ POST”请求的日志进行了比较.在请求部分未找到任何差异.
我知道我在这篇文章中很冗长.我想提供尽可能多的上下文信息.请帮忙.
谢谢
解决方法:
您需要做两件事:
首先,将Apache Commons FileUpload库添加到您的类路径.如果使用maven,则可以获得here依赖性.如果不使用,则仍可以下载jar并手动添加.
其次,您必须在上下文中使用名称multipartResolver声明一个MultipartResolver bean.通过Apache Commonds FileUpload,您可以使用CommonsMultipartResolver.例如,使用Java配置,那将是
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
// set any fields
return commonsMultipartResolver;
}
使用XML配置,
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
标签:java,spring,rest,spring-mvc
来源: https://codeday.me/bug/20191012/1900735.html
本文档描述了在尝试将Spring MVC应用部署为Web应用时,遇到POST请求400错误的问题。作者创建了一个SpringMVC项目,并编写了用于文件上传的控制器。客户端使用HttpClient进行GET和POST请求。GET请求工作正常,但POST请求导致400错误。解决方案是添加Apache Commons FileUpload库到类路径,并在应用上下文中配置MultipartResolver bean。

1868

被折叠的 条评论
为什么被折叠?



