2017-09-25 96 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

相關問題