【发布时间】:2013-12-09 08:31:50
【问题描述】:
我看过几个关于同一主题的 qts。但是我没有发现任何关于这个错误的线索。
我正在开发 POC 并点击下面的链接。 http://spring.io/guides/gs/uploading-files/
正如上面教程中提到的,在独立模式下[spring embeded 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 Bad Request - 客户端发送的请求语法错误
'GET' 调用工作正常。我将“POST”请求的日志与我在使用 Spring 教程中提到的独立方法进行测试时得到的相同“POST”请求进行了比较。在请求部分没有发现任何差异。
我知道我在这篇文章中很冗长。我想提供尽可能多的上下文信息。请帮忙。
谢谢
【问题讨论】:
-
尝试从您的控制器中删除
@ResponseBody并查看。 -
@aman 这不会影响请求进来。
-
@Aman - 感谢您的回复。但这没有帮助。刚才,我试过了
-
请为
org.springframework.web包设置日志并检查输出。您使用的是 Spring 3.2+ 吗? -
不,我没有使用 3.2+ 版本。我正在使用 3.0.5 是的,我启用了日志,这有帮助。我担心我的代码有问题。谢谢
标签: java spring rest spring-mvc