从服务器ActionForward / Struts / Java将文件下载到Downloads文件夹

时间:2019-07-18 15:17:58

标签: java jsp servlets web-applications struts

我正在尝试从Web应用程序的ActionForward Java类中下载文件。我看过许多示例来尝试不同的解决方案,但到目前为止,都没有奏效。我的知识有限,并且花了很多时间来使它起作用。

在我的jsp页面上,一个链接在我的struts配置中命中了一个动作,该动作将线程带到java类上的ActionForward返回类型方法。

然后我将传入的文件名从Amazon s3存储桶中获取。从s3存储桶下载文件后,我现在有了文件bytes []。

然后我需要像大多数文件一样将文件下载到本地计算机上(出现在downloads文件夹中,并在页面底部显示网络上的下载内容)

在遵循一些示例之后,我一直收到此错误

Servlet Exception - getOutputStream() has already been called for this 
response

我通过

克服了错误
response.getOutputStream().write

而不是像这样创建新的OutputStream

OutputStream out = response.getOutputStream();

现在它可以正常运行,但是没有文件下载。

这是我正在尝试执行的Java文件。

您可以在下面的文件中看到一个被注释掉的DownloadServlet类,我尝试了它作为另一种尝试。我之所以这样做,是因为许多示例都具有对HttpServlet进行扩展的类,我对DownloadServlet进行了扩展,但并没有区别。

package com.tc.fms.actions;

    import com.sun.media.jai.util.PropertyUtil;
    import com.tc.fw.User;
    import org.apache.commons.beanutils.PropertyUtils;

    import java.io.*;
    import java.io.File;
    import java.util.ArrayList;

    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.tc.fw.actions.BaseAction;
    import org.apache.struts.upload.FormFile;


    import io.isfs.utils.ObjectUtils;


    import com.tc.fw.*;
    import com.tc.fms.*;
    import com.tc.fms.service.*;


public class FileDownloadAction extends BaseAction {

private static ObjectUtils objectUtils = new ObjectUtils();
private final int ARBITARY_SIZE = 1048;

public ActionForward performWork(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    System.out.println("In File Download Action");
    ActionMessages errors = new ActionMessages();
    User user = (User)request.getSession().getAttribute(User.lookupKey);
    String fileName = (String) PropertyUtils.getSimpleProperty(form, "fileName");
    String outboundDir = (String) PropertyUtils.getSimpleProperty(form, "outboundDir");
    System.out.println("File Dir: " + outboundDir + " File Name: " + fileName);
    try{
        try {
// Get file from amazon
            byte[] fileBytes = objectUtils.getFileDavid(outboundDir, fileName);
            if (fileBytes != null) {
                java.io.File file = File.createTempFile(fileName.substring(0, fileName.lastIndexOf(".") - 1), fileName.substring(fileName.lastIndexOf(".")));
                FileOutputStream fileOuputStream = new FileOutputStream(file);
                fileOuputStream.write(fileBytes);
                try {
/*                        DownloadServlet downloadServlet = new DownloadServlet();
                    downloadServlet.doGet(request, response, file);*/

                    response.setContentType("text/plain");
                    response.setHeader("Content-disposition", "attachment; filename=" + file.getName());

                    InputStream in = new FileInputStream(file);
                    /*OutputStream out = response.getOutputStream();*/

                    byte[] buffer = new byte[ARBITARY_SIZE];

                    int numBytesRead;
                    while ((numBytesRead = in.read(buffer)) > 0) {
                        response.getOutputStream().write(buffer, 0, numBytesRead);
                    }
                } catch (Exception e) {
                    System.out.println("OutputStream EROOR: " + e);
                }
            } else {
                System.out.println("File Bytes Are Null");
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("fms.download.no.file.found"));
                saveErrors(request, errors);
                return mapping.findForward("failure");
                // Failed
            }
        } catch (Exception eee){
            System.out.println("Failed in AWS ERROR: " + eee);
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("fms.download.failed"));
            saveErrors(request, errors);
            return mapping.findForward("failure");
        }
    }catch (Exception ee){
        System.out.println("Failed in global try");
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("fms.download.failed"));
        saveErrors(request, errors);
        return mapping.findForward("failure");
    }
    return mapping.findForward("success");
}
}

0 个答案:

没有答案