0

I have method in controller of spring mvc project :

//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);
    }
}

This code actually works.The file download is successful When i send request to the above controller,from the below given jsp page :

<%@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>

But when a request to download goes to the same controller,from a different jsp page,which is given below :

    <%@ 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>

But now,this gives me an error:

HTTP Status 400 -

type Status report

message

description The request sent by the client was syntactically incorrect.

Apache Tomcat/8.0.3

I am really confused why this error occurs?!! The way i request is same,controller method is same,even the file to be downloaded is the same in both cases!!

still the error occurs.Can anyone help me? pls....

ANJU
  • 3
  • 2
  • Open the developers tools on your browser (firefox/chrome) and inspect the request sent in each case. Moreover put `spring-web` to debug and investigate the logs as described here: https://stackoverflow.com/a/15432703/3635454 – pleft Sep 25 '17 at 12:00
  • GET http://localhost:8084/IntelliLabsWeb/oneProfile/downlo 400 (Bad Request) – ANJU Sep 26 '17 at 06:06
  • when i try to send request from jsp page "oneProfile" to any controller method in my project,it gives an error 400 – ANJU Sep 26 '17 at 09:54
  • logs logs logs, what your logs,console,stacktrace etc etc report for this error 400? – pleft Sep 26 '17 at 09:57

1 Answers1

0

Based on the info posted in your question (and comments) I suppose the following:

  • In your controller the downloadPDFResource method is listening to the url: /downlo. If your controller has no other @RequestMapping annotation on its class definition, this means that the download URL is accessible from the root of your application, e.g.: localhost:8084/IntelliLabsWeb/downlo. Can you confirm this?

  • Since you are using relative links in your <a> tags, for the first case the file happens to be in the root of your application hence the <a> tag point to the correct download url: localhost:8084/IntelliLabsWeb/downlo

  • But, in the second case, your file probably resides inside a subfolder most probably named oneProfile and the relative link of the <a href> tag points to localhost:8084/IntelliLabsWeb/oneProfile/downlo, which is not served from the downloadPDFResource method hence you get an error 400.

Try to change your second link to

<a href="../downlo">Click here to download the file</a>
pleft
  • 7,567
  • 2
  • 21
  • 45