Results 1 to 6 of 6
  1. #1
    leahg1508 is offline Member
    Join Date
    Feb 2013
    Location
    panama
    Posts
    3
    Rep Power
    0

    Default How to file download using java

    I have a java app using tomcat 7 in one server and my data in othe directory, I access every file with windows path that i record from a database.
    it works fine with small files < 10mb, but when a browser client request download it delays too much time proportional to filesize but when the size is more than 50MB it takes to much to time to answer or it crash the session.
    the error that shows to the browser client is:

    Proxy Error

    The proxy server received an invalid response from an upstream server.
    The proxy server could not handle the request POST /Private/DocumentManager/DocumentWorkSpaces.jsf.

    Reason: Error reading from remote server


    could you helpme with some code and if i need to configure my tomcat.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: How to file download using java

    What error is the server throwing?
    What's in the logs?
    How are you reading/writing the file?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    leahg1508 is offline Member
    Join Date
    Feb 2013
    Location
    panama
    Posts
    3
    Rep Power
    0

    Default Re: How to file download using java

    Quote Originally Posted by Tolls View Post
    What error is the server throwing?
    What's in the logs?
    How are you reading/writing the file?
    I am including my code related to this

    CLIENT SIDE CODE
    (richfaces)

    <a4j:push interval="1000"
    eventProducer="#{documentWorkSpacesController.addL istener}"
    reRender="downloadPanel"
    enabled="#{documentWorkSpacesController.enabled}" id="download" />



    <h:commandButton id="btnDownLoad"
    action="#{documentWorkSpacesController.doDownLoad} "
    target="_top" reRender="download"
    value="#{msg['label.boton.descargar']}" />


    SERVER SIDE CODE I am using tomcat7

    public void doDownLoad() throws IOException, ServletException{
    byte[] readData = new byte[1024];
    ServletOutputStream stream ;
    FileInputStream file;
    int readed = 0;
    int buffered = 0;
    File archivo;

    HttpServletResponse resp;

    setEnabled(true);

    resp = (HttpServletResponse) FacesContext.getCurrentInstance()
    .getExternalContext().getResponse();
    archivo = new File(ParametroDirectorio.getValorCaracter()
    + Documento.getDocumento_Padre().getNombre());

    resp.addHeader("Content-Type", "application/force-download");
    resp.addHeader("Content-Disposition", "attachment; filename=\""
    + Documento.getDocumento_Padre().getNombre() + "\"");

    resp.setContentLength((int) archivo.length());
    resp.resetBuffer();


    file = new FileInputStream(archivo);
    stream = resp.getOutputStream();

    try {
    if (archivo.exists()) {
    while ((readed = file.read(readData)) > -1) {
    stream.write(readData, 0, readed);
    buffered += readed;
    if(buffered >= (1024 * 1024))
    {
    buffered = 0;
    stream.flush();
    }
    }

    }
    archivo = null;
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    if(file != null){
    file.close();
    resp.resetBuffer();
    }
    }
    }
    }

    MY LOG IS NOT SHOWING ANY ERROR MESSAGE ONLY THIS
    127.0.0.1 - - [21/Feb/2013:12:36:58 -0500] "GET /a4j/s/3_3_3.Finalorg.ajax4jsf.resource.UserResource/n/n/-879258763/DATA/eAGNkr9rFEEUx98tBqNR0FxQLALx!IUic5woEWJAMKIHe4m4Rj FWc7vv7uac3Zmbmb0sRgULFbSwUDsLQctY-V8INmlt1EZEBLERO2d2z5xJ5XZv-c73fd77vtXvMKIVHBOqTWiXZie7ukUUapGqEMmiRnV5UBxcVGy OGgruK5!65sEWH3aGCqnBcyIxmBgD436X9mmV06RdXWh2MTQzP mzFTDLr2YO7UPJhNBYRazGMBvVIn!IU8yKTluWws8hIi4aoSSh iKRLrTQJjG10UPEIV0D6q6-!ezj598b7hgefDtpBTredpjBsZAqNY0rYM27V9E-UeBvYUlExUA1SMcnaLNjnOZNK1P2JbEp0mOQBHowlycoW2G2g6 IjqfSTuJZiLJ9wBQ2gGQKdhXQFvpZl3P3zu29OTTDy!Xldd1Q6 dX9x8FP5fWzjiFI6jkYUgadtD13mxYjyWv!6YfeO32ftfcbW50-SgcOrASiTCN7bKuCXUzsAaoXTJKcI6KSMoScwcgVVC-kW-AuJzsYvMdvVm7-uXr5MqFnMIO5xmYcLCECbKQGpkaK0QaG9iV!87fFhlLmS33QFQ vKda3IVXnBhgNmtA2qvV6iEWyjon51Nnaienj07WpDVc0W!mvQ SpFXLuHMHV7hLbd-OeXr3!de3jag1IdiuOyAf0DPZ!GTVQPVp9Pjj37-PjvvBN!AALKDtk_.jsf?time=1361468214390 HTTP/1.1" 200 5662
    127.0.0.1 - - [21/Feb/2013:12:36:59 -0500] "POST /Private/DocumentManager/DocumentWorkSpaces.jsf HTTP/1.1" 200 11311
    127.0.0.1 - - [21/Feb/2013:12:36:59 -0500] "GET /a4j/g/3_3_3.Finalorg.richfaces.renderkit.html.images.But tonDisabledBackgroundImage/DATB/eAFjYGDg!vjx45kzZ5gAGIMFRQ__.jsf HTTP/1.1" 200 113
    127.0.0.1 - - [21/Feb/2013:12:37:02 -0500] "POST /Private/DocumentManager/DocumentWorkSpaces.jsf HTTP/1.1" 200 11312
    127.0.0.1 - - [21/Feb/2013:12:42:05 -0500] "GET /favicon.ico HTTP/1.1" 404 988
    127.0.0.1 - - [21/Feb/2013:12:43:32 -0500] "POST /Private/DocumentManager/DocumentWorkSpaces.jsf HTTP/1.1" 200 16384

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: How to file download using java

    Please post code inside [code] tags [/code] in order to retain formatting.
    Many of us will not read unformatted code.

    You need some more debugging logging in your app so you can see where it is getting to.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    leahg1508 is offline Member
    Join Date
    Feb 2013
    Location
    panama
    Posts
    3
    Rep Power
    0

    Default Re: How to file download using java

    CLIENT SIDE CODE
    (richfaces)
    Java Code:
    <a4j:push interval="1000"
    eventProducer="#{documentWorkSpacesController.addL istener}"
    reRender="downloadPanel"
    enabled="#{documentWorkSpacesController.enabled}" id="download" />
    <h:commandButton id="btnDownLoad"
    action="#{documentWorkSpacesController.doDownLoad} "
    target="_top" reRender="download"
    value="#{msg['label.boton.descargar']}" />


    SERVER SIDE CODE I am using tomcat7

    Java Code:
    public void doDownLoad() throws IOException, ServletException
    {
            byte[] readData = new byte[1024];
            ServletOutputStream stream ;
            FileInputStream file;
            int readed = 0;
            int buffered = 0;
            File archivo;
            HttpServletResponse resp;
            setEnabled(true);
            resp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
            archivo = new File(ParametroDirectorio.getValorCaracter()+ Documento.getDocumento_Padre().getNombre());
            resp.addHeader("Content-Type", "application/force-download");
            resp.addHeader("Content-Disposition", "attachment; filename=\""+ Documento.getDocumento_Padre().getNombre() + "\"");
            resp.setContentLength((int) archivo.length());
            resp.resetBuffer();
            file = new FileInputStream(archivo);
            stream = resp.getOutputStream();
    
            try {
                   if (archivo.exists()) {
                                      while ((readed = file.read(readData)) > -1) 
                                              {
                                                stream.write(readData, 0, readed);
                                                buffered += readed;
                                                if(buffered >= (1024 * 1024))
                                                {              
                                                  buffered = 0;
                                                  stream.flush();
                                                }
                                              }
    
             }
             archivo = null;
       } catch (IOException e) 
              { e.printStackTrace();
               }
         finally{
                  if(file != null)
                   {
                     file.close();
                     resp.resetBuffer();
                   }
                 }
       }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: How to file download using java

    And is it getting into that method?
    If you added some logging in there (like I suggested above) you might find out.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 11-24-2011, 03:03 AM
  2. Java download file
    By Soljav22 in forum New To Java
    Replies: 4
    Last Post: 11-17-2010, 08:08 PM
  3. To download file, using java.nio
    By Jorik in forum Networking
    Replies: 3
    Last Post: 10-25-2010, 03:13 PM
  4. Download a file by Bluetooth in Java SE
    By danielpereira in forum New To Java
    Replies: 0
    Last Post: 10-06-2010, 09:56 PM
  5. Replies: 0
    Last Post: 03-05-2010, 05:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •