java-SpringMVC-FileUpload-客户端发送的请求在语法上不正确
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-SpringMVC-FileUpload-客户端发送的请求在语法上不正确,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3644字,纯文字阅读大概需要6分钟。
内容图文
我在同一个主题上看过几个qts.但是我没有发现此错误的任何线索.
我正在研究POC,并点击下面的链接.
http://spring.io/guides/gs/uploading-files/
如上面的教程中所述,在独立模式下(春季嵌入式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配置,
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set any properties -->
</bean>
这在Spring official documentation中有进一步记录.
内容总结
以上是互联网集市为您收集整理的java-SpringMVC-FileUpload-客户端发送的请求在语法上不正确全部内容,希望文章能够帮你解决java-SpringMVC-FileUpload-客户端发送的请求在语法上不正确所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。