user1332962
user1332962

Reputation: 111

File response in REST based webservice using java

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?

Upvotes: 2

Views: 11463

Answers (3)

Anand
Anand

Reputation: 21320

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

Upvotes: 4

user1332962
user1332962

Reputation: 111

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?

Upvotes: 0

fonZ
fonZ

Reputation: 2479

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 :)

Upvotes: 0

Related Questions