java客户端连接请求发不出去_java-SpringMVC-FileUpload-客户端发送的请求在语法上不正确...

本文档描述了在尝试将Spring MVC应用部署为Web应用时,遇到POST请求400错误的问题。作者创建了一个SpringMVC项目,并编写了用于文件上传的控制器。客户端使用HttpClient进行GET和POST请求。GET请求工作正常,但POST请求导致400错误。解决方案是添加Apache Commons FileUpload库到类路径,并在应用上下文中配置MultipartResolver bean。

我在同一个主题上看过几个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值