Route of Content-Disposition attachment - adds trailing text

26 views
Skip to first unread message

Arik Sher

unread,
Jan 30, 2017, 7:10:49 AM1/30/17
to sparkjava
Hi,

been trying to create Route that downloads a file. here is the class:

public class MyRoute
implements     Route
{
@Override
public Object handle(Request request, Response response) throws Exception {
HttpServletResponse httpResponse = response.raw();

httpResponse.addHeader("Cache-Control", "no-cache");
httpResponse.setContentType("text/plain");

httpResponse.addHeader("Content-Disposition", "attachment; filename=\"abc.txt\"");

ServletOutputStream outputStream = httpResponse.getOutputStream();
outputStream.write("hello\n".getBytes());

  return httpResponse;
}
}

when I requested for the URL it downloaded a file named abc.txt (as expected) but the look at the content:

hello
HTTP/1.1 200 
Date: Mon, 30 Jan 2017 12:04:17 GMT
Cache-Control: no-cache
Content-Type: text/plain
Content-Disposition: attachment; filename="abc.txt"

it contains the printed hello, but has trailing (http header???) info.
looks like a bug?
or Am I doing something wrong?

I use version 2.5.4 via maven:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5.4</version>
</dependency>

please help.
Thanks,
Arik



domin...@gmail.com

unread,
Jan 30, 2017, 2:28:19 PM1/30/17
to sparkjava
Can I just confirm what you are trying to do.

You want a route that downloads a file attachment named abc.txt. And the content of that file to be the string "hello"

Is that correct?

domin...@gmail.com

unread,
Jan 30, 2017, 2:45:10 PM1/30/17
to sparkjava
If that is the case you shouldn't be returning the raw HttpResponse object. Spark is going to call toString on that I think. Return null, and correct complete your response stream. like the code snippet below. Or look at the example project

public static void main(String[] args) {
    System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
    get("/", new Route() {
        public Object handle(Request request, Response response) throws Exception {
            HttpServletResponse httpResponse = response.raw();

            httpResponse.addHeader("Cache-Control", "no-cache");
            httpResponse.setContentType("text/plain");

            httpResponse.addHeader("Content-Disposition", "attachment; filename=\"abc.txt\"");

            ServletOutputStream outputStream = httpResponse.getOutputStream();
            byte[] bytes = "hello".getBytes(Charset.defaultCharset());
            outputStream.write(bytes);

            httpResponse.setStatus(200);
            httpResponse.setContentLength(bytes.length);
            
            return null;
        }
    });
}

Arik Sher

unread,
Jan 31, 2017, 5:43:25 AM1/31/17
to sparkjava
the return of null works.
however , in my real code it is harder to know the ContentLength.
and if not setting it it won't work.
the workaround is :
outputStream.close();
instead of
         httpResponse.setContentLength(...)

thanks.
Reply all
Reply to author
Forward
0 new messages