下载文件时获取附带文件内容的j​​sp源代码 [英] Getting the jsp source code appended with the file content when I download files

查看:91
本文介绍了下载文件时获取附带文件内容的j​​sp源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Struts2框架在Java中开发文件上载/下载功能,我们在该文件上载到远程服务器路径并从中进行下载.当我在本地计算机上使用本地路径作为我下载和上传任何格式文件的目的地路径检查本地计算机上的功能时,所有功能似乎都正常运行.开发环境具有JBoss服务器. 但是,当我在将应用程序部署在Weblogic服务器中的prod env上运行相同的文件时,.txt,.csv,.html的文件(基本上是文本格式的文件)会将我的jsp源代码附加到文件内容中. 以下是我用于下载的代码:

I am working on a File upload/download functionality, in Java using Struts2 framework, where we are uploading to and downloading from a remote server path. All seems to work fine when I check the functionality at my local machine with a local path as the destined path from where i am downloading and to which am uploading the files of any format. The development environment has JBoss server. But when I run the same over at the prod env, where the application is deployed in Weblogic server, files of .txt,.csv,.html (basically text format files) have my jsp source code appended to the file content. Below is the code that I have used for downloading:

BufferedOutputStream bout=null;
FileInputStream inStream = null;
byte[] buffer = null;

try {   
    inStream = new FileInputStream(path+File.separator+filename);       
    buffer = new byte[8192];
    String extension = "";
    int pos = filename.lastIndexOf(".");
    if (pos > 0) 
        extension = filename.substring(pos+1);

    int bytesRead = 0, bytesBuffered = 0;   

    response.setContentType("application/octet-stream");
    response.setHeader("content-disposition", "attachment; filename="+ filename);               
    bout = new BufferedOutputStream(response.getOutputStream());

    while((bytesRead = fistrm.read(buffer)) > -1){                  
        bout.write(buffer, 0, bytesRead);
        bytesBuffered += bytesRead;
        if(bytesBuffered > 1048576){
            bytesBuffered = 0;
            bout.flush();
        }

    }           
} catch (IOException e) {
    log.error(Logger.getStackTrace(e));         
} finally {             
    if(bout!=null){
        bout.flush();
        bout.close();
    }
    if(inStream!=null)
        inStream.close();               

}   

对于扩展名,我尝试使用不同的响应内容类型,但这没有帮助. 似乎outputstream甚至在从inputstream写入之前就在其中包含了jsp源代码.

I have tried using different response content types with respect to the extension, but it was of no help. Seems like the outputstream has the jsp source code in it even before writing from the inputstream.

任何人都可以提出解决方案并解释为什么会发生这种情况吗?

Can anyone please suggest a solution and explain why is this happening ?

推荐答案

之所以发生这种情况,是因为您直接在输出流中编写代码,然后返回一个struts结果,即您的JSP.您正在使用一个动作,就好像它是一个servlet,不是.

It is happening because you are writing directly in the outputstream, and then returning a struts result, that is your JSP. You are using an action as if it would be a servlet, which is not.

在Struts2中,要实现您的目标,您需要使用流结果 类型,如以下答案所述:

In Struts2, to achieve your goal, you need to use the Stream result type, as described in the following answers:

https://stackoverflow.com/a/16900840/1654265

否则,如果您想绕过框架机制并自己手动写入outputStream(在极少数情况下有用,请

Otherwise, if you want to bypass the framework mechanisms and manually write to the outputStream by yourself (there are very rare cases in which it is useful, like downloading dynamically created ZIP), then you must return the NONE result.

从Action类方法返回ActionSupport.NONE(或null)将导致结果处理被跳过.如果该操作完全处理了结果处理(例如直接写入HttpServletResponse OutputStream),这将很有用.

Returning ActionSupport.NONE (or null) from an Action class method causes the results processing to be skipped. This is useful if the action fully handles the result processing such as writing directly to the HttpServletResponse OutputStream.

但是我强烈建议您使用Stream结果,这是标准方法.

But I strongly suggest you to go with the Stream result, the standard way.

这篇关于下载文件时获取附带文件内容的j​​sp源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆