You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by raygrrr <ra...@gmail.com> on 2011/10/19 11:17:42 UTC

can't download excel file correctly

Dear Experts,

I have this action class to perform file downloading function, however, it
doesn't export excel file correctly, it give me a 'actionClassName.zip' file
instead of the excel file, but other format files like pdf file, jpg file
and text file are fine though, can't figure it out why 



My action class code:
public class DownloadDocumentAction extends Action {
	private static Log log = LogFactory.getLog(DownloadDocumentAction.class);
	
	public ActionForward execute(ActionMapping mapping, 
									ActionForm form, 
									HttpServletRequest request, 
									HttpServletResponse response) {
		

        try 
    	{
    		String token = OpenkmUtils.getToken();
    		byte[] localFile = OpenkmUtils.getFile(token, okmPath);
    		
    		ServletOutputStream out = response.getOutputStream();
    		InputStream in = new ByteArrayInputStream(localFile);
     
    		byte[] buffer = new byte[1024];

    		int length = -1;
    		while((length = in.read(buffer)) != -1) {                 
    			out.write(buffer, 0, length);       
    		}
    		
    		in.close();
    		out.flush();
    		out.close();
    	  }
          catch (java.io.IOException ex)
          {
    		 log.error(ex.getMessage());
    	  }
    	  

    	  response.setContentType("application/vnd.ms-excel");
              
response.setHeader("Content-Disposition","attachment;filename=\"" +
fileName+"\"");
          
    	  return null;
	}

}

--
View this message in context: http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4917014.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: can't download excel file correctly

Posted by raygrrr <ra...@gmail.com>.
Hi Brian, Nate, You guys are right, it is exactly the case you guys
described!!! Thanks very much!!!

On Thu, Oct 20, 2011 at 12:44 AM, Sarr, Nathan-2 [via Struts]
<ml...@n5.nabble.com> wrote:
> Is there any chance this is an .xlsx (office 2007) and not an xls document?
>  .xlsx documents are really zip files.  If this is the case then your
> content type would be different as well.
>
> I believe the .xlsx is:
> application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
>
> Hope this helps.
>
> -Nate
>
> Nathan Sarr
> Senior Software Engineer
> River Campus Libraries
> University of Rochester
> Rochester, NY  14627
> (585) 275-0692
>
> -----Original Message-----
> From: raygrrr [mailto:[hidden email]]
> Sent: Wednesday, October 19, 2011 5:18 AM
> To: [hidden email]
> Subject: can't download excel file correctly
>
> Dear Experts,
>
> I have this action class to perform file downloading function, however, it
> doesn't export excel file correctly, it give me a 'actionClassName.zip' file
> instead of the excel file, but other format files like pdf file, jpg file
> and text file are fine though, can't figure it out why
>
>
>
> My action class code:
> public class DownloadDocumentAction extends Action {
>         private static Log log =
> LogFactory.getLog(DownloadDocumentAction.class);
>
>         public ActionForward execute(ActionMapping mapping,
>
> ActionForm form,
>
> HttpServletRequest request,
>
> HttpServletResponse response) {
>
>
>         try
>     {
>     String token = OpenkmUtils.getToken();
>     byte[] localFile = OpenkmUtils.getFile(token, okmPath);
>
>     ServletOutputStream out = response.getOutputStream();
>     InputStream in = new ByteArrayInputStream(localFile);
>
>     byte[] buffer = new byte[1024];
>
>     int length = -1;
>     while((length = in.read(buffer)) != -1) {
>     out.write(buffer, 0, length);
>     }
>
>     in.close();
>     out.flush();
>     out.close();
>      }
>           catch (java.io.IOException ex)
>           {
>     log.error(ex.getMessage());
>      }
>
>
>      response.setContentType("application/vnd.ms-excel");
>
> response.setHeader("Content-Disposition","attachment;filename=\"" +
> fileName+"\"");
>
>      return null;
>         }
>
> }
>
> --
> View this message in context:
> http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4917014.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
> http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4918544.html
> To unsubscribe from can't download excel file correctly, click here.


--
View this message in context: http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4919985.html
Sent from the Struts - User mailing list archive at Nabble.com.

RE: can't download excel file correctly

Posted by "Sarr, Nathan" <ns...@library.rochester.edu>.
Is there any chance this is an .xlsx (office 2007) and not an xls document?  .xlsx documents are really zip files.  If this is the case then your content type would be different as well.

I believe the .xlsx is: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Hope this helps.

-Nate

Nathan Sarr
Senior Software Engineer
River Campus Libraries
University of Rochester
Rochester, NY  14627
(585) 275-0692

-----Original Message-----
From: raygrrr [mailto:raygrrr@gmail.com] 
Sent: Wednesday, October 19, 2011 5:18 AM
To: user@struts.apache.org
Subject: can't download excel file correctly

Dear Experts,

I have this action class to perform file downloading function, however, it
doesn't export excel file correctly, it give me a 'actionClassName.zip' file
instead of the excel file, but other format files like pdf file, jpg file
and text file are fine though, can't figure it out why 



My action class code:
public class DownloadDocumentAction extends Action {
	private static Log log = LogFactory.getLog(DownloadDocumentAction.class);
	
	public ActionForward execute(ActionMapping mapping, 
									ActionForm form, 
									HttpServletRequest request, 
									HttpServletResponse response) {
		

        try 
    	{
    		String token = OpenkmUtils.getToken();
    		byte[] localFile = OpenkmUtils.getFile(token, okmPath);
    		
    		ServletOutputStream out = response.getOutputStream();
    		InputStream in = new ByteArrayInputStream(localFile);
     
    		byte[] buffer = new byte[1024];

    		int length = -1;
    		while((length = in.read(buffer)) != -1) {                 
    			out.write(buffer, 0, length);       
    		}
    		
    		in.close();
    		out.flush();
    		out.close();
    	  }
          catch (java.io.IOException ex)
          {
    		 log.error(ex.getMessage());
    	  }
    	  

    	  response.setContentType("application/vnd.ms-excel");
              
response.setHeader("Content-Disposition","attachment;filename=\"" +
fileName+"\"");
          
    	  return null;
	}

}

--
View this message in context: http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4917014.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: can't download excel file correctly

Posted by raygrrr <ra...@gmail.com>.
Hi Brian,

Thanks for your reply, I removed some unnecessary code - the
'fileName' variable is fine.

I will try to rename it tomorrow - but I don't think it will work as I
can unzip the actionClassName.zip file and it isn't the file... will
show you that tmr :)

Ray

On Thu, Oct 20, 2011 at 12:27 AM, Brian Thompson-5 [via Struts]
<ml...@n5.nabble.com> wrote:
> If you rename actionClassName.zip to actionClassName.xls, can you open it in
> Excel?
>
> Where is the 'fileName' variable defined?
>
> Brian
>
>
>
> On Wed, Oct 19, 2011 at 4:17 AM, raygrrr <[hidden email]> wrote:
>
>> Dear Experts,
>>
>> I have this action class to perform file downloading function, however, it
>> doesn't export excel file correctly, it give me a 'actionClassName.zip'
>> file
>> instead of the excel file, but other format files like pdf file, jpg file
>> and text file are fine though, can't figure it out why
>>
>>
>>
>> My action class code:
>> public class DownloadDocumentAction extends Action {
>>        private static Log log =
>> LogFactory.getLog(DownloadDocumentAction.class);
>>
>>        public ActionForward execute(ActionMapping mapping,
>>
>>  ActionForm form,
>>
>>  HttpServletRequest request,
>>
>>  HttpServletResponse response) {
>>
>>
>>        try
>>        {
>>                String token = OpenkmUtils.getToken();
>>                byte[] localFile = OpenkmUtils.getFile(token, okmPath);
>>
>>                ServletOutputStream out = response.getOutputStream();
>>                InputStream in = new ByteArrayInputStream(localFile);
>>
>>                byte[] buffer = new byte[1024];
>>
>>                int length = -1;
>>                while((length = in.read(buffer)) != -1) {
>>                        out.write(buffer, 0, length);
>>                }
>>
>>                in.close();
>>                out.flush();
>>                out.close();
>>          }
>>          catch (java.io.IOException ex)
>>          {
>>                 log.error(ex.getMessage());
>>          }
>>
>>
>>          response.setContentType("application/vnd.ms-excel");
>>
>> response.setHeader("Content-Disposition","attachment;filename=\"" +
>> fileName+"\"");
>>
>>          return null;
>>        }
>>
>> }
>>
>> --
>> View this message in context:
>>
>> http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4917014.html
>> Sent from the Struts - User mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
> http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4918472.html
> To unsubscribe from can't download excel file correctly, click here.


--
View this message in context: http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4918501.html
Sent from the Struts - User mailing list archive at Nabble.com.

Re: can't download excel file correctly

Posted by Brian Thompson <el...@gmail.com>.
If you rename actionClassName.zip to actionClassName.xls, can you open it in
Excel?

Where is the 'fileName' variable defined?

Brian



On Wed, Oct 19, 2011 at 4:17 AM, raygrrr <ra...@gmail.com> wrote:

> Dear Experts,
>
> I have this action class to perform file downloading function, however, it
> doesn't export excel file correctly, it give me a 'actionClassName.zip'
> file
> instead of the excel file, but other format files like pdf file, jpg file
> and text file are fine though, can't figure it out why
>
>
>
> My action class code:
> public class DownloadDocumentAction extends Action {
>        private static Log log =
> LogFactory.getLog(DownloadDocumentAction.class);
>
>        public ActionForward execute(ActionMapping mapping,
>
>  ActionForm form,
>
>  HttpServletRequest request,
>
>  HttpServletResponse response) {
>
>
>        try
>        {
>                String token = OpenkmUtils.getToken();
>                byte[] localFile = OpenkmUtils.getFile(token, okmPath);
>
>                ServletOutputStream out = response.getOutputStream();
>                InputStream in = new ByteArrayInputStream(localFile);
>
>                byte[] buffer = new byte[1024];
>
>                int length = -1;
>                while((length = in.read(buffer)) != -1) {
>                        out.write(buffer, 0, length);
>                }
>
>                in.close();
>                out.flush();
>                out.close();
>          }
>          catch (java.io.IOException ex)
>          {
>                 log.error(ex.getMessage());
>          }
>
>
>          response.setContentType("application/vnd.ms-excel");
>
> response.setHeader("Content-Disposition","attachment;filename=\"" +
> fileName+"\"");
>
>          return null;
>        }
>
> }
>
> --
> View this message in context:
> http://struts.1045723.n5.nabble.com/can-t-download-excel-file-correctly-tp4917014p4917014.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>