Re: wicket File downlaod
opps, sorry what i meant to write was: File pdfFile = new File(test.pdf); But this doesn't really change much and the problem still remains that the file is empty (whether txt or pdf). mbelarbi wrote: Hi, I have some code that generates a pdf report file (using jasper reports). This file is stored somewhere in the project. What I need to do is be able to provide a link which makes that file available for download when clicked (downloadLink?). How do i do this using wicket? and are there any examples. I've tried this: File pdfFile = new File(test.txt); DownloadLink dLink = new DownloadLink(dLink, pdfFile); This does exactly what i am looking for (bring up a file for download), but this file is empty, it has no content. This pdf file resides in the same hierarchy as the source code file. -- View this message in context: http://www.nabble.com/wicket-File-downlaod-tf4874269.html#a13946991 Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket File downlaod
The file really ought to reside in another location, but anyway... I think new File(test.pdf) would actually indicate a file named test.pdf, at the current working directory of the application server. Output the file's path with getCanoicalPath/getAbsolutePath (or something along that line) to verify that the path of the pdf is correct first. On 26 Nov 2007, at 6:07 PM, mbelarbi wrote: opps, sorry what i meant to write was: File pdfFile = new File(test.pdf); But this doesn't really change much and the problem still remains that the file is empty (whether txt or pdf). mbelarbi wrote: Hi, I have some code that generates a pdf report file (using jasper reports). This file is stored somewhere in the project. What I need to do is be able to provide a link which makes that file available for download when clicked (downloadLink?). How do i do this using wicket? and are there any examples. I've tried this: File pdfFile = new File(test.txt); DownloadLink dLink = new DownloadLink(dLink, pdfFile); This does exactly what i am looking for (bring up a file for download), but this file is empty, it has no content. This pdf file resides in the same hierarchy as the source code file. -- View this message in context: http://www.nabble.com/wicket-File- downlaod-tf4874269.html#a13946991 Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket File downlaod
Sometime ago this was answered on the list but i cannot find it right now :-) Maybe the following snippets will help you (it's wicket 1.2.6): WebResource export = new WebResource() { private static final long serialVersionUID = 1L; @Override public IResourceStream getResourceStream() { try { return new FileService(f).getResourceStream(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void setHeaders(WebResponse response) { super.setHeaders(response); response. setAttachmentHeader(attachment.getOriginalFilename()); } }; export.setCacheable(false); ResourceLink dlLink = new ResourceLink(attachmentLink, export); ... public class FileService { private File file; private String outputName; private String contentType = ; public FileService(File file) { this.file = file; this.outputName = this.file.getName(); } /** * sets the output name and returns itself * @param outputName * @return */ public FileService setOutputName(String outputName) { this.outputName = outputName; return FileService.this; } /** * sets the content type and returns itself * @param contentType * @return */ public FileService setContentType(String contentType) { this.contentType = contentType; return FileService.this; } public IResourceStream getResourceStream() throws IOException { FileInputStream fi = new FileInputStream(this.file); return new IResourceStreamImpl(fi, this.contentType, this.file.length()); } /** * wrapper which creates the necessary [EMAIL PROTECTED] ResourceStreamRequestTarget} * @return * @throws IOException */ public ResourceStreamRequestTarget getResourceStreamRequestTarget() throws IOException { return new ResourceStreamRequestTarget(this.getResourceStream()) { public String getFileName() { return (outputName); } }; } } ... public class IResourceStreamImpl implements IResourceStream { private static final long serialVersionUID = 1L; private Locale locale = null; private String contentType = null; private InputStream inputStream = null; private long size; /** * @param fileInputStream * @param contentType * @param file */ public IResourceStreamImpl(InputStream inputStream, String contentType, long size) { this.inputStream = inputStream; this.size = size; this.contentType = contentType; } public void close() throws IOException { this.inputStream.close(); } public InputStream getInputStream() throws ResourceStreamNotFoundException { return this.inputStream; } public String getContentType() { return (this.contentType); } public Locale getLocale() { return (this.locale); } public long length() { return this.size; } public void setLocale(Locale locale) { this.locale = locale; } public Time lastModifiedTime() { return null; } } mbelarbi schrieb: opps, sorry what i meant to write was: File pdfFile = new File(test.pdf); But this doesn't really change much and the problem still remains that the file is empty (whether txt or pdf). mbelarbi wrote: Hi, I have some code that generates a pdf report file (using jasper reports). This file is stored somewhere in the project. What I need to do is be able to provide a link which makes that file available for download when clicked (downloadLink?). How do i do this using wicket? and are there any examples. I've tried this: File pdfFile = new File(test.txt); DownloadLink dLink = new DownloadLink(dLink, pdfFile); This does exactly what i am looking for (bring up a file for download), but this file is empty, it has no content. This pdf file resides in the same hierarchy as the source code file. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket File downlaod
thanks Kent lai, a system print for the file's getCanoicalPath seemed to show that the file path i was passing in was incorrect. Now it's resolved. Thanks kent lai wrote: The file really ought to reside in another location, but anyway... I think new File(test.pdf) would actually indicate a file named test.pdf, at the current working directory of the application server. Output the file's path with getCanoicalPath/getAbsolutePath (or something along that line) to verify that the path of the pdf is correct first. On 26 Nov 2007, at 6:07 PM, mbelarbi wrote: opps, sorry what i meant to write was: File pdfFile = new File(test.pdf); But this doesn't really change much and the problem still remains that the file is empty (whether txt or pdf). mbelarbi wrote: Hi, I have some code that generates a pdf report file (using jasper reports). This file is stored somewhere in the project. What I need to do is be able to provide a link which makes that file available for download when clicked (downloadLink?). How do i do this using wicket? and are there any examples. I've tried this: File pdfFile = new File(test.txt); DownloadLink dLink = new DownloadLink(dLink, pdfFile); This does exactly what i am looking for (bring up a file for download), but this file is empty, it has no content. This pdf file resides in the same hierarchy as the source code file. -- View this message in context: http://www.nabble.com/wicket-File- downlaod-tf4874269.html#a13946991 Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- View this message in context: http://www.nabble.com/wicket-File-downlaod-tf4874269.html#a13947358 Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket File downlaod
Hi, I have never used jasper reports, but a quick search shows me that you can also generate the file as a byte[] in memory: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperExportManager.html#exportReportToPdf(net.sf.jasperreports.engine.JasperPrint) Class JasperExportManager: public static byte[] exportReportToPdf(JasperPrint jasperPrint) throws JRException In such case you could avoid all the trouble with files by using a DynamicWebResource instead of the WebResource described by Florian. Here a quick example: public class MyDynamicWebResource extends DynamicWebResource { protected ResourceState getResourceState() { return new ResourceState() { public String getContentType() { return application/pdf; } public byte[] getData() { return generatePDFDataUsingJasperOrWhatever(); } }; } // this is optional i think protected void setHeaders(WebResponse cResponse) { super.setHeaders(cResponse); cResponse.setAttachmentHeader(test.pdf); } } For me this is the most convenient thing to do if you don't need to store the files for later usage. Bye, German It seems that Florian Sperber wrote: Sometime ago this was answered on the list but i cannot find it right now :-) Maybe the following snippets will help you (it's wicket 1.2.6): WebResource export = new WebResource() { private static final long serialVersionUID = 1L; @Override public IResourceStream getResourceStream() { try { return new FileService(f).getResourceStream(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void setHeaders(WebResponse response) { super.setHeaders(response); response. setAttachmentHeader(attachment.getOriginalFilename()); } }; export.setCacheable(false); ResourceLink dlLink = new ResourceLink(attachmentLink, export); ... public class FileService { private File file; private String outputName; private String contentType = ; public FileService(File file) { this.file = file; this.outputName = this.file.getName(); } /** * sets the output name and returns itself * @param outputName * @return */ public FileService setOutputName(String outputName) { this.outputName = outputName; return FileService.this; } /** * sets the content type and returns itself * @param contentType * @return */ public FileService setContentType(String contentType) { this.contentType = contentType; return FileService.this; } public IResourceStream getResourceStream() throws IOException { FileInputStream fi = new FileInputStream(this.file); return new IResourceStreamImpl(fi, this.contentType, this.file.length()); } /** * wrapper which creates the necessary [EMAIL PROTECTED] ResourceStreamRequestTarget} * @return * @throws IOException */ public ResourceStreamRequestTarget getResourceStreamRequestTarget() throws IOException { return new ResourceStreamRequestTarget(this.getResourceStream()) { public String getFileName() { return (outputName); } }; } } ... public class IResourceStreamImpl implements IResourceStream { private static final long serialVersionUID = 1L; private Locale locale = null; private String contentType = null; private InputStream inputStream = null; private long size; /** * @param fileInputStream * @param contentType * @param file */ public IResourceStreamImpl(InputStream inputStream, String contentType, long size) { this.inputStream = inputStream; this.size = size; this.contentType = contentType; } public void close() throws IOException { this.inputStream.close(); } public InputStream getInputStream() throws ResourceStreamNotFoundException { return this.inputStream; } public String getContentType() { return (this.contentType); } public Locale getLocale() { return (this.locale); } public long length() { return this.size; } public void setLocale(Locale locale) { this.locale = locale; } public Time lastModifiedTime() { return
Re: wicket File downlaod
Yes I agree, I will later move to providing these reports as streams on the fly instead of storing them, since there will be many many files generated so storage could be a problem in the long run. German Morales-2 wrote: Hi, I have never used jasper reports, but a quick search shows me that you can also generate the file as a byte[] in memory: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperExportManager.html#exportReportToPdf(net.sf.jasperreports.engine.JasperPrint) Class JasperExportManager: public static byte[] exportReportToPdf(JasperPrint jasperPrint) throws JRException In such case you could avoid all the trouble with files by using a DynamicWebResource instead of the WebResource described by Florian. Here a quick example: public class MyDynamicWebResource extends DynamicWebResource { protected ResourceState getResourceState() { return new ResourceState() { public String getContentType() { return application/pdf; } public byte[] getData() { return generatePDFDataUsingJasperOrWhatever(); } }; } // this is optional i think protected void setHeaders(WebResponse cResponse) { super.setHeaders(cResponse); cResponse.setAttachmentHeader(test.pdf); } } For me this is the most convenient thing to do if you don't need to store the files for later usage. Bye, German It seems that Florian Sperber wrote: Sometime ago this was answered on the list but i cannot find it right now :-) Maybe the following snippets will help you (it's wicket 1.2.6): WebResource export = new WebResource() { private static final long serialVersionUID = 1L; @Override public IResourceStream getResourceStream() { try { return new FileService(f).getResourceStream(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void setHeaders(WebResponse response) { super.setHeaders(response); response. setAttachmentHeader(attachment.getOriginalFilename()); } }; export.setCacheable(false); ResourceLink dlLink = new ResourceLink(attachmentLink, export); ... public class FileService { private File file; private String outputName; private String contentType = ; public FileService(File file) { this.file = file; this.outputName = this.file.getName(); } /** * sets the output name and returns itself * @param outputName * @return */ public FileService setOutputName(String outputName) { this.outputName = outputName; return FileService.this; } /** * sets the content type and returns itself * @param contentType * @return */ public FileService setContentType(String contentType) { this.contentType = contentType; return FileService.this; } public IResourceStream getResourceStream() throws IOException { FileInputStream fi = new FileInputStream(this.file); return new IResourceStreamImpl(fi, this.contentType, this.file.length()); } /** * wrapper which creates the necessary [EMAIL PROTECTED] ResourceStreamRequestTarget} * @return * @throws IOException */ public ResourceStreamRequestTarget getResourceStreamRequestTarget() throws IOException { return new ResourceStreamRequestTarget(this.getResourceStream()) { public String getFileName() { return (outputName); } }; } } ... public class IResourceStreamImpl implements IResourceStream { private static final long serialVersionUID = 1L; private Locale locale = null; private String contentType = null; private InputStream inputStream = null; private long size; /** * @param fileInputStream * @param contentType * @param file */ public IResourceStreamImpl(InputStream inputStream, String contentType, long size) { this.inputStream = inputStream; this.size = size; this.contentType = contentType; } public void close() throws IOException { this.inputStream.close(); } public InputStream getInputStream() throws ResourceStreamNotFoundException { return this.inputStream; } public String getContentType() { return (this.contentType); } public Locale getLocale() { return (this.locale); } public long length() {