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

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.
}

Similar Messages

  • I have an excel file,i want to read and write this file to xml

    hello,
    i have an excel file,i want to read excel data and write to xml file .i also have an exisitng xml file and i want to appened the excel data to the exisiting xml.please give some example code.
    thanks

    GoOgLe

  • I just restored my 13" MBP i5 at the apple store to the newest version of Lion after issues with a previous Time Machine backup from Snow Leopard- this time I created a new account and just ported files and folders, and now MS Office doesn't work. Help?

    I just restored my 13" MBP i5 at the apple store to the newest version of Lion after issues with a previous Time Machine backup from Snow Leopard- this time I created a new account and just ported files and folders, and now MS Office doesn't work.
    ^^ that's the main problem. Here's the full history.
    I bought a new 13" i5 MBP, early 2011 edition. I had an old white Macbook 2.14 ghz core2duo on Snow Leopard. I attempted to port over my time machine backup, but encountered problems in that my User was inaccessible from the new computer after the import finished, and I had to go in and change the root password, etc, and for some reason or another, I couldn't install any programs at all from that administrator's account. By "couldn't" I mean I could install them, but upon installation they would never boot. So, I took it to the apple store and did a clean install from the most up to date Lion OSX. Then, I created a brand new admin account, instead of trying to import the old one, and things seemed great. Then, I just imported my old files from the TM backup, but not any system settings, permissions, or user data. Just my Docs, pics, vids, apps, and itunes stuff.
    Here's where things get weird again. I imported this stuff under the name "old", but all of these folders have a red negative sign on them, marking them as restricted. So, from my main admin account, I cannot even peruse these folders. Since I didn't import user data, I can't sign in to the "old" account to change permissions. I already tried to change the permissions from system preferences, but that didn't change anything. And now, for whatever reason, of all the apps that were imported then, MS Office is the only set of apps that does not work. When I click on it, it just says there was a problem and asks if I'd like to send a report to apple. I tried reinstalling it to no avail. I'm an English student, so i really need access to Word. Can anyone help? The Apple store is a major detour for me and would like to fix this issue myself.

    Most likely you have Office 2004 which are PPC-only applications and will not work in Lion. Upgrade to Office 2011. Other alternatives are:
    Apple's iWork suite (Pages, Numbers, and Keynote.)
    Open Office (Office 2007-like suite compatible with OS X.)
    NeoOffice (similar to Open Office.)
    LibreOffice (a new direction for the Open Office suite.)

  • I just upgraded my mac book to 10.6.8 and downloaded the new version of itunes. When I try to open it and error message appears saying, "The iTunes Library file cannot be found or created. The default location for this file is in the "iTunes" folder.

    I just upgraded my mac book to 10.6.8 and downloaded the new version of itunes. When I try to open it and error message appears saying, "The iTunes Library file cannot be found or created. The default location for this file is in the "iTunes" folder in the "Music" folder."
    I have tried a few things but iTunes closes right after i click okay. Anyone know anything?

    Look in your Home > Music folder.  There should be an iTunes folder there and it should have a number of files and folders including one called library.itl  You want to guide iTunes to that file.  If you don't see that file or folder then in your next post you will have to be very specific as to what you do see or how you have your iTunes set up because that's where it should be according to default settings.

  • I am trying to open iTunes on my Windows 7 PC and am getting the following message...the itunes library can not be found or created.  The default location for this file is in the "iTunes" folder in the "Music" folder.  Any ideas?

    I am trying to open iTunes on my Windows 7 PC and am getting the following message...the itunes library can not be found or created.  The default location for this file is in the "iTunes" folder in the "Music" folder.  Any ideas?

    Let's try this first. Hold down the Shift key while you try to launch iTunes. You should eventually see the following dialog:
    Click "Choose library". Browse to inside the following location (depending on what operating system you're running):
    Operating System
    Default location of iTunes library
    Microsoft Windows XP
    \Documents and Settings\[your username]\My Documents\My Music\iTunes\
    Microsoft Windows Vista
    \Users\[your username]\Music\iTunes\
    Microsoft Windows 7
    \Users\[your username]\My Music\iTunes\
    ... and open the iTunes library you find in there.

  • What is causing this?  The file "iTunes Library.itl" does not appear to be a valid iTunes library file. iTunes has created a new iTunes library and renamed this file to "iTunes Library (Damaged) 4".  This happens everytime I start iTunes lateley.  Help!!!

    Everytime I open iTunes lately...I get all my music thrown out of the library and this annoying message come up...The file “iTunes Library.itl” does not appear to be a valid iTunes library file. iTunes has created a new iTunes library and renamed this file to “iTunes Library (Damaged) 4”.  Is this how an apple works?  It doesn't?  Why all of a sudden is it doing this?  Please help.  Thank you.
    David

    Did you happen to bring this iTunes library over from a windows machine? You might read this kb article to see if it helps http://support.apple.com/kb/HT1451

  • How do I migrate files created in a Windows OS to a new external drive that is formatted as a MAC drive. I want to read and write these files back to the new drive.

    How do I migrate files from an external hard drive created in a Windows OS to a new MAC external drive? I want to read and write these files to the new drive.

    LKrzesowski wrote:
    How do I migrate files from an external hard drive created in a Windows OS to a new MAC external drive? I want to read and write these files to the new drive.
    If all you're trying to do is move the files from the Windows external to the Mac external, you can simply connect them both to the Mac and drag the files from the Windows drive to the Mac drive. If you want to move the files back and forth between them, you'll need to establish the format of the Windows external. Macs can read and write to FAT32 and exFAT (which can handle files larger than 4GB) formatted drives but can only read NTFS formatted drives without additional software. With the Windows drive connected, you can check its format with Disk Utility.

  • Why has this occurred The file "iTunes Library.itl" does not appear to be a valid iTunes library file. iTunes has created a new iTunes library and renamed this file to "iTunes Library (Damaged)".

    My itunes library suddenly that the file “iTunes Library.itl” does not appear to be a valid iTunes library file. iTunes has created a new iTunes library and renamed this file to “iTunes Library (Damaged)”. I use an external hard drive for my Itunes.

    The library file appears to have been corrupted.  You say you use your external drive but is the whole library on it or only your media?  No matter, you will have to rebuild the library file:
    iTunes: How to re-create your iTunes library and playlists - http://support.apple.com/kb/ht1451

  • File Read and Write using File Adapter in Bpel

    In Bpel Process i am using File Adapter ( Schema is Opaque) for read and write the file contents. i am able do successful deployment and read, write function in first time deployment, after that again i tired to run the application, its not going to write the content of file, its only writing the file with out data's or content in that file.
    Please help me...
    Saravanan

    Hi Eric
    In my domain.log file having the following details. In this file im unable to find out what the exact problem. Please look at this and help me.
    <2008-01-22 18:25:42,024> <INFO> <default.collaxa.cube.compiler> validating "C:\product\10.1.3.1\OracleAS_1\bpel\domains\default\tmp\.bpel_BPELProcess2_1.1_298e83988d77b6640c33dfeec11ed31b.tmp\BPELProcess2.bpel" ...
    <2008-01-22 18:25:49,850> <INFO> <default.collaxa.cube.engine.deployment> <CubeProcessFactory::generateProcessClass>
    Process "BPELProcess2" (revision "1.1") successfully compiled.
    <2008-01-22 18:25:49,914> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> Loading JCAActivationAgent for {portType=Read_ptt}
    <2008-01-22 18:25:49,914> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> JCAActivationAgent::load - Locating Adapter Framework instance: OraBPEL
    <2008-01-22 18:25:49,930> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> JCAActivationAgent::load - Done loading JCAActivationAgent for processId='bpel://localhost/default/BPELProcess2~1.1/
    <2008-01-22 18:25:49,930> <INFO> <default.collaxa.cube.engine.deployment> Process "BPELProcess2" (revision "1.1") successfully loaded.
    <2008-01-22 18:26:02,698> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> JCAActivationAgent::uninit Shutting down the JCA activation agent, processId='bpel://localhost/default/BPELProcess2~1.0/', activation properties={portType=Read_ptt}
    <2008-01-22 18:26:02,698> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> Adapter Framework instance: OraBPEL - performing endpointDeactivation for portType=Read_ptt, operation=Read
    <2008-01-22 18:26:02,698> <INFO> <default.collaxa.cube.ws> <File Adapter::Outbound> Endpoint De-activation called in adapter for endpoint : D:\MAXIMUS_Project_Softwares\jdevstudiobase10132\jdev\mywork\MyLabs\BPELProcess2\in
    <2008-01-22 18:26:02,698> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> JCAActivationAgent::init - Initializing the JCA activation agent, processId='bpel://localhost/default/BPELProcess2~1.1/
    <2008-01-22 18:26:02,698> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> JCAActivationAgent::initiateInboundJcaEndpoint - Creating and initializing inbound JCA endpoint for:
    process='bpel://localhost/default/BPELProcess2~1.1/'
    domain='default'
    WSDL location='rd.wsdl'
    portType='Read_ptt'
    operation='Read'
    activation properties={portType=Read_ptt}
    <2008-01-22 18:26:02,698> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> Adapter Framework instance: OraBPEL - endpointActivation for portType=Read_ptt, operation=Read
    <2008-01-22 18:26:02,730> <INFO> <default.collaxa.cube.activation> <File Adapter::Inbound> Endpoint Activation called in File Adapter for endpoint: D:\MAXIMUS_Project_Softwares\jdevstudiobase10132\jdev\mywork\MyLabs\BPELProcess2\in
    <2008-01-22 18:26:02,730> <INFO> <default.collaxa.cube.activation> <AdapterFramework::Inbound> Adapter Framework instance: OraBPEL - successfully completed endpointActivation for portType=Read_ptt, operation=Read
    <2008-01-22 18:26:02,890> <WARN> <default.collaxa.cube.activation> <File Adapter::Inbound> PollWork::run exiting, Worker thread will die
    <2008-01-22 18:26:04,171> <INFO> <default.collaxa.cube.ws> <File Adapter::Outbound> Managed Connection Created
    <2008-01-22 18:26:04,171> <INFO> <default.collaxa.cube.ws> <File Adapter::Outbound> Connection Created
    <2008-01-22 18:26:04,171> <INFO> <default.collaxa.cube.ws> <File Adapter::Outbound> FileInteraction Created

  • Itunes library.itl file cannot be found or created. The default location for this file is in the "iTunes" folder in the "Music" folder

    itunes library.itl file cannot be found or created. The default location for this file is in the "iTunes" folder in the "Music" folder, I keep getting this message everytime I try to install Itunes after having to remove windows 8.1 and go back to windows 8.  I have uninstalled and reinstalled and it keeps telling me the same thing. What do I do?

    Hi Renee31,
    If you are having issues with the library.itl file in iTunes, you may find the following articles helpful:
    iTunes: What are the iTunes library files?
    http://support.apple.com/kb/ht1660
    Apple Support: Removing and reinstalling iTunes and other software components for Windows Vista, Windows 7, or Windows 8
    http://support.apple.com/kb/HT1923
    Regards,
    - Brenden

  • The iTunes Library.itl file cannot be found or created. The default location for this file is in the "i Tunes" folder in the "Music" folder. PLEASE HELP!!!

    I have been trying for over 2 weeks now trying to get my iTunes to open up so I can use my iTunes but I have not had any luck since I up graded to the new itunes. I have tried uninstalling (all apple programs) and reinstalling and nothing seems to work. Everytime I try to open my iTunes I receive the following message - The iTunes Library.itl file cannot be found or created. The default location for this file is in the "i Tunes" folder in the "Music" folder. Please help as I'm getting very frustrated and want to throw every apple device in the trash!!!
    FYI: I thought about getting the iPhone but since I"m not having very good luck getting assistance with my iPod/iTunes I think I will rethink my decision.

    Found the solution.

  • I can't open my Itunes keep receiving error message- The iTunes Library.itl file cannot be found or created. The default location for this file is in the "i Tunes" folder in the "Music" folder

    I can't open my Itunes keep receiving error message- The iTunes Library.itl file  cannot be found or created. The default location for this file is in the "i Tunes" folder in the "Music" folder. I have even tried removing and reinstalling iTunes and it still does work because I continue to receive this error message. Please Help!

    Anyone can help to advice how to solve this issue ?

  • I keep getting the following message The iTunes library cannot be found or created. The default location for this file is in the iTunes folder in the music folder.

    I keep getting the following message The iTunes library cannot be found or created. The default location for this file is in the iTunes folder in the music folder. All of my playlists have disappeared and I am trying to recover them however I now can't even open iTunes!

    Hi crazymaiden,
    I have two articles for you here that should be able to help you troubleshoot this issue with your ability to launch iTunes:
    Troubleshooting iTunes installation on Mac OS X
    http://support.apple.com/kb/HT2311
    iTunes: How to re-create your iTunes library and playlists
    http://support.apple.com/kb/HT1451
    Take care, and thanks for visiting the Apple Support Communities.
    -Braden

  • Rreading and writing this file's metadata (xmp) has been disabled. Renders unplayable/corrupted files

    Hi everyone,
    I've been using Premiere Pro CC for a while now, as I have a YouTube channel, and I've rendered, exported and edited around a dozen different videos, and have had no issues until now. I haven't done anything to my settings, but randomly, I am now unable to export any videos. I've tried exporting multiple times, using different formats, and I still get the same error:
         File importer detected an inconsistency in the file structure of [FILE NAME]. Reading and writing this file’s metadata (XMP) has been disabled.
    I've looked around, and I've seen just about no help in fixing the issue, so I was hoping that someone could help me out.
    Some of my computer details are:
    Premiere Pro CC
    Toshiba Z10T
    Windows 8.1
    Hopefully that's enough to go by
    Thanks guys

    After moving LR3.5 and catalogs to a new computer running Windows 7 and LR from an XP system, I suddenly had 6400 files with exclamation points--failure to synch metadata or similar warnings on every file.  If I thought I had one without the error, if I clicked on it--there error showed up.    It was very frustrating and I was wondering if I was going to have to keep my old computer forever.
    After I searched for a couple of hours and found that there have been several discussions concerning failures to sync xmp sidecar files and failures to write metadata on this forum and others.  Some of the postings had angry rants against Adobe.  On my system, it was not Adobe that should be blamed, it was Microsoft:
    On one of the Adobe threads, I noticed a post that the person had multiple external drives and the problem did not show itself on one drive.  The person said that changing the permissions on the other hard drives solved the problem.   I tried this and it worked for me.  I did not have this problem with this same drive on my XP system.  I will try to find that other forum and thank the person who gave me the solution.

  • I'm trying to share folder between users on a single mac.  I put the folder in "shared," set permissions so other user can read and write, enabled file sharing, but can't find the folder on the second user's account.  Any help?

    I'm trying to share a folder between users on a single mac.  I want both users to be able to read and write so the folder stays current on both accounts.  I put the folder in "shared," set permissions on folder so other user can read and write, enabled file sharing, but can't find the folder on the second user's account.  Any help?

    Did you log out of one account and into the other or just used Fast user switching?
    Is the permissions set to anyone?
    When you move data to teh Shared folder is it copied or just moved?
    If copied then it's not a folder both can access, just a way station like a USB thumb drive that things are coped too and off of likely.
    You can run this #5 on each user account to reset the user permissions once they are taken back out of the Shared folder
    Step by Step to fix your Mac

Maybe you are looking for

  • Field length misreported in application with AL32UTF8 database

    Hi all, I have the following problem with two different applications running against an Oracle 10.2 database with AL32UTF8 character set. The tables in the database are created using NLS_LENGTH_SEMANTICS=CHAR. The problem is that in the applications,

  • PC doesn't communicate with HP Color Laserjet 1600

    Hi, my Laserjet 1600 printer worked fine with my old pc. Now i have a new pc with Windows 7 ultimate 64 bit and somehow it can't communicate with my printer. Printer does work. cable isn't broken. Driver won't install either. What can be the problem?

  • Building RULES file to load shared members in Aggregate storgage outline

    Hi there I posted this yesterday (sorry, somewhat different subject description). I am trying to create an alternate hierarchy containing shared members using a load rule and flat file in an Aggregate Storage outline. One response was to remove the "

  • Fault information from bpel process when exception happens

    How to extract all the fault information from bpel process when exception happens? Are there any other methods other than "ora:getFaultName(), ora:getFaultAsString()" When I use "ora:getFaultName(), ora:getFaultAsString()", I get the below message,so

  • Recursive WITH (Recursive Subquery Factoring) Never Returns

    11.2.0.2 database on Windows, SQL Developer Version 3.2.20.09, build MAIN-09.87 (Database and SQL Developer are on the same machine. I have also tried connecting to a Linux 11.2 database and have the same results.) I've been doing some simple testing