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 switching stringbuffers not synchronized counterpart stringbuilder.
  • 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 calls system.err because it's error handling here. it's far perfect. you'd better off logging system.

Comments

Popular posts from this blog

Combining PHP Registration and Login into one class with multiple functions in one PHP file -

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -