Results 1 to 14 of 14
  1. #1
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default how to get a file from the server and save to disk of the client pc

    Hi friends,

    This is my first thread and I want to ask for help on my problem.
    What I want to do is to be able to download a file residing on the application server pc and save it to the client pc where i am accesing the application. Can someone show me a sample code on how to go about this procedure. Thanks a lot..

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by ralphus View Post
    Hi friends,

    This is my first thread and I want to ask for help on my problem.
    What I want to do is to be able to download a file residing on the application server pc and save it to the client pc where i am accesing the application. Can someone show me a sample code on how to go about this procedure. Thanks a lot..
    Welcome Ralphus,

    Please give some more details about your query.

    What do you want to use for downloading files from server.
    JSP/Servlet ..Or Other.

    And What Have you tried to do it.??.....where are you getting the problem.??
    sanjeev,संजीव

  3. #3
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    i havent started the project yet coz i want to understand the process first. I think i can understand if you can give me sample either in JSP or servlet.

  4. #4
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    the project that i want to make is for the clients to be able to get the generated report file that will be save to the server pc. sample file would be excel files or pdf. i believe type will only differ to the mime type so i am more interested how to be able to let the client get the file residing in the remote server pc.

  5. #5
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by ralphus View Post
    the project that i want to make is for the clients to be able to get the generated report file that will be save to the server pc. sample file would be excel files or pdf. i believe type will only differ to the mime type so i am more interested how to be able to let the client get the file residing in the remote server pc.
    How you are generating reports..?

    One doubt : Do you need to create project only to show Reports.??




    sanjeev,संजीव

  6. #6
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    i was able to get a sample code but my problem is how to configure the code to my incorporate it to my servlet. i dont see error but i cant compile this servlet. I create a servlet form and paste this code but as ive said i cant compile it and i dont know why, and maybe someone can help me.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletResponse;

    public class FileDownloader {

    /** Creates a new instance of FileDownloader */
    public FileDownloader() {
    }

    public void downloadFile(HttpServletResponse objHttpServletResponse, String strFileName) {
    try {

    File fExcelFile = new File(strFileName);
    int dotIndex = strFileName.indexOf(".");
    String fileType = strFileName.substring(dotIndex+1,strFileName.lengt h());
    if (fileType.trim().equalsIgnoreCase("doc")) {
    objHttpServletResponse.setContentType( "application/msword" );
    } else if (fileType.trim().equalsIgnoreCase("xls")) {
    objHttpServletResponse.setContentType( "application/vnd.ms-excel" );
    } else if (fileType.trim().equalsIgnoreCase("pdf")) {
    objHttpServletResponse.setContentType( "application/pdf" );
    } else if (fileType.trim().equalsIgnoreCase("ppt")) {
    objHttpServletResponse.setContentType( "application/ppt" );
    } else {
    objHttpServletResponse.setContentType( "application/octet-stream" );
    }
    objHttpServletResponse.setHeader("Content-Disposition", "attachment;filename=\"" + strFileName + "\"");

    objHttpServletResponse.setHeader("Pragma", "public");
    objHttpServletResponse.setHeader("Cache-control", "max-age=0");

    InputStream objFileInputStream = new FileInputStream(fExcelFile);
    ServletOutputStream objServletOutputStream = objHttpServletResponse.getOutputStream();
    int iBit = 256;

    try {
    while(iBit >= 0) {
    iBit = objFileInputStream.read();
    objServletOutputStream.write(iBit);
    }
    } catch(IOException ioe) {
    ioe.printStackTrace(System.out);
    }
    objServletOutputStream.flush();
    objServletOutputStream.close();
    objFileInputStream.close();
    } catch(FileNotFoundException e) {
    e.printStackTrace();
    } catch(IOException e1) {
    e1.printStackTrace();
    }
    }
    }

  7. #7
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    What error are you getting??
    sanjeev,संजीव

  8. #8
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    the reports are already generated. its in xls and pdf format. what i want to do is to be able to download the report file and save it to the client pc.

  9. #9
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    type Exception report

    message

    descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: PWC1397: Wrapper cannot find servlet class trader.web.FileDownloader or a class it depends on
    root cause

    java.lang.ClassNotFoundException: trader.web.FileDownloader
    note The full stack traces of the exception and its root causes are available in the Sun Java System Application Server 9.1_01 logs.


    --------------------------------------------------------------------------------

    Sun Java System Application Server 9.1_01

  10. #10
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    i imported the the trader.web package where i created the servlet and i got this error.

    HTTP Status 500 -

    --------------------------------------------------------------------------------

    type Exception report

    message

    descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: PWC1403: Class trader.web.FileDownloader is not a Servlet
    root cause

    java.lang.ClassCastException: trader.web.FileDownloader cannot be cast to javax.servlet.Servlet
    note The full stack traces of the exception and its root causes are available in the Sun Java System Application Server 9.1_01 logs.


    --------------------------------------------------------------------------------

    Sun Java System Application Server 9.1_01

  11. #11
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    just extends your class with HttpServlet.

    because your class is not a servlet
    sanjeev,संजीव

  12. #12
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    oh i see, ok thanks sanjeev for your help

  13. #13
    ralphus is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    i got this another error message, what could be the problem here?

    HTTP Status 405 - HTTP method GET is not supported by this URL

    --------------------------------------------------------------------------------

    type Status report

    messageHTTP method GET is not supported by this URL

    descriptionThe specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).


    --------------------------------------------------------------------------------

    Sun Java System Application Server 9.1_01

  14. #14
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    This error is coming because goGet() method is not implemented by your servlet and by default when client makes a request it goes to doGet() method.

    So do one thing :

    provide implementations of doGet() and then call your method from doGet().

    sanjeev,संजीव

Similar Threads

  1. To transfer a file from client to server
    By phani in forum Networking
    Replies: 4
    Last Post: 10-12-2010, 07:15 PM
  2. send file via client - server model
    By spasavvas in forum Networking
    Replies: 15
    Last Post: 08-13-2010, 12:45 PM
  3. how to save image to disk after using pixelgrabber
    By shishirg in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 03:45 PM

Posting Permissions

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