2

I am writing a REST webserivce using spring. I have to return back a file in the response.

Its a GET call and when the user enters the URL, the user should be shown with the download section in the browser.

I am not sure what should be the return type in the controller. Do i have to specify any content type i code?

3 Answers 3

4

I had the similar requirement in my project. I used the below piece of code

@Controller
@RequestMapping("/reports")
public class ReportsController {

    protected static String PRODUCTIVITY_REPORT_FILE = "productivityReportFile";

    @Resource(name="propertyMap")
    protected Map<String, String> propertyMap;

    @RequestMapping(value="/cratl/productivity_report", method=RequestMethod.GET, produces="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    public @ResponseBody byte[] getProductivityReport()
            throws Exception {
        byte[] reportBytes = null;
        try {
            File reportFile = new File(propertyMap.get(PRODUCTIVITY_REPORT_FILE));
            if (reportFile != null && reportFile.exists()) {
                InputStream reportInputStream = new FileInputStream(reportFile);
                long length = reportFile.length();
                reportBytes = new byte[(int)length];
                int offset = 0;
                int numRead = 0;
                while (offset < reportBytes.length
                       && (numRead = reportInputStream.read(reportBytes, offset, reportBytes.length-offset)) >= 0) {
                    offset += numRead;
                }
                if (offset < reportBytes.length) {
                    throw new Exception("Could not completely read file "+ reportFile.getName());
                }
                reportInputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reportBytes;
    }

I hope it helps you

Sign up to request clarification or add additional context in comments.

1 Comment

can anyone explain me why this one scored with -1
0

Your controller method, which can have any name you want, returns a String with a url name that is defined in the views.xml that is defined for the view you want to load, the downloads section in this case. So your controller looks like this:

@Controller
public class MyController {

    @RequestMapping(value = "/downloads", method = RequestMethod.GET)
    public String getDownloadSection() {
        System.out.println("getting downloads");
        return "downloads/index";
    }
}

Your views.xml should contain the tag:

<definition extends="default" name="downloads/index">
<put-attribute name="body" value="/WEB-INF/views/downloads/index.jspx"/>
</definition>

The extends="default" is a tile definition that should be in your layouts.xml

I think thats about it. If you do a GET request to //yoursite/downloads it should print the message.

That should answer your question i hope :)

Comments

0

I used the below piece of code

    FileInputStream inputStream = new FileInputStream("FileInputStreamDemo.java");  //read the file

    response.setHeader("Content-Disposition","attachment; filename=test.txt");
    try {
        int c;
        while ((c = inputStream.read()) != -1) {
        response.getWriter().write(c);
        }
    } finally {
        if (inputStream != null) 
            inputStream.close();
            response.getWriter().close();
    }

This was found in one more thread

how to write a file object on server response and without saving file on server?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.