Acoustic Alchemy - The Beautiful Game - Instrumental JAZZ
Isn't it awesome ?
<a href="download">Download String </a>
Controller Method :
@RequestMapping(value = "/download", method = RequestMethod.GET)
public @ResponseBody
void downloadFile(HttpServletResponse resp) {
String downloadFileName= "download.txt";
String downloadStringContent= getStringToWrite(); // implement this
try {
OutputStream out = resp.getOutputStream();
resp.setContentType("text/plain; charset=utf-8");
resp.addHeader("Content-Disposition","attachment; filename=\"" + downloadFileName + "\"");
out.write(downloadStringContent.getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
} catch (IOException e) {
}
}
<a href="downloadFile?fileName=log.txt">Download String </a>
Controller Method :
@RequestMapping(value = "/downLoadFile", method = RequestMethod.GET)
public void downLoadFile( HttpServletRequest request, HttpServletResponse response ) {
try {
String fileName = request.getParameter( "fileName" );
File file = getFileToDownload(fileName) // implement this to return a valid file object
InputStream in = new BufferedInputStream( new FileInputStream( file ) );
response.setContentType( "text/plain" ); // define your type
response.setHeader( "Content-Disposition", "attachment; filename=" + fileName );
ServletOutputStream out = response.getOutputStream( );
IOUtils.copy( in, out ); //import org.apache.commons.io.IOUtils;
response.flushBuffer( );
} catch ( Exception e ) {
e.printStackTrace( );
}
}
package g.t.test; import org.xhtmlrenderer.pdf.ITextRenderer; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; public class HtmlToPDFConverter { public static void convert(String htmlContent, File pdfFile) throws Exception { ByteArrayOutputStream os = new ByteArrayOutputStream(); //step1: render html to memory
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(os);
//step2: conver to byte array stream
byte[] pdfAsBytes = os.toByteArray();
os.close();
//step3: write byte array stream to file
FileOutputStream fos = new FileOutputStream(pdfFile);
fos.write(pdfAsBytes);
fos.flush();
fos.close();
}
// let's test !!
public static void main(String[] args) throws Exception{
convert("<html> <body> " +
"<h1>Hello Crazy World !!</h1> <br/> " +
"<h2> I hope you are doing great.</h2> " +
"</body> </html>", new File("test.pdf"));
}
}
<dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf</artifactId> <version>9.0.9</version> </dependency>