Problem with downloading a PDF file from a webserver run on Linux

Hi,
I've written a simple functionality that manages file attachments.
Everything works fine (attaching, downloading, deleting) when the webserver runs on Windows.
However when I deployed the code to the Resin webserver run on Linux and use the Win browser to connect to the app, the downloading of PDF file doesn't work (uploading and downloading of txt, doc, xls, jpg files is OK).
The downloaded PDF file is almost twice as big as original (~28KB when original is ~12KB) and it can't be open.
I guess it is the problem of writing to the output stream of HttpServletResponse but I can't localize the problem.
Here is the code I use for downloading file:
private boolean downloadFile(HttpServletResponse response, String filePath,
               String originalFilename) {
     File file = new File(filePath);
     String contentType = URLConnection
               .guessContentTypeFromName(originalFilename);
     // If the content type is unknown set the default value.
     if (contentType == null) {
          contentType = "application/octet-stream";
     BufferedInputStream input = null;
     BufferedOutputStream output = null;
     try {
          input = new BufferedInputStream(new FileInputStream(file));
          int contentLength = input.available();
          response.reset();
          response.setContentLength(contentLength);
          response.setContentType(contentType);
          response.setHeader("Content-disposition", "attachment; 
                       filename=\""+ originalFilename + "\"");
            output = new BufferedOutputStream(response.getOutputStream());
          int bufSize = 10000;
          byte[] buf = new byte[bufSize];
          int bytesNo = 0;               
          while ((bytesNo = input.read(buf, 0, bufSize)) != -1) {
               output.write(buf, 0, bytesNo);
          output.flush();
          input.close();
          output.close();
     } catch (IOException e) {
          log.debug(e.getMessage());
          e.printStackTrace();
}Can you point any problem?
Thanks in advance,
Ala

matali wrote:
          int bufSize = 10000;
          byte[] buf = new byte[bufSize];
          int bytesNo = 0;               
          while ((bytesNo = input.read(buf, 0, bufSize)) != -1) {
               output.write(buf, 0, bytesNo);
This piece is completely wrong and doesn't work for files bigger than 10000 bytes. Replace it by
byte[] buffer = new byte[10240]; // 10KB exactly.
int length = 0;
while ((length = input.read(buffer)) > 0) { // Read next 10KB of input to buffer.
    output.write(buffer, 0, length); // Write specified length of buffer to output.
}Or just use output.write(input.read()) in a loop of the contentLength as you're alredy using BufferedInputStream/BufferedOutputStream.
Another thing, the method is declared to return a boolean, but it actually doesn't? I would just let it throw an exception in case of failure instead of returning a boolean and let the calling method handle the exception.

Similar Messages

  • How to download a .pdf file from a JavaMail application?

    hi to all,
    iam unable to download a .pdf file from a mailserver using
    my javamail application.i have downloaded all kind of files except .pdf.
    my code snipnet:
    public static void dumpPart(Part p) throws Exception {
    //     if (p instanceof Message)          dumpEnvelope((Message)p);
              System.out.println("CONTENT-TYPE: " + p.getContentType());
              Object o = p.getContent();
              if (o instanceof String) {
                   System.out.println("This is a String");
                   System.out.println((String)o);
                   } else if (o instanceof Multipart) {
                        System.out.println("This is a Multipart");
                        Multipart mp = (Multipart)o;
                        int count = mp.getCount();
    System.out.println("****************************************");
    System.out.println("count" + count);
    System.out.println("****************************************");
                        for (int i = 0; i < count; i++){
                             System.out.println("CONTENT-TYPE: " + p.getContentType());
                             dumpPart(mp.getBodyPart(i));
                        } else if (o instanceof InputStream) {
                             System.out.println("This is just an input stream");
                             InputStream is = (InputStream)o;
                             String filename=System.currentTimeMillis()+".exe     ";
                             FileOutputStream fos=new FileOutputStream(filename);
                             int c;
                             while ((c = is.read()) != -1)
                                  //System.out.write(c);
                                  fos.write((char)c);
    }//dumpPart
    what's wrong with this code?
    Nagaraju.G

    fos.write((char)c);Converting the byte to a char is unnecessary and probably harmful. Just do this: fos.write(c);

  • Problem with saving a pdf file to computer. Continually get an error message " This document could not be saved. There was a problem reading this document (21).

    Need advice on a saving file issue. I'm having problem with saving a .pdf file to computer. Continually get an error message " This document could not be saved. There was a problem reading this document (21). This is new as this error message just recently started to pop-up.

    More information about this issue can be found here:
    https://forums.adobe.com/thread/1672655
    A "quick" fix that worked for me was to uninstall Adobe... then download the base install for Adobe Reader 11.0.
    Then download each of the individual updates and run them sequentially. 
    I've installed back up to the last security update which is version 08 and have been able to do normal Save As operations.
    You will have to disable automatic updates in order to stay at version 08 until Adobe resolves this issue in a later release.
    http://www.adobe.com/support/downloads/product.jsp?product=10&platform=Windows
    Adobe Reader 11.0 - Multilingual (MUI) installer    AdbeRdr11000_mui_Std
    Adobe Reader 11.0.01 update - Multilingual (MUI) installer    AdbeRdrUpd11001_MUI.msp
    Adobe Reader 11.0.02 update - All languages    AdbeRdrSecUpd11002.msp
    Adobe Reader 11.0.03 update - Multilingual (MUI) installer    AdbeRdrUpd11003_MUI.msp
    Adobe Reader 11.0.04 update - Multilingual (MUI) installer    AdbeRdrUpd11004_MUI.msp
    Adobe Reader 11.0.05 security update - All languages    AdbeRdrSecUpd11005.msp
    Adobe Reader 11.0.06 update - Multilingual (MUI) installer    AdbeRdrUpd11006_MUI.msp
    Adobe Reader 11.0.07 update - Multilingual (MUI) installer    AdbeRdrUpd11007_MUI.msp
    Adobe Reader 11.0.08 security update - All languages    AdbeRdrSecUpd11008.msp

  • How to download a pdf file from the server from an strut application?

    Hi,
    I wan to download a pdf file from the server side to local system. Please help me how i use the down load option.

    Read up on the Struts download action [1].
    And next time, please post in the relevant forum, one of the web-tier ones [2].
    [1] http://wiki.apache.org/struts/StrutsFileDownload
    [2] http://forum.java.sun.com/category.jspa?categoryID=20
    People on the forum help others voluntarily, it's not their job.
    Help them help you.
    Learn how to ask questions first: http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
    ----------------------------------------------------------------

  • How do i download a pdf file from an e-mail? save to computer did not work.

    How do i download a pdf file from an email? Save to computer did not work.

    What is your operating system?  What is your email client?  What exactly means "did not work"?
    Most email clients I know have a 'Save Attachments' functionality; did you try that?

  • How do I move folders with subfolders (containing pdf files) from my pc to ipad 3 to use in adobe reader

    How do I move folders with subfolders (containing pdf files) from my pc to ipad 3 to use in adobe reader

    You need to connect the ipad to the computer and open itunes.
    Open the ipad and go into settings, icloud. Tap on "Documents and Data" and turn that on to sync to icloud.
    Now go back into itunes and it should pick up your ipad already and tap on the ipad connection. There will pop up some taps on the center screen for sync options. Go thru each one (summary, info, apps, music, tv shows, podcasts, itunesU, books and photos).....you want to click on "Books".
    To the left click the arrow next to the ipad under Devices.
    The arrow will point down and open the sub menu, click on the "Books" sub menu here. You will see in the center window current books, PDF currently on the ipad.
    Next go to the top and click on "File"  (file, edit, view, controls, store, advanced, help) and click on "add folder to library"...... it will bring up your computer file system in a seperate window. File the folder your looking for and add it. This should get the folder set up to sync onto ipad thru itunes.
    For additional ease in doing things on the middle window of itunes click back on summary, scroll a bit and you will see "Backup", click on "Backup to icloud". Then under "Options", check "Sync with this ipad over WiFi".
    This will allow you to wirelessly sync your itunes and your ipad without having the ipad connected to the computer with a cable. You just have to be on the same wireless network.
    If in itunes you add something like a pdf or photo, when your on your ipad open the Ibooks or Iphoto app and it wull update the file system to match your itunes account. The sync with with anything you do in itunes and it works the other way around also. If you save files, photos, books on the ipad, using the ipad (app store purchases, games, books)....the ipad will update to itunes next time your on the same network.
    FYI - both the ipad and the computer have to be on.
    Itunes updates (changes you make thru itunes), does not need itunes to be open for update content to update to the ipad. This is because it updates to the cloud.
    Updates from the ipad to itunes will need itunes open on the computer. So it can sync with the cloud info.

  • Downloading a pdf file from javascript() window does not work

    PDF download/view works fine where the link points to actual pdf file. But I have problem downloading/viewing pdf files that are loaded using javascript(), e.g. bank statements and such. When I click a link FF downloads a file which either has no extension or has a different one (not .pdf).
    Later I realized that I can manually rename the downloaded file to insert .pdf extension and open it in reader.
    How can I solve this? I don't have this problem when using IE or Chrome.
    Thanks

    Could you try a new profile?
    Create a new profile as a test to check if your current profile is causing the problems.
    See "Creating a profile":
    https://support.mozilla.org/kb/profile-manager-create-and-remove-firefox-profiles
    http://kb.mozillazine.org/Standard_diagnostic_-_Firefox#Profile_issues
    If the new profile works then you can transfer some files from an existing profile to the new profile, but be cautious not to copy corrupted files to avoid carrying over the problem.
    http://kb.mozillazine.org/Transferring_data_to_a_new_profile_-_Firefox
    If it still happens with a new and clean profile then the most likely cause is that you have security software that is blocking this JavaScript.

  • Problem with creating/viewing PDF's from InDesign CS3

    I have a problem with our PDF workflow and just cannot seem to resolve it.
    The problem is as follows: My coworker and I (both designers running CS3 on iMac's running 10.5.6 Leopard) work daily on producing documents and graphic layouts.
    Internally we can view and print PDF documents we create just fine with no troubles with the exception of our supervisor, who is running a mac with Tiger operating system. Our office environment is both Mac and PC. On may occasions he cannot print PDF's we create. Many times his prints will contain garbled characters, drop italics and formatting, replace fonts, or just print slowly.
    This problem is also happening to our editor who is offsite. This is a fairly serious problem for her, considering her job relies heavily on being able to view and open PDF files we create. She was able to send a PDF file which shows the garbled mess her printer spit out when she printed. Apparently there were pages upon pages of messy garbled text. When documents do print from her, they are usually very slow in printing, taking up to a minute or more to print each page.
    The sample of what she sent me is attached, and can also be found on my MobileMe iDisk at: http://public.me.com/rlcollier (document entitled Print Results.PDF)
    My question really to the community is obviously what might be causing these problems. Its very frustrating not being able to determine if its something we're doing ourselves thats causing some incompatability or corruption in these files, or if its the users systems themselves. I can say that Debra our editor has can have a garbled mess of a 4 page file from us, and then turn around and print a graphic heavy 90 page PDF with ease from Boeing. Our PDF's seem to be the only ones she struggles with. That being said, my inclination is that its something on our end.
    Any ideas of where to start looking? Any help at all would be greatly appreciated and welcomed. Thanks!

    I currently had our editor test printing of some of our files using both Foxit and Adobe Reader (as was suggested) in order to see if either made a difference in her printing ability and here is what she came back with:
    I tried to print out both these pdfs (David's is the one you reworked and Lisa's HESSM-3, both sent yesterday).
    With Adobe:  David's first page printed quickly, but it had errors (part of his pants didn't print, and there's an arbitrary shaded box in the text).  Page 2 didn't print--every time I tried it had a different "offending command" code.  Printing Lisa's HESSM  made it up to page 7 before problems showed up (stock photo only partially printed), and it stopped on page 8 (with the random "offending command" code).
    With Foxit:  Both David's and the HESSM pdfs printed completely and without error...but it took a long time.   David's 2 pages took about 3 to 4 minutes, and HESSM's 16 pages took close to 20 minutes.  The time is in the transfer of data to the printer; the physical printing  goes pretty quickly.
    I cant say that I believe email is the problem, although I cant rule it out. I've tested emailing vs. passing through our workgroup with my supervisor, and it does not make any difference in his ability (or lack of ability) to print our files. He was able to print to a different printer (an HP 4650 as opposed to a 4100) without troubles. He refuses to believe its a printer problem however because PDF files originating from our office are the only ones he has trouble with. Never has he had any trouble with a single PDF file produced from any other source. This is also the case for our editor who only has trouble with PDF files originating from either mine, or my coworkers systems.
    PS: I've attached both files that were referenced by our editor above for viewing/testing.

  • When wanting to download a PDF file from a site, when cliicking on the link, Firefox 3.6.13 goes to a blank page. Previous versions worked perfectly. How do I fix this please?

    Since no window shows up asking where to download, etc as happens in other browsers (Opera) and in previous versions of Firefox, no able to click on a web site link to download a PDF file. Just goes to a blank page.

    Some firewalls like Kaspersky can block content and send a 1x1 GIF image instead.
    Kaspersky: Protection > Firewall > Filtration system: disable the "Banner Ad Blocker"
    See [[Configuring Kaspersky Internet Security]] and [[Firewalls]]

  • When downloading a pdf file from a link the page displays "done" but with no content displayed

    This relates to pdf document downloads from a website link. This has been a recent phenomenon. I have no idea as to the possible source of the problem. I discount an adobe problem. No problem downloading in explorer

    https://support.mozilla.com/en-US/kb/Using+the+Adobe+Reader+plugin+with+Firefox
    https://support.mozilla.com/en-US/kb/Opening+PDF+files+within+Firefox

  • Problems loading and saving pdf files from sites with latest version.

    On my utilities I wish to download and save pdf files of my e-bill.
    Lately when I click on getting bill, Firefox opens new tab and stops.
    with a light message in url place saying "Type a web address".
    To get around this I must do a right click on the save bill and
    select open in new window, then it opens a new copy of Firefox,
    and two tabs, with the second one asking me to use Adobe to see
    pdf file. I tell it to open and then save it and print it from the tab
    that opens with the pdf file in it. This never happened before was
    always able to just click on link and it would open new tab with
    pdf file there.

    Thanks for the replies. I don't think I was clear enough with my question.
    What I want to be able to do is to click on a PDF file in my Firefox browser and be able to save it as an Adobe PDF file, not a Preview PDF file, in any folder on my computer, rather than just the specified default location for downloads. This way I can save, for example, phone bills in the phone bills folder and bank statements in the bank statements folder without having to save them to the desktop, "Get Info" on each, and change the "Open with:" box from Preview to Adobe Reader.
    Fortunately, thanks to Michael's post, I found an add-on from Firefox that allows me to do just that: https://addons.mozilla.org/en-US/firefox/addon/636
    Thanks for your help. Now, within my Firefox browser, I can choose whether to view or download PDF files, always in Adobe rather than Preview, and when I save them I get the option to choose the location each and every time.
    MacBook Mac OS X (10.4.9)
    MacBook Mac OS X (10.4.9)

  • Problem with indexing a PDF file

    Hi all,
    We can most of the time successfully index PDF files, but sometimes it simply fails to do so. Nothing wrong with the code, but it looks to me that some PDF files are not accepted. Is there some sort of a list from Oracle which tells me what version of PDF is accepted or how they should be made so that Oracle can index the content?
    Nitai
    Message was edited by:
    Nitai

    Hi,
    Reproduced.
    I indexed 5 pdf's downloaded from the internet and yours. Your one pdf is the only one not indexed. All are version 1.4 (right-click | properties | pdf) which is fully supported (see http://www.verity.com/cms/groups/public/documents/collateral/mk0459.pdf for a list of supported formats for the keyview filter, referred to as auto_filter in 10g).
    The differences between the files are:
    Works:
    ============
    Application PScript5.dll Version 5.2
    PDF Producer: Acrobat Distiller 5.0.5 (Windows)
    Fast Web View: No
    Doesn't
    ============
    Application: Adobe InDesign CS2(4.0)
    PDF Producer: Adobe PDF Library 7.0
    Fast Web View: Yes
    I checked to see that the pdf was not simply a scanned image and it wasn't. No errors in CTX_USER_INDEX_ERRORS and the pending record disappears post index sync. No records in the $I table.
    A few possible things to check with support:
    1) If custom fonts are used check with Oracle support on the filter's ability to extract using the custom font.
    2) Are there any known issues with PDF docs generated using InDesign CS2 or with docs created for Fast Web View?
    See bug 3814696 and reference it with your service request. The outcome is not published so I don't know how/if they resolved it. You may want to mention this with your TAR since the scenario is similar, and toss in the differences between the "works" and "doesn't" scenarios that I mention above.
    Hope it helps,
    -Ron

  • Problems with Contribute3/Dreamweaver8 & PDF files

    Does anyone know of problems downloading PDF files that have
    been uploaded with either Contribute 3 or Dreamweaver 8 to a
    server. I seem to be able to read these files in FireFox but not
    IE6 - don't know what I am doing wrong - HELP !!

    May be that the acrobat reader association with IE has got
    corrupted. Why
    not try to download an updated reader, and install it.
    Paul Whitham
    Certified Dreamweaver MX2004 Professional
    Adobe Community Expert - Dreamweaver
    Valleybiz Internet Design
    www.valleybiz.net
    "mitchellbutt" <[email protected]> wrote in
    message
    news:eiaop2$4o0$[email protected]..
    > Does anyone know of problems downloading PDF files that
    have been uploaded
    > with
    > either Contribute 3 or Dreamweaver 8 to a server. I seem
    to be able to
    > read
    > these files in FireFox but not IE6 - don't know what I
    am doing wrong -
    > HELP
    > !!
    >

  • Problem with color in pdf files

    My documents contain color logo.
    Whenever converted into pdf file, I always see color logo in the pdf files.
    Today, the logo came out to be in black and white.
    I repeated many times even with Adobe PDF (printer driver).
    But logo came out in B & W.
    I am wondering how to fix this problem.
    I currently use WinXP Pro SP2-Acrobat Pro 7.10.
    NOte: Last week, v. 7.09 was updated to 7.10.
    I wonder this update contains a bug which produces B&W image.

    Please ignore this post.
    I would like to ask the moderator to delete this post.
    (note; I found that theproblem has nothing to do with pdf but with the original xls invoice douments).

  • CS2 problem with 5D MKII RAW files from Lightroom 2.2

    Ok, this one is weird. I have tried it on two different computers with the same resulting problem. If I convert a full size RAW 5D MkII to Tiff in the new Lightroom 2.2, then bring it into CS2 and covert it to 8-bit and try to save as JPG, it always comes up with a program error. I tried an sRAW file with the same process and it worked fine.
    Just to make sure I did have something else messed up, I converted in DPP to tiff and then went to CS2 and saved as JPG just fine.
    Anyone else try this and see a problem? Looks like it's a combination of a problem with the new Lightroom, plus CS2.
    It almost seems like CS2 doesn't have enough memory to convert the file to jpeg. I have had this program error before in CS2, but with much larger files. The 5D MKII should not overrun memory.

    This is a known bug in CS2 where it trips up because of the (valid) metadata written by Lightroom. You need to delete the xmp metadata written by Lightroom from the file before saving as jpg or export from lIghtroom with minimize metadata checked.

Maybe you are looking for