我有方法Spring MVC中項目的控制器:Spring MVC的錯誤400:客戶端發送的請求是語法不正確
//download resume
//----------------------------------------------------------------------
//@RequestMapping(value="/download/{fileName}",method=RequestMethod.GET)
@RequestMapping(value="/downlo",method=RequestMethod.GET)
public void downloadPDFResource(HttpServletRequest request,
HttpServletResponse response){
// @PathVariable("fileName") String fileName) {
//If user is not authorized - he should be thrown out from here itself
String fileName="Ente Kadha - Madhavikkutty.pdf";
//Career c=cardao.retrieveProfile(a_id);
//c.setA_id(a_id);
//String fileName=c.getResumeDoc();
System.out.println("filename i got :"+fileName);
//Authorized user will download the file
String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/downloads/");
Path file = Paths.get(dataDirectory, fileName);
if (Files.exists(file)) {
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
try {
Files.copy(file, response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
else
{
System.out.println("\n\nFile not found!!\n\n");
System.out.println(file);
}
}
這段代碼實際上works.The文件下載成功,當我發送請求上述控制器,從下面給出jsp頁面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
<center>
<h3 name="fileName">Ente Kadha - Madhavikkutty.pdf</h3>
<h2><a href="downlo">Click here to download the file</a></h2>
</center>
</body>
</html>
但是,當下載一個請求轉到相同的控制器,從一個不同的jsp頁面,這在下文中給出:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF8">
<title>Profile</title>
</head>
<body>
<table>
<th><h3>${profile.name}</h3></th>
<tr>
<td>Application Id :</td>
<td>${profile.a_id}</td>
</tr>
<tr>
<td>Name :</td>
<td>${profile.name}</td>
</tr>
<tr>
<td>Address :</td>
<td>${profile.address}</td>
</tr>
<tr>
<td>Email:</td>
<td>${profile.email}</td>
</tr>
<tr>
<td>Phone:</td>
<td>${profile.phone}</td>
</tr>
<tr>
<td>Vacancy id:</td>
<td></td>
</tr>
<tr>
<td>Date Applied :</td>
<td>${profile.dateApplied}</td>
</tr>
<tr>
<td>Resume : ${profile.resumePath}${profile.resumeDoc} </td>
<td><a href="downlo">Click here to download the file</a></td>
</tr>
</table>
</body>
</html>
但現在,這給了我一個錯誤:
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect.
Apache Tomcat/8.0.3
我真的很困惑,爲什麼會出現這樣的錯誤! 我要求的方式是一樣的,控制器方法是一樣的,即使是要下載的文件在兩種情況下都是一樣的!
仍然出現錯誤。任何人都可以幫助我嗎? 請....
開放開發工具,在你的瀏覽器(Firefox/Chrome瀏覽器),並檢查在每種情況下發送的請求。此外,將'spring-web'調試並調查日誌,如下所述:https://stackoverflow.com/a/15432703/3635454 – pleft
GET http:// localhost:8084/IntelliLabsWeb/oneProfile/downlo 400(Bad Request) – ANJU
當我嘗試從jsp頁面「oneProfile」發送請求到我的項目中的任何控制器方法,它會給出錯誤400 – ANJU