2017-09-25 64 views
0

我有方法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

我真的很困惑,为什么会出现这样的错误! 我要求的方式是一样的,控制器方法是一样的,即使是要下载的文件在两种情况下都是一样的!

仍然出现错误。任何人都可以帮助我吗? 请....

+0

开放开发工具,在你的浏览器(Firefox/Chrome浏览器),并检查在每种情况下发送的请求。此外,将'spring-web'调试并调查日志,如下所述:https://stackoverflow.com/a/15432703/3635454 – pleft

+0

GET http:// localhost:8084/IntelliLabsWeb/oneProfile/downlo 400(Bad Request) – ANJU

+0

当我尝试从jsp页面“oneProfile”发送请求到我的项目中的任何控制器方法,它会给出错误400 – ANJU

回答

0

根据张贴在你的问题(和评论)的信息,我想以下几点:

  • 在你的控制器downloadPDFResource方法听网址:/downlo。如果您的控制器在其类定义上没有其他@RequestMapping注释,则表示可以从您的应用程序的根目录访问下载URL,例如:localhost:8084/IntelliLabsWeb/downlo。你能证实这一点吗?

  • 由于您使用在您的<a>标签相对链接,对第一种情况下的文件,会出现在你的应用程序的根因此<a>标签指向正确的下载网址:localhost:8084/IntelliLabsWeb/downlo

  • 但是,在第二种情况下,你的文件可能驻留在一个最有可能命名为oneProfile子文件夹和<a href>标记点来localhost:8084/IntelliLabsWeb/oneProfile/downlo相对链接,这不是从downloadPDFResource法里担任因此,你会得到一个错误400

尝试改变你的第二个链接

<a href="../downlo">Click here to download the file</a> 
+0

yes..download URL可从应用程序的根目录访问,例如'localhost:8084/IntelliLabsWeb/downlo'。 – ANJU

+0

好吧,如果你改变(看我更新的答案)你的第二个链接:'Click here to download the file'它应该工作 – pleft

+0

但是它也可以从驻留在子文件夹内的http:// localhost:8084/IntelliLabsWeb/AllQueries访问。 – ANJU

相关问题