How do I get the byte size of a server file before sending output via HTTP?

I need to get the byte size of the file prior to streaming it. I can't seem to find a class/method I need. Basically, I have the path c:\\tomcat\\webapps\\documents\\sample.pdf in the servlet, I was hoping I could just use something from the File class but I couldn't find anything that seems to do the trick?
thanks, in advance,
Chuck

maybe the source of the problem will help...I am trying to stream a PDF to IE and a blank page is being generated although all other file type work.
I have found a lot of answers in the forum but no specific code examples. Here's what I have so far from picking through threads in here (can someone please show me how to get the byte size of the file so that I can assign it to the method response.setContentLength();?):
String CONTENT_TYPE = " ";
     String target = " ";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
     StringBuffer buf = new StringBuffer();
     HttpSession session = request.getSession();
     String file = request.getParameter("filename");
     target = file;
     int end = file.length();
int beg = end-2;
     String type = file.substring(beg, end);
     if (type.equals("DOC")){
          CONTENT_TYPE = "application//vnd.msword";
     }else if (type.equals("XLS")){
          CONTENT_TYPE = "application//vnd.x-excel";
     }else if (type.equals("PPT")){
          CONTENT_TYPE = "application//vnd.ms-powerpoint";
     }else if (type.equals("PDF")){
          CONTENT_TYPE = "application//vnd.x-pdf";
     }else if (type.equals("MPP")){
          CONTENT_TYPE = "application//vnd.ms-project";
     }else if (type.equals("ZIP")){
          CONTENT_TYPE = "application//ZIP";
     }else if (type.equals("TXT")){
          CONTENT_TYPE = "text//plain";
     }else {
          CONTENT_TYPE = "text//html";
     //File f = new File(file);
     //int l = f.length();
     response.setContentLength(l); <----- supposedly this fixes my problem but I don't know how to get the byte szie of the file in an integer??
     // reset the response
     response.reset();
     response.setContentType(CONTENT_TYPE);
     try{
     // Get streams
     FileInputStream fileInputStream = new FileInputStream(target);
     ServletOutputStream servletOutputStream = response.getOutputStream();
     // Init byte count and array
     int bytesRead = 0;
     byte byteArray[] = new byte[4096];
     // Read in bytes through file stream, and write out through servlet stream
     while((bytesRead = fileInputStream.read(byteArray)) != -1) {
     servletOutputStream.write(byteArray, 0, bytesRead);
          servletOutputStream.flush();
     // Flush and close streams
     servletOutputStream.flush();
     servletOutputStream.close();
     fileInputStream.close();
     } catch (Exception e) {
     System.out.println(e.toString());

Similar Messages

  • How do i get the approximate size of back up file required to save on disk using sql query because i want to show the required sapce for backup on my application

    hi i am face with problem that is i want to show the how much approximate space required for backup or .bak file to get backup using sql query because i want to show the required memory for backup file on my java application  

    Hi FIROZ
    TENNALI
    Is this still an issue, or can we close the thread?
    * closing a thread by marking the answer/s (there is a link beneath each response) and you can vote for useful responses as well (there is a link to vote on the left of each response)
      Ronen Ariely
     [Personal Site]    [Blog]    [Facebook]

  • How to get the byte[] size dynamically from the StreamMessage object

    Hi all,
    Using JMS, I am receiving the FDF file as StreamMessage from the queue and attaching it to the PDF file which is present in the local system.
    For that, I have written the code as follows:
    {color:#0000ff} Message msg = jmsTemplate.receive();
    if(msg !=null && msg instanceof StreamMessage)
    StreamMessage message = (StreamMessage) msg;{color}
    {color:#ff6600}//hardcoded the byte array size value
    {color}{color:#0000ff} byte[] bytes = new byte[{color:#ff0000}856{color}];
    System.out.println("read msg="+message.readBytes(bytes));{color}
    {color:#0000ff}PdfReader pdfreader = new PdfReader("D:\\Managing_Workflows_No_Comment.pdf");
    PdfStamper stamp = new PdfStamper(pdfreader, new FileOutputStream("D:\\12345.pdf"));
    FdfReader fdfreader = new FdfReader(bytes);
    stamp.addComments(fdfreader);
    {color} {color:#0000ff} stamp.close();
    {color}{color:#000000}The above code is working fine except with the hardcoded of {color:#ff0000}byte array{color}{color:#ff0000} size value{color} which is given in {color:#ff0000}RED{color} in color.
    Can anybody know, {color:#000000}*how to get the byte[] size dynamically from the StreamMessage*{color} object ?
    Any help would be highly beneficial for me !!!!
    Thanks and Regards,
    Ganesh Kumar{color}

    When you create your stream message you could add an property to your message, something like streamSize that would contain the number of bytes in your stream message. Then you could get this property from the message before declaring your array.
    Tom

  • In iPhoto most of my photos appear to have been replaced by much smaller versions. What has happened and how can I get the full size photos back?

    In iPhoto most of my photos appear to have been replaced by much smaller versions. What has happened and how can I get the full size photos back?

    That is either a bug in Firefox 5 or the script has a problem with Firefox 5.<br />
    It works in all other Firefox versions like Firefox 3 and 4 and 6 (Aurora) and 7 (Nightly).

  • How do I reduce the "KB" size of a photo to better send on e mail?

    How do I reduce the "KB" size of a photo to better send on e mail?

    Where is the image?  What software are you using?  What software do you have?
    (If it is iPhoto or Aperture, you might get better answers in those forums:
    Aperture
    iPhoto

  • I have a problem with mail.  the spelling and grammer check box before sending the messege is no longer there.  I did everything but cannot get it back.  is ther anyone who knows how to get the box with spelling and grammer checks before sending

    i have a problem with mail.  the spelling and grammer check box before sending the messege is no longer there.  I did everything but cannot get it back.  is ther anyone who knows how to get the box with spelling and grammer checks before sending the mail.
    Also the mail is acting very funny by not getting the rules work in a proper method.  Is ther a software to repair mail.

    i did both of them, but still the while sending the mail the diolog box is not showing up and also the spelling and grammer does not do the spelling check. 
    This problem just started for about 3 to 4 days now.  earlier it was working normally.

  • C Programming: How can we get the filesystem name for a given file-path?

    C Programming: How can we get the filesystem name for a given file-path?
    Say I have a filepath=/mnt1/file1
    Using some OS API like stat, can I get the Filesystem /mnt ?
    Thanks in advance,
    -V

    Enter the command up to the point of entering the file path and add a space, then drag the file into the terminal window. It will fill out the path.
    If you need to go further into the contents of the Application package, you can continue with /Contents...
    Another way is to start typing and then hit Tab to auto-complete. It will stop where it can't determine the next letter.
    So, type /App tab and it will fill in /Applications. Type a / and start with the name of the app, then tab and it should complete. Continue till you have the correct path.
    Spaces will be replaced with \<space>, so, App Store would end up as /Applications/App\ Store.app

  • How do I get the full size of my pics or video into the time line?

    Greetings,
    I've nener been part of a forum before and an wondering if this will actually be taken seriously. I'm new to video editing and know little about the program I am using (Adobe Premiere Pro CC). Would someone please advise me as to how I can add the full size video or pics to the time line. It appears whenever I add something from my Gopro 3 HD camera I only get a partial of the video / pic.
    Regards,
    The Powered Parachute Guy

    Click the NEW ITEM link on reply page #5 in the PPro/Encore tutorial list in message #3 http://forums.adobe.com/message/2276578 may also help, with more help in message #5
    Also read below...
    Some of this is from the Premiere Elements forum, but the idea of sizing pictures is the same for PPro or Encore
    Pictures that are "overly" large may cause video editing problems
    Photo Scaling for Video http://forums.adobe.com/thread/450798
    -HiRes Pictures to DVD http://forums.adobe.com/thread/1187937?tstart=0
    -PPro Crash http://forums.adobe.com/thread/879967

  • How do you get the font size back to normal on a website?

    The size of my fonts on my hotmail acct are tiny. Don't know how it happened, & I can't get it back. Using the "Tools" menu, to attempt to change fonts, changes it for all sites. How do I change the font size back for just one website? Can anyone help?

    Reset the page zoom on pages that cause problems: <b>View > Zoom > Reset</b> (Ctrl+0 (zero); Cmd+0 on Mac)
    * http://kb.mozillazine.org/Zoom_text_of_web_pages

  • How do I get the computer to automatically eject a drive before it enters Sleep?"

    I have an external eSATA drive connected via PCI Express card to my Mac Pro.  When I wake the computer, I get a complaint about not correctly ejecting the disk.  How can I get the computer to eject the drive before entering Sleep?  There are two partitions on the drive, so the answer will need to include "Eject All."  Perhaps an Apple Script could tell the drive to eject, but how to automatically run it?  If I could schedule it to run five or ten minutes before the scheduled Sleep, that should work.
    The reason for the external drive is to back up my TiVo recordings...WARNING...If you have a DVR Expander connected to your TiVo and EITHER fails, you lose everything!  I now transfer all my recordings from my TiVos to this drive so I won't lose recordings in the future.  Plus I can take the drive with me and connect it via a Thunderbolt wire and watch them on my laptop.

    Hello,
    http://hints.macworld.com/article.php?story=20120211184732735
    http://irwinkwan.com/2013/06/27/eject-all-mac-os-x-disks-with-a-script/

  • How do I get the java code from a class file ?

    I want to know how to findout the java code from a .class file . Is there any utility or any way to get the java code from a .class file. If so , please let me know the step by step procedure .
    Thanks
    Shiva

    Check out Mocha (http://www.brouhaha.com/~eric/computers/mocha.html)
    It's free!

  • How can I get the touching size from TouchPoint class

    I built a WPF project
    MainWindow.xaml:
    <Window x:Class="TouchSize.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" TouchMove="Window_TouchMove" WindowState="Maximized" TouchDown="Window_TouchMove">
    <Grid>
    <TextBox x:Name="TextBoxOutput" TextWrapping="Wrap" Text="TextBox" Padding="5" Margin="5" IsReadOnly="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/>
    </Grid>
    </Window>
    MainWindow.xaml.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    namespace TouchSize
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    public MainWindow()
    InitializeComponent();
    private void Window_TouchMove(object sender, TouchEventArgs e)
    TouchPoint tp = e.TouchDevice.GetTouchPoint(this);
    I got the TouchPoint while i touching the touch screen, and i want to get the size of the Touching point.
    I tried tp.Size but is zero.
    Thank you.

    If the Size is always 0 in a TouchUp event handler, your touch screen hardware may be the issue. Appearantly some touch screens report sizes in a non-standard way and then you cannot really get the size of the TouchPoint. Please refer to the following
    link for more information:
    http://nuigroup.com/forums/viewthread/10979/#59933.
    Please also remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question.

  • How do I get the pixel size at 100% on Photoshop CS6 to match the pixel size that will appear on web

    I have a 500px x 500px document open but the size that it looks on Photoshop at 100% does not match the size that is 500px on the web. How do I change it on Photoshop CS6 so that it will give me an accurate display of the pixel size?
    (I don't know if this needs to be mentioned but I am working on the new macbook pro and just downloaded the new photoshop update for retina displays).
    Thank you in advance!!!

    Ask in teh PS forum. Generally, though, pixels are pixels. Perhaps you simply have things backwards - as far as I know by default Safari doubles image sizes on Retina devices, which can lead to such discrepancies. There's an option for that in the prefs.
    Mylenium

  • How do i get the print size to enlarge on an email print out?

    Everything I send from my lap top to be printed is so small I can hardly read it. How do I enlarge the print out?

    What model of printer are you using?
    Also are you printing directly from your computer or using ePrint?
    -------------How do I give Kudos? | How do I mark a post as Solved? --------------------------------------------------------

  • How can I get the Voice memo's source computer file to keep the original file name?

         I ran out of space in my iPhone some weeks ago and I've been cleaning up. I am pretty much done deleting the photos and apps. The only thing I need to finish is erasing my voice memos. I already synced my music/voice memos with my computer, the only problem is that when I get the files from the iTunes media it changes the name and makes it extremely hard to organize. I want to upload this files to another website so I can access them freely.
    I would REALLY appreciated if anyone could tell me how to fix this or if you have another method please tell me.

    This is pretty straightforward.
    If the file isn't too big you can just read the entire file contents:
    set fileContents to read file "HD:path:to:the:file.txt"
    Once you've read it in, you can iterate through it with a repeat loop like:
    repeat with eachParagraph in (paragraphs of fileContents)
      -- code here to process each line
    end repeat
    Within the loop, eachParagraph represents each successive line in the file, so all you need to do is pull out the data you want. Now your subject says that your file is tab-delimited, but your example uses commas, so you may need to adjust this script depending on what's actually the case:
    set my text item delimiters to tab -- or "," as appropriate
    set firstName to text item 1 of eachParagraph
    set lastName to text item 2 of eachParagraph
    set emailAddr to last text item of eachParagraph
    -- rest of code here to send an email
    The last thing to do is to reset the 'text item delimiters' to their previous value so you don't end up getting confused.
    So, putting that all together you end up with something like:
    set fileContents to read file "HD:path:to:the:file.txt"
    set oldTIDs to my text item delimiters -- additional code to remember the current TIDs
    set my text item delimiters to tab -- or ","
    repeat with eachParagraph in (paragraphs of fileContents)
      set firstName to text item 1 of eachParagraph
      set lastName to text item 2 of eachParagraph
      set emailAddr to last text item of eachParagraph
      -- rest of code here to send an email
    end repeat
    set my text item delimiters to oldTIDs -- restore the TIDs for cleanliness
    Note that this should work for reasonably-sized files (up to a few thousand records). If it's much more than that you may need something a little more sophisticated to read the file progressively rather than all at once.

Maybe you are looking for

  • HP Officejet 150 mobile, HP scan does not work (Can not find scanner)

    Hi, I just bought a HP Officejet 150 Mobile All-in-One. I installed the complete software package (downloaded from HP Site) with bluetooth on a windows 7 Acer Aspire S5 laptop. Printing seems to work fine, but when starting HP Scan, i get the followi

  • No longer able to boot into OSX

    My Mac Mini will only boot to Windows XP (I realise this situation is not without some degree of irony). Bootcamp has been installed for several months and I have had no problems booting between OSX and XP. I have not booted into XP recently only OSX

  • Time capsule....cannot backup

    I just purchased my macbook pro and have started switching over from PC world to mac world, I have an iphone and ipad so it was just a matter of time.  What I did purchase was a timecapsule to store all the info my husband and I have from our old PC'

  • Issue on Function Module

    Hi Gurus I have an issue with function module, i am passing a filepath to the function module which is like this /temp/folder/AFILE.csv. to pull the data from application server. When ever it enters to the function module it is changing the file name

  • Weird Unix-files in the waste-bin

    I have a fairly new MacBook and just found a couple of weird looking unix-files in my waste-bin. Obviously the names are quite odd, not to talk about the dates they were supposedly last changed. I'm pretty sure, those are no files/programmes I delete