Create OutputFile in jsp and write this file to response
Hello
Please can you help me
I need to dynamically create a file in jsp and send this file to user
Please help
Hello
Please can you help me
I need to dynamically create a file in jsp and send this file to user
Please help
Don't this in the JSP, use a servlet instead. You can then use the servlet from any JSP to fetch a file.
I'm fairly certain you know how to work with files using streams. The same principle applies here, open a filestream for output and write to it to create a file.
To send the file to the user, set the required headers and then get the output stream of the response and write the contents of the file to it.
A skeleton example would be:
BufferedOutputStream out = new BufferedOutputStream( resp.getOutputStream());
String filename = "myFile.xls";
try
{
resp.setContentType("application/vnd.ms-excel" );
resp.setHeader("Content-disposition","attachment; filename=\"" + filename + "\"" );
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("/") + filename));
int i;
while ((i=bufferedInputStream.read()) != -1)
{
out.write(i);
}
bufferedInputStream.close();
out.flush();
out.close();
}
catch ( Exception e )
{
System.out.println(e);
}
You can improve on this quite a bit but it should get you started...this is specific to an Excel file so you should change the MIME type. I'm not certain but I think application/octet-stream would work for generic files...
Don't forget to set the contentLength. Some clients/applications will refuse to run/open the file otherwise.
At the bottom of this article you can find several reuseable downloadFile() utility methods: http://balusc.xs4all.nl/srv/dev-jep-pdf.html
I'll copypaste them here:
package net.balusc.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLConnection;
import javax.servlet.http.HttpServletResponse;
public class HttpServletUtil {
/**
* Send the given file as a byte array to the servlet response. If attachment
* is set to true, then show a "Save as" dialogue, else show the file inline
* in the browser or let the operating system open it in the right application.
* @param response The HttpServletResponse to be used.
* @param bytes The file contents in a byte array.
* @param fileName The file name.
* @param attachment Download as attachment?
*/
public static void downloadFile(HttpServletResponse response, byte[] bytes, String fileName, boolean attachment) throws IOException {
// Wrap the byte array in a ByteArrayInputStream and pass it through another method.
downloadFile(response, new ByteArrayInputStream(bytes), fileName, attachment);
}
/**
* Send the given file as a File object to the servlet response. If attachment
* is set to true, then show a "Save as" dialogue, else show the file inline
* in the browser or let the operating system open it in the right application.
* @param response The HttpServletResponse to be used.
* @param file The file as a File object.
* @param attachment Download as attachment?
*/
public static void downloadFile(HttpServletResponse response, File file, boolean attachment) throws IOException {
// Prepare stream.
BufferedInputStream input = null;
try {
// Wrap the file in a BufferedInputStream and pass it through another method.
input = new BufferedInputStream(new FileInputStream(file));
downloadFile(response, input, file.getName(), attachment);
} catch (IOException e) {
throw e;
} finally {
// Gently close stream.
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
// This is a serious error. Do more than just printing a trace.
}
}
}
}
/**
* Send the given file as an InputStream to the servlet response. If attachment
* is set to true, then show a "Save as" dialogue, else show the file inline
* in the browser or let the operating system open it in the right application.
* @param response The HttpServletResponse to be used.
* @param input The file contents in an InputStream.
* @param fileName The file name.
* @param attachment Download as attachment?
*/
public static void downloadFile(HttpServletResponse response, InputStream input, String fileName, boolean attachment) throws IOException {
// Prepare stream.
BufferedOutputStream output = null;
try {
// Prepare.
int contentLength = input.available();
String contentType = URLConnection.guessContentTypeFromName(fileName);
String disposition = attachment ? "attachment" : "inline";
// Init servlet response.
response.setContentLength(contentLength);
response.setContentType(contentType);
response.setHeader("Content-disposition", disposition + "; filename=\"" + fileName + "\"");
output = new BufferedOutputStream(response.getOutputStream());
// Write file contents to response.
while (contentLength-- > 0) {
output.write(input.read());
}
// Finalize task.
output.flush();
} catch (IOException e) {
throw e;
} finally {
// Gently close stream.
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
// This is a serious error. Do more than just printing a trace.
}
}
}
}
}