Third Party Download Managers issue with Struts2 File download link

余生颓废 提交于 2019-12-12 05:57:39

问题


I am getting java.lang.IllegalStateException: getOutputStream() has already been called for this response when user clicks on a download link in my struts2 web application. This exception occurs only when the client has third-party download managers installed in their machines like Internet Download Manager. Without IDM or any other third party download managers, our code runs fine without any exception.

Solutions we have tried are :

We have observed that IDM sends requests to the server in chunks for file download. So in order to avoid multi-part requests, we have created a filter and found that two simultaneous requests are generated, one with Accept-Encoding : gzip, deflate and second subsequent request coming with header Accept-Encoding : identity. This works fine for us but it throws exceptions intermittently, behavio@@@r is unpredictable. Below is our filter code :

import java.io.IOException;   
import java.util.Enumeration;   
import javax.servlet.Filter; 
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
public class DisallowingAcceptRangeFilter implements Filter{ 
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

        // TODO Auto-generated method stub
        HttpServletResponse response = (HttpServletResponse)res;
        HttpServletRequest request = (HttpServletRequest)req;
        // Request Headers
                Enumeration<String> headers = request.getHeaderNames();
                boolean rejectAcceptEncodingIdentity = false;
                while(headers.hasMoreElements()){
                    String key = headers.nextElement();
                    String value = request.getHeader(key);                                   
                    if(key.equalsIgnoreCase("accept-encoding") && value.equalsIgnoreCase("identity")){
                        rejectAcceptEncodingIdentity = true;

                    }

                }

                // response.setHeader("Accept-Ranges", "none");
                // System.out.println(response.getHeader("Accept-Ranges"));           
        if(rejectAcceptEncodingIdentity == false){
            chain.doFilter(req, res);

        }else{
            response.sendRedirect(request.getHeader("Referer"));

        }       

    }

    @Override

    public void init(FilterConfig filterConfig) throws ServletException {

        // TODO Auto-generated method stub

        System.out.println("DisallowingAcceptRangeFilter initialized !!!");  

    }

}

In my struts.xml file, i have mapped request action to this :

<action name="downloadFile" class="models.PDFDownload">          
            <param name="path">D:/DATA/PDFs/</param>
            <result name="downloadedFile" type="stream">
             <param name="contentDisposition">attachment;filename="${fileName}"</param>
                <param name="contentType">application/pdf</param>
                <param name="inputName">inputStream</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>

Pdf download action class code is :

package models;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class PDFDownload extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static final Logger LOGGER = Logger.getLogger(PDFDownload.class);
    private InputStream inputStream;
    private String fileName;
    private long contentLength;
    private String path; 

    public long getContentLength() {
        return contentLength;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public InputStream getInputStream() {
        return inputStream;
    }  

    public String execute() throws FileNotFoundException {
        LOGGER.info("File download execution in PDFDownload class execute method. Downloaded File Name is "+getFileName());
        System.out.println("FileName : " + fileName);
        System.out.println("Getter " + getFileName());
        File fileToDownload = new File(path+fileName);
        inputStream = new FileInputStream(fileToDownload);
        contentLength = fileToDownload.length();

        return "downloadedFile";
    } 
}

来源:https://stackoverflow.com/questions/32247434/third-party-download-managers-issue-with-struts2-file-download-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!