Results 1 to 8 of 8
  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    331
    Blog Entries
    1
    Rep Power
    7

    Default issue while download file, support only txt & HTML

    Hi All!

    I am trying to download files from my network shared folder. can download and display .HTML & .txt files. but i want to display the below formats

    Java Code:
    {"doc","docx","dotx","pdf", "txt","png","jpg","html"}
    i tried with Servlet & JSP, tried by replaced the content type too. But nothing to use, please guide me to reach my goal.

    Please find my below code

    JSP call the service via Ajax and display the content via div


    Java Code:
     $.ajax({
    						type:"GET",    
    						url: "DWResume.jsp",
    						data:"resumeId="+UpdateValue+"&UserName="+UserName,        
    						  success: function(success) {
    							$("#resume_content").html(success); 
    							}
    						});

    JSP page to download the content.

    Java Code:
    String  UserName= (String)session.getAttribute("UserName");
                  String  resumeId = (String)request.getParameter("resumeId");
    
    BufferedInputStream buf=null;
               ServletOutputStream myOut=null;
    
            try{
               String sub_locationPath = (String) session.getAttribute("sub_locationPath");
               myOut = response.getOutputStream();
               File myfile =  null;
    
    
               String fileLoaction = sub_locationPath+"/Resumes_US/"+UserName+"/"+resumeId;
               System.out.println("fileLoaction : "+fileLoaction);
                String[] docType = {"doc","docx","dotx","pdf", "txt","png","jpg","html"};
    
    
                 String ext=" ";
                       for(int i=0; i < docType.length; i++){
            	myfile = new File(fileLoaction+"."+docType[i]);
            	if(myfile.exists()){
            	ext=docType[i];
            	break;
            	}
                       
            		   }
                       
                       if(ext.trim().equalsIgnoreCase("doc") || ext.trim().equalsIgnoreCase("docx") || ext.trim().equalsIgnoreCase("dotx"))
                       {
                      	 response.setContentType("application/msword");
                       }
                       else if(ext.trim().equalsIgnoreCase("pdf"))
                       {
                      	 response.setContentType("application/pdf");
                       }
                       else if(ext.trim().equalsIgnoreCase("png") || ext.trim().equalsIgnoreCase("jpg"))
                       {
                      	 response.setContentType("application/jpeg");
                       }
                       else if(ext.trim().equalsIgnoreCase("html"))
                       {
                      	 response.setContentType("text/HTML");
                       }
                       else if(ext.trim().equalsIgnoreCase("xls"))
                       {
                      	 response.setContentType("application/vnd.ms-excel");
                       }
                       else if(ext.trim().equalsIgnoreCase("txt"))
                       {
                      	 response.setContentType("text/plain");
                       }
                       else
                       {
                           response.setContentType( "application/octet-stream" );
                       } 
                       
                       
            	 String fName = fileName+"."+ext;
            	 response.setCharacterEncoding("UTF-8");
            	 response.addHeader("Content-Disposition","attachment; filename=\"" + fName + "\"");
                 response.setContentLength( (int) myfile.length( ) );
                 FileInputStream input = new FileInputStream(myfile);
                 buf = new BufferedInputStream(input);
                 int readBytes = 0;
                 while((readBytes = buf.read( )) != -1)
                  myOut.write(readBytes);
            } catch (IOException ioe){
                    throw new ServletException(ioe.getMessage( ));
                 } finally {
                     if (myOut != null)
                         myOut.close();
                      if (buf != null)
                      buf.close();
                 }

    If i call the HTML / txt file it return the exact file. But if i call the PDF, DOCX or other format, the output is looklike below.

    issue while download file, support only txt &amp; HTML-output.png
    - Raj

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: issue while download file, support only txt & HTML

    i want to display the below formats
    Is your browser configured to display those file types?
    Or do you mean you want your browser to download those file types to a disk file.

    The text of the png file you posted shows the contents of a zip file(see the PK at the beginning)
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    331
    Blog Entries
    1
    Rep Power
    7

    Default Re: issue while download file, support only txt & HTML

    Hi Norm,

    Thanks for your help,
    if i open the file from new tab it display the content eg : "file://F:\Users\raj\\Forms\634882.doc".
    if i download those files through my code, it saving as exact format and data also fine to read. But i dont want to download, i want to display the content on div.

    The text of the png file you posted shows the contents of a zip file(see the PK at the beginning)
    yes you are right, when i goole it, the docx --> zip format & doc --> binary formate.
    Last edited by Norm; 06-15-2018 at 07:07 PM. Reason: Changed color to quote tags
    - Raj

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: issue while download file, support only txt & HTML

    i want to display the content on div.
    Then the content needs to be converted to text, not the binary values that are in a zip file.

    If you are using a browser that has extensions (like Chrome), then maybe there is an extension that will make the contents of the file into text for displaying.
    Last edited by Norm; 06-15-2018 at 07:10 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    331
    Blog Entries
    1
    Rep Power
    7

    Default Re: issue while download file, support only txt & HTML

    Quote Originally Posted by Norm View Post
    Then the content needs to be converted to text, not the binary values that are in a zip file.

    If you are using a browser that has extensions (like Chrome), then maybe there is an extension that will make the contents of the file into text for displaying.
    Fine, i am using the browser to run my app. what you are saying is perfect. but every end user will not ready to install the extension and use my application. it may cause for failure of app.

    is ther any possiblity to make it by code itself.

    Best,
    Raj
    - Raj

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: issue while download file, support only txt & HTML

    make it by code itself
    The code on the server could create text from the file to be displayed in the returned html page instead of returning the file.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,217
    Rep Power
    12

    Default Re: issue while download file, support only txt & HTML

    Quote Originally Posted by raj.mscking@gmail.com View Post
    Fine, i am using the browser to run my app. what you are saying is perfect. but every end user will not ready to install the extension and use my application. it may cause for failure of app.

    is ther any possiblity to make it by code itself.

    Best,
    Raj
    The problem is that each of those files has a specific binary (i.e. non-textual) encoding. JPEG is for images, docx for word, etc. Each requires it's own special application. One solution is to invoke Desktop.open() on the file and let the jvm launch the appropriate application. I have never tried this myself though. And if the app doesn't exist on the users machine or the file type isn't set properly, it won't work.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    331
    Blog Entries
    1
    Rep Power
    7

    Default Re: issue while download file, support only txt & HTML

    Quote Originally Posted by jim829 View Post
    The problem is that each of those files has a specific binary (i.e. non-textual) encoding. JPEG is for images, docx for word, etc. Each requires it's own special application. One solution is to invoke Desktop.open() on the file and let the jvm launch the appropriate application. I have never tried this myself though. And if the app doesn't exist on the users machine or the file type isn't set properly, it won't work.

    Regards,
    Jim
    Great Idea Jim,

    I will try this approach and let you know the status.

    Thanks
    Raj
    - Raj

Similar Threads

  1. Replies: 5
    Last Post: 07-29-2014, 07:42 PM
  2. Replies: 0
    Last Post: 07-21-2014, 11:53 AM
  3. Replies: 0
    Last Post: 05-12-2010, 11:11 AM
  4. Replies: 1
    Last Post: 01-22-2010, 05:23 PM
  5. Replies: 1
    Last Post: 11-06-2008, 04:47 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
  •