ParseException Content-Disposition filename spaces

Hello All,
I have an email generated from Thunderbird 1.5.0.9 (Windows/20061207) which contains an attachment whose filename has spaces.
JavaMail (1.4) throws a javax.mail.internet.ParseException on the MimeBodyPart.getDisposition() call:
javax.mail.internet.ParseException: Expected ';', got "-"
javax.mail.internet.ParameterList.<init>(ParameterList.java:179)
javax.mail.internet.ContentDisposition.<init>(ContentDisposition.java:87)
javax.mail.internet.MimeBodyPart.getDisposition(MimeBodyPart.java:1039)
javax.mail.internet.MimeBodyPart.getDisposition(MimeBodyPart.java:299) The offending attachment part has a part header which looks like this:
Content-Disposition: inline;
     filename*0=Test - Test.pdf
Clearly the parser is failing due to the spaces in the filename. Bugzilla for Thunderbird (Bug 221028 - https://bugzilla.mozilla.org/show_bug.cgi?id=221028) discusses this issue, however the status of the bug is VERIFIED WONTFIX.
According to the Mozilla discussion, their implementation is conformant to the RFC:
...Just two days ago I was talking about this issue in #mozillazine with Christian
Biesinger and Boris Zbarsky, and they said that this is the proper behaviour
according to the RFC...
Whether it IS conformant to all relevant RFCs I don't know, but it seems it won't be changed any time soon.
This issue also exists for the Content-Type header in the same email which has the same format:
Content-Type: application/pdf;
     name*0=Test - Test.pdfAnyone know if there is a way I can "safely" parse these headers? Is this a JavaMail bug?
Thanks

Absolutely, and please don't take my posting as a complaint - well over 99.9% of our emails are consumed flawlessly. I'm expressing more of (my opinion only) a high-level philosophical argument about of why I think good software (JavaMail) should try to by-default interact with not-so-good software. I'm not basing this not on "technical correctness", but rather the following unfortunate scenario:
One particular example would be certain products made by a company with the initals "MS". These products almost always hide brokenness (they act as highly liberal consumers). Outlook, Internet Explorer, are the 2 primary examples. So a user (and unfortunately most users have never read or care about an RFC) opens the email with Outlook, or the web page with Explorer, etc. It "works". Now, we programmers certainly know better, but remember that we aren't buying or choosing products or services, we're offering them. User then opens with Java-based product (stack trace). User buys or chooses MS-based product, MS-based product retains market stranglehold, commercial email providers test their products with Outlook, and in the end, brokenness is propagated anyway. It's kind of like reverse-Darwinism for software (survival of the most-broken).
Some customers complain that JavaMail doesn't strictly enforce every requirement of the standards
As an SMTP producer, I might see that, but personally I can never recall seeing a single problem with outgoing email from JavaMail. As a client consumer, I'm not sure why they would take that philosophy, unless maybe they are using JavaMail to test RFC-compliance.
there are forms of brokenness that go beyond what can be handled at the JavaMail API level.
Of course, but I have yet to see a message that the "MS" client could not read. So perhaps they are working around some of these at a higher level (in the GUI, maybe?)
But for developers who don't think about this problem...
Most developers most likely aren't using JavaMail to read in over 25,000 emails a day. So admittedly, I'm in a unique situation.
Having made that decision it's relatively straightforward to configure JavaMail appropriately. Not really, one change required updating the jar file, others require obscure uses of the API. And a developer/administrator is only going to make those changes after seeing the problem.
to avoid propagating brokenness as you suggest.
Brokenness will get propagated anyway (see reverse-Darwinism, or the Theory of De-evolution above).
I'm looking forward to trying out 1.4.1, and thanks again for all the assistance, a very thorough FAQ, and overall a great API. I think the opensourcing was a good idea, JavaMail really is by far the best API that I have seen for this type of high-volume work.

Similar Messages

  • Setting path for "Content-disposition" filename

    I'm trying to figure out how to set the filename in the following:
    String path = "/usr/local/jakarta-tomcat-4.1.29/webapps/myapp/my_images.zip";
    response.setContentType("application/octet-stream");
    response.setHeader("Content-disposition","attachment; filename=" +path);The file that I'd like the user to download is called my_images.zip. It resides here: /usr/local/jakarta-tomcat-4.1.29/webapps/myapp/my_images.zip
    However, when I run the servlet, it pops up a dialog box with this as the filename: usrlocal_jakarta-tomcat-4.1.29_webapps_myapp_my_images.zip
    It's replaced the slashes with underscores. What's going on? The zip file resides in the root folder of my app. The servlet is in a subfolder called "fpauto".

    Ah, ok. Right, so when you set that heading what you are saying to the client is: "The response from this web request is actually a file with the following name..."
    For example you could set it to be README.TXT and then use
    response.getWriter().println("Hello World.");as the only other line of your servlet. That would send a text "file" called README.TXT to the client containing the text "Hello World" only. Setting that response header doesn't tell the servlet to do anything other than inform the client what filename to use.
    It would be nice if there was a method called "transmit file" or somesuch, but there isn't. The simplest reason why is that if you just want to send a file you can usually provide it directly from the web (or app) server by putting it into the content. For example create a file my_files.zip in the root of your war and it will typically be made available as a file anyway.
    The assumption is that servlets are for generating content not just sending content that already exists.
    However, if you have a good reason for wanting to do this, you can create a loop that reads in the file and sends its contents to the response's output stream. I usually use the commons-io IOUtil [copy |http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html#copy(java.io.InputStream,%20java.io.OutputStream)] functions to do this.
    So why do you want to use a servlet to send "static" content?

  • Content-Disposition Convert to RTF

    Hi I have some problem with my jsp.
    Apprarently I am able to populate my page into MS-WORDS as RTF
    However I encounter Garbage value like
    &Amp; instead of &
    May there be some better way to convert? May it due to some settings i need to set on the response.XXX???
    Thanks for your help. Deeply appreciated.
    Derek Sum
    <%@ page language="java"%>
    <%@page import="test.kis.dto.*"%>
    <%@page import="test.kis.utility.*"%>
    <%@page import="java.util.*"%>
    <%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
    <%@ taglib uri="/WEB-INF/standard/c.tld" prefix="c" %>
    <%
    response.setHeader ("Content-Disposition", "filename=appln.rtf;");
    response.setContentType("appln.rtf");
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("dd/MMM/yy");
    String today = format.format(new java.util.Date());
    %>

    To export PDF to RTF/Word file
    Using Adobe ReaderX or Reader XI
    Launch Adobe Reader X or Reader XI
    Select “Tools” and click “Sign In” link to sign in with your Adobe ID and password
    Select “Export PDF” then click “Select PDF file” link to choose your PDF file
    Select format from “Convert To” pull down menu(docx, doc,rtf,xlsx)
    Click “Convert”
    Click “Download Converted File” link to download the file to your computer after the process is completed.
    Using Web UI
    Log into https://createpdf.acrobat.com/signin.html with your Adobe ID and password
    Select “Export from PDF”
    Click “Select Files” button then choose your PDF file
    Select the format from the list below
    Check ON “Recognized text in” if your PDF file is scanned images to recognize the image to text
    Click “Export” button
    Click “Download” button in the progress bar after completion of exporting to download the file to your computer.
    Note: All exported files are stored at https//files.acrobat.com
    Hisami

  • Bug setting Content-Disposition of response with the July 20th build of Weblogic 6.1

              I am using the July 20th Beta build, weblogic610_bld_127633_sol.bin, running under
              Solaris OS 2.6.
              I have two basic questions:
              1. Why am I seeing the error I describe below.
              2. Will this problem be fixed either by:
              A. A new release of the iisproxy.dll accompanying Weblogic 6.1 GA release on July
              30th (I currently use the iisproxy.dll which accompanied Weblogic 6.0 sp 1).
              B. A fix in the jsp response handling of the application server in the GA release.
              When used with the iisproxy.dll, in Weblogic 6.1 a jsp can not contain code to
              alter the filename in the response it is returning based on a request. This functionality
              worked in both Weblogic 4.5 and Weblogic 6.0 sp2.
              I use the following code to send back a tab seperated value file with a variable
              filename from a jsp page.
              response.setHeader("Content-Disposition", "filename=" + strFileName ".tsv");
              response.setContentType("text/tab-separated-values");
              When hitting Weblogic Server directly, this code works fine in Weblogic 6.1.
              My browser presents me with a download prompt, with a default filename equal to
              the variable filename I am setting in the jsp page.
              When hitting Weblogic Server through iisproxy.dll, iisproxy.dll gets the response
              from the application server, ignores it, then repeatedly resends the request to
              the application server. It gives up after 6 "bad" responses, and returns
              "No backend server available for connection: timed out after 20 seconds.".
              The functionality was perfect with the identical iisproxy.dll used with either
              Weblogic 4.5 or Weblogic 6.0 sp2.
              Things I have already tried:
              I can't find a newer version of the iisproxy.dll than the one that came with Weblogic
              6.0 sp1 (This is the 696 KB one).
              I have confirmed that the exact line of code which breaks the iisproxy.dll is
              the line where you set the filename. The download prompt works fine if you don't
              try to alter the response to set a specific filename.
              I have confirmed that changing the file extension to .jsp while still trying to
              alter the filename fails.
              

    As you said, it could be the WLS6 truststore which doesn't contain the certificate authority. Just check which files are used (cacerts, cacerts.jks,..), and maybe you can find it by checking the option startup : -Dweblogic.security.SSL.trustedCAKeyStore
    It doesn't matter when the exception is triggered (you can do an explicit handshake, or just write in the socket and then wait for an implicit handshake)

  • Making HTTP requests with content-disposition

    I need to write a Java app that can access a web server that returns most content as files (ie. using content-disposition).
    Previously I have used something like this to make HTTP GET requests to return files on a web server.
    ��String strUrl = "http://www.yahoo.com/index.html";
    ��URL url = new URL( strUrl );
    ��URLConnection conn = url.openConnection();
    ��InputStream in = url.openConnection.getInputStream();
    ��byte[] buf = new byte[conn.getContentLength()];
    ��in.read( buf, 0, getContentLength() );
    This doesn't seem to work when the HTTP header has "content-disposition". Here is the header returned by the web server.
    ��Server=Microsoft-IIS/4.0
    ��Date=Thu, 04 Jul 2002 13:18:53 GMT
    ��Content-Disposition=filename=file.xyz
    ��Content-Type=text/html
    ��Cache-control=private
    ��Transfer-Encoding=chunked
    ��content-length=30306
    The number of bytes actually read (as returned by in.read()) is less than the number of bytes as indicated in the content-length.
    How can I handle this?
    Mike

    I have managed to discover that an incorrect content-type header is causing the problems that I have been encountering with file downloads from a web server.
    As I mentioned in a previous post the content-length header was not a good indicator of file download success.
    My program downloads pictures from the internet, almost always in .jpeg format. I have found that files are successfully downloaded only when the content-type header contains the words: image/jpeg. In some instances although I am trying to download a picture file the http content-type header is stated as being: text/html.
    In those instances where text/html is given as the content-type file download fails in so far as the files are not acutally viewable on the native system (Windows varieties in my case).
    To determine the error I used the following code:
    URLConnection urlConnect = myURL.openConnection();
    System.out.println("Http Header, Content-Type: "+urlConnect.getContentType());
    System.out.println("Http Header, Content-Length: "+urlConnect.getContentLength());Even though content-length mostly corresponded with the downloaded files length, if the Conten-Type header was wrong then the file was all but useless.
    Regards
    Davo

  • ("Content-Disposition", "attachment;filename=x") works in Netscape; not IE.

    I'm sending an ASCII file back to the client using the following method:
    private void sendFile(String fullFilePath, HttpServletResponse res) {
    if(fullFilePath!=null) {
    try {
    res.setContentType("application/text/plain"); //this will be an unknown type
    res.setHeader("Content-Disposition", "attachment;filename=" + fullFilePath);
    FileInputStream fi = new FileInputStream(fullFilePath);
    OutputStream out = res.getOutputStream();
    while(true) {
    int d = fi.read();
    out.write(d);
    if(fi.available()==0)
    break;
    fi.close();
    out.close();
    catch (Exception e) {
    Logger.err.println("Error in TableQueryServlet.sendFile(): " + e,Log.ERROR);
    This works great in Navigator. The filename that it defaults to in the Save file dialog box is the same one I set it to using the fullFilePath string - including the .txt extension. However, in IE it ignores my filename and assigns its own random filename. Any ideas how I can get this to work in IE?

    It's not the greatest, but see the following...
    MS knowledgebase # Q279667

  • Specifying pdf document's name in browser - content-disposition problem

    Hello ,
    I want to open a PDF document in browser, which needs to have a specified filename when user tries to save it.
    My web app sends an "application/pdf" document back to the browser.
    I use the Content-Disposition HTTP header to instruct the browser to open the document inline, i.e. using the appropriate plugin, which is Adobe PDF in this case. When the user clicks Save As..., the document filename needs to be the one specified by the same HTTP header. Here is the code:
    response.setContentType("application/pdf");
    String contentDisposition = "inline; filename=\"filename.pdf\"";
    response.setHeader ("Content-Disposition", contentDisposition);
    response.setHeader ("Content-Length",new Long(file.length()).toString());Unfortunately this does not work for me. Document opens fine, but I can't get it to have the name I specified. The plugin uses the default name, which is the full URL of the document. I tried several combinations of HTTP headers, but neither worked. I tried putting an additional GET request parameter at the end of the URL, like ...&filename=filename.pdf , but that didn't work either.
    I tried Adobe 7.0 and 8.1.2.
    Any suggestions ?
    Thanks a lot!

    Thank you very much for your answer.
    Needless to say I tried opening the document in IE, FF and Chrome, and neither worked.
    I had a look at the code at your web site, and saw that you were serving files for direct download: Content-Dispositoin:attachment... .
    This works fine for me too, and the filename is recognized correctly in that case. The problem is, I want to display the PDF inline...
    My server is not configured for URL rewriting, and I would be probably quite some problem to make this app work with it, so the /filename.pdf is not an option for me.
    Edited by: matavulj on Sep 25, 2008 7:21 AM

  • How to specify the charatcer encoding for the parametes Content-Disposition

    I want to download a file with chinese name .
    response.setHeader("Content-Disposition","attachment; filename=" + fileName);
    the above part of code is working fine for english file names. but i am facing problem when i try to get file with chinese name .
    The pop up window is not defaulting with the chinese name .
    When i searched for net , in one rfc it was specified how to do that one.
    http://www.faqs.org/rfcs/rfc2184.html.
    I tried it but .. could not able to solve the problem.

    Hi,
    Am facing similar problem with russian and japanese characters in file name.
    I tried the UrlEncoder.encode(filename, "utf-8') api to encode the file name. This worked fine with IE but firefox and safari shows junk for japanese/russian characters.
    Anyone having an idea abt this?
    Thanks,
    Kapil

  • Setting the title for File download dialog when using content-disposition

    I am trying to send a file from server to client (browser). I am setting the content type of the resopnse and using the "content-disposition" header and giving the filename.
    But the issue is, when it is downloading and the dialog box is showing the progress bar, The title of the dialog box is showing something like "40% of localhost:9080.....". It doesn't show the file name in the title bar. Also, if the URL is very long (I am passing some get parameters and the servlet name is also long), I get junk characters like small boxes.
    How to set this title in the servlet?

    Is there a reason why you have to use FileReference.Save versus File.browseForSave?  In Adobe AIR, the File class, which extends the FileReference class,    provides more capabilities and has less security restrictions than the FileReference class.  Using File.browseForSave, it has a title:String parameter which is a string that is diplayed in the title bar of the dialog box.

  • Content-Disposition in IE

    Hi
    I want to stream a pdf file and then show in the browser . res.setHeader("Content-Disposition", "inline;filename=temp.pdf;");
    This works with netscape but in IE it shows blank page after starting a Acrobat process.
    But if i use
    res.setHeader("Content-Disposition", "attachment;filename=temp.pdf;"); this works with IE and netscape both . I am not able to figureout what is the problem . Anyone knows solution for this problem ? Please let me know .
    Thanks in advance
    Amit Kumar

    Hi
    I want to stream a pdf file and then show in the
    he browser . res.setHeader("Content-Disposition",
    "inline;filename=temp.pdf;");
    Hi,
    I am currently working with reports being exported to excel
    You can try this :
    res.setContentType("application/pdf");
    I think it should work ....but I am not sure whether the mime type for pdf is ("application/pdf"); or ("application/vnd-pdf");
    try both ....let me noe if fit works ....:)
    cheers

  • Problem with non-ASCII file name in content disposition header

    Hi All,
    I am facing some problems with the non-ASCII file name incase of content-disposition header. I read from the RFC 2183 that if the file name contains non-ASCII characters then the same should be encoded before sending to browser. I did the same but realized 2 problems:
    1. The name of the file is truncated in case the file name is slightly long for e.g. �����������j�b�g��������������������������.txt
    2. Also when the same file is opened in notepad, the title is showing encoded name %E6%9C%80%E4%B8%8A%E4%BD%8D.....
    Overall, I feel that the browser is not understanding or responding to the encoded header values.
    Is there any solution to this problem? I am using Microsoft IE 6.0.
    The code snippet is given below:
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              String fileName = "�����������j�b�g��������������������������.txt";          
              fileName = URLEncoder.encode(fileName, "UTF-8");
              resp.setCharacterEncoding("UTF-8");
              resp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
              resp.setContentType("application/download-binary");
              String s = "This is inside txt file";
              resp.getOutputStream().write(s.getBytes("UTF-8"));
              return;
         }Any help or pointer would be highly appreciated.
    Thanks and Regards,
    Ashish

    The MIME standards for non-ASCII filenames are not widely implemented.
    Many mailers use an ad hoc method for encoding filenames. JavaMail
    supports both methods, but you need to set properties, such as the
    mail.mime.encodefilename property. See the JavaMail javadocs for
    the javax.mail.internet package.

  • Inline type of Content-Disposition problem in IE

    Hi,
    In a project, we need post some information from applet to servlet. The servlet will send some files back. We like the files shown on browsers. So we set the header in servlet as
    response.setHeader("Content-Disposition",
    "inline;filename=\"" + fileName + "\"");
    We use JSobject in applet to call form from a html file to post the information to servlet, target of the form is set as "_blank", which suppose will open a new browser and show the file. For example if the file is a word file, the IE will show the file inside browser.
    However, if two files need to be shown, only the first file is shown, the other browser is blank, and when you kill the blank window, the other will turn to blank window too. We test other types of file, xls files have similar problem, the PDF files work OK. Two things can fix the problem, one is set the form method to ��get��, the other is changing the header from ��inline�� to ��attachment��. But we can't use any of them under the project requirement. Has anyone experienced such wield behavior and know the reason? Is this the bug of IE?
    Thanks in advance!

    Here are a little more about the file types.
    The files we test have problems are msword or excel files, I think all MS office type files have the problem. Same problem even you mix them, for example one word file and one excel give you the same problem as two word files.
    However there is no problem if I view several PDF files. If we mix the word file with pdf file. If the first file to view is PDF, then it works OK. Otherwise same problem.
    The browser we are testing IE 6.0 with SP1.

  • Bug: DocumentManager does not emit Content-Disposition header

    Good afternoon,
    I have a process with an output variable of type Document: this document has a defined filename. When invoking the process, instead of downloading the file with the defined filename, I was receiving a file with a name that looks like an MD5 hash.
    The fault for this lies with the DocumentManager application: it does not emit the content disposition header that would suggest to the browser what the real name of the octet-stream is. As such, without this header, the user agent will assume that the file-name is the endpoint name.
    The trace of the HTTP exchange is below:
    GET http://HOST:8080/rest/services/APP/SERVICE:1.0?q=83
    Cache-Control     no-cache, must-revalidate
    Date              Wed, 17 Nov 2010 12:54:29 GMT
    Pragma            no-cache
    Transfer-Encoding chunked
    Location          http://HOST:8080/DocumentManager/docm$(NUMBER)/$(HASH)?type=$(TYPE)
    X-Powered-By      Servlet/2.5 JSP/2.1
    GET http://HOST:8080/DocumentManager/docm$(NUMBER)/$(HASH)?type=$(TYPE)
    Date              Wed, 17 Nov 2010 12:54:35 GMT
    Transfer-Encoding chunked
    Content-Type      application/vnd.ms-excel
    X-Powered-By      Servlet/2.5 JSP/2.1
    The 2nd HTTP request should have included the specified header, like so:
    Content-Disposition: attachment; filename=MyExcel.xls
    RFC 2183: The Content-Disposition Header Field
    Best regards,

    Hi Robert,
    thanks for the reply. This is certainly a different aspect of the problem. The HTTP response that serves the dynamic portion (the one with the timestamp) has the response headers:
    HTTP/1.1 200 OK
    Date: Wed, 21 Jan 2015 18:05:53 GMT
    Server: Apache/2.2.29 (Unix)
    X-Powered-By: PHP/5.3.29
    Cache-Control: max-age=100000, public
    Keep-Alive: timeout=3, max=95
    Connection: Keep-Alive
    Transfer-Encoding: chunked
    Content-Type: application/xml
    So it is marked as cacheable. 
    My expectation now is this:
    - when clicking the "refresh" link in the markup, there should be no new timestamp. Reason is that the dynamic part should have been cached and I would expect the XSLT engine to use the browser's caching layer when fetching fragments
    - when hitting "F5" I would expect a revalidation request to be send to the server (e.g. max-age=0 or if-modified-since). Then the server can revalidate the fragment. In my example case the fragment would be served in any case, so I would expect a
    new timestamp
    - when hitting "CTRL+F5" I would expect a request with "cache-control: no-cache" that enforces the server side caches to always return new content. I would expect a new timestamp in this case.
    What I observe is that:
    - Chrome and FF work as expected
    - IE (10/11) will always fetch the dynamic fragment, so I will even see a new timestamp when clicking the refresh link. Also the "if-none-match" header in the "F5" case is NOT forwarded to the dynamic fragment (potentially because it is
    not cached in the browser at all)
    Carsten

  • Upload file - get rid of Content-Disposition: form-data;

    Hello,there.I just want to know if somebody had succesfully solved problem of getting rid of rows
    at uploaded file
    -----------------------------7d23c05be8
    Content-Disposition: form-data; name="fromflr"; filename="C:\WINDOWS\Desktop\Clock"
    Content-Type: text/plain
    "Some Data"
    -----------------------------7d23c05be8
    Content-Disposition: form-data; name="toflr"
    HARGAL
    I just need get rid of rows like
    -----------------------------7d23c05be8
    Content-Disposition: form-data; name="toflr"
    HARGAL
    and leave only "Some Data" in uploaded file.
    Any suggestion would be appreciate.

    See for example Upload taglib from Coldtags suite:
    http://www.servletsuite.com/jsp.htm

  • Content-Disposition inline/attachment

    I am using Content-Disposition for excel file download as below:
    I have searched the forum but not able to get the exact answer or solution. Please also guide me to some url if possible.
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "inline; filename=xyz.xls" );
    If I am using inline attribute but I am not able to specify the filename as xyz.xls and by default fileName provided by browser is the request URL(action-mapping in case of struts).
    But it works fine with attachment attribute and prompts for the exact file Name and works fine.
    I want to know how to specify the desired fileName with inline Attribute ? I am using the struts based request for Excel file generation.
    Best Regards,
    Amber Gupta

    if You dont't mind , I just want to tell u something on this code.
    If im wrong, Please tell me why? and what is the best way of acheiving it then,
    the code below loads whole filedata and it does not matter how big the file is. Don't you think that it will heat up the memory by doing so. and consider the method what he was doing is perfectly right. but is there any other solution/wrong with above code what MR/ms/Mrs Kenneth has written like.
    I just wanted to know the answer what is the wrong with MR. Kennth Code since i looked at it as a perfect working code(but with some glicth).
    with Regards
    Lokesh T.c
    FileInputStream in = new FileInputStream(fileToDownload);
    ServletOutputStream out = response.getOutputStream();
    int bytesRead = 0;
    byte byteArray[] = new byte[fileTODownload.length()];
    // Read in bytes through file stream, and write out through servlet stream
    /*while( ( bytesRead = in.read( byteArray ) ) != -1 ) {
    out.write( byteArray, 0, bytesRead );
    in.read(bytesArray,0,fileTODownload.length());
    out.write(bytesArray,0,fileTODownload.length());
    in.close();
    out.flush();
    out.close();

Maybe you are looking for