servlets - Dowload link in Java, Spring MVC -
i trying implement link in webpage download file csv. content of file being appended stringbuffer , flushed response there itself. code below tried achieve :
public void getdownloadfile(httpservletresponse response) throws ioexception{ response.setcontenttype("application/octet-stream"); response.setheader("content-disposition","attachment;filename=data.csv"); servletoutputstream out = response.getoutputstream(); try{ stringbuffer sb = generatecsvfilebuffer(); inputstream in = new bytearrayinputstream(sb.tostring().getbytes("utf-8")); byte[] outputbyte = new byte[4096]; while(in.read(outputbyte, 0, 4096) != -1) { out.write(outputbyte, 0, 4096); } in.close(); out.flush(); out.close(); } catch (exception e) { system.out.println("error in csvfilewriter !!!"); e.printstacktrace(); } { try { out.flush(); out.close(); } catch (ioexception e) { system.out.println("error while flushing/closing filewriter !!!"); e.printstacktrace(); } } return; }
and generatecsvfilebuffer() method called details file written. method follows:
public stringbuffer generatecsvfilebuffer(){ stringbuffer writer = new stringbuffer(); writer.append("name"); writer.append(", "); writer.append("age"); writer.append(", "); writer.append("address"); writer.append(", "); writer.append("phone no"); writer.append("\n"); return writer; }
the programs runs till last line of code without error no output. when link clicked nothing happens in view. highly appreciated.
i took liberty rewrite code java 7's try-with-resources, better error handling , readability.
package demo.spring.mvc; import java.io.bytearrayinputstream; import java.io.ioexception; import java.io.inputstream; import javax.servlet.servletoutputstream; import javax.servlet.http.httpservletresponse; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; @controller public class filedownloadcontroller { @requestmapping("/download") public void download(model model, httpservletresponse resp) { resp.setcontenttype("text/csv"); resp.setheader("content-disposition", "attachment;filename=data.csv"); string csv = generatecsv(); try (servletoutputstream out = resp.getoutputstream();) { byte[] arr = new byte[4096]; try (inputstream in = new bytearrayinputstream(csv.getbytes("utf-8"));) { (int c; (c = in.read(arr)) != -1;) { out.write(arr, 0, c); } } } catch (ioexception e) { system.err.println("error in csvfilewriter!!!"); e.printstacktrace(); } } public string generatecsv() { stringbuilder sb = new stringbuilder(); sb.append("name") .append(", ").append("age") .append(", ").append("address") .append(", ").append("phone no") .append("\n"); return sb.tostring(); } }
as added bonus, here thoughts original code.
- i changed
stringbuffer
stringbuilder
. since java 5, can better performance switchingstringbuffer
s not synchronized counterpartstringbuilder
. - your while loop had error because didn't test number of characters read (the
c
int introduced). - you can chain calls
append(...)
avoid repeating variable name upon method applied. - i switched calls
system.out
callssystem.err
because it's error handling here. it's far perfect. you'd better off logging system.
Comments
Post a Comment