`

同InputStream in, OutputStream out生成文件

 
阅读更多
<%

%><%@ page import="java.io.*"
%><%@ page import="java.net.*"
%><%@ page import="java.util.*"
%><%@ page import="mmmmllll.hk.aws2.quotation.util.QuotationWebKeys"
%><%@ page import="mmmmllll.hk.aws2.master.util.AwsResourceReader"
%><%@ page import="mmmmllll.common.framework.util.GenericResourceReader"
%><%@ page import="mmmmllll.common.framework.security.CipherObject"
%><%@ page import="mmmmllll.common.util.Logger"
%><%!
    public static void copyFile(InputStream in, OutputStream out) throws IOException {
        try {
            byte[] buf = new byte[4096];
            int i = 0;
            while ((i = in.read(buf)) != -1) {
                out.write(buf, 0, i);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                in.close();
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
%><%
    String quotationFileDownloadServerPath = 
            AwsResourceReader.in.getSafeString("QuotationPdfFileDownloadServer.path");

    String filePath = "";
    String[] parameters = (String[]) session.getAttribute(QuotationWebKeys.QUOTATION_PRINT_PARAMETERS);
        /*
         * parameters[0] - quotation owner key
         * parameters[1] - quotation number
         * parameters[2] - filename
         * parameters[3] - generateFlag  'Y' or 'N'
         * parameters[4] - optional param, generateFilePathFlag 'Y' or 'N';
         */
    String fileName = parameters[2];
    String quotationNr = parameters[1];
    String ownerKey = parameters[0];
    String generateFlag = parameters[3];
    String generateFilePathFlag = "N";
    if (parameters.length == 5) {
        generateFilePathFlag = parameters[4];
    }

    try{
        GenericResourceReader reader = AwsResourceReader.in;
        String username = reader.getSafeString("FILE_DOWNLOAD_USERNAME");
        String password = reader.getSafeString("FILE_DOWNLOAD_PASSWORD");
        String isProduction = reader.getSafeString("FILE_DOWNLOAD_IS_PRODUCTION");
        //filePath = reader.getSafeString("FILE_DOWNLOAD_QUOTATION_FOLDER_PATH");

        String urlPath = quotationFileDownloadServerPath + "?file_name=" + fileName;
        System.out.println("Url Path = " + urlPath);
        // *** if isProduction, get and decrypt username and password from properities file.
        if (isProduction != null && isProduction.equalsIgnoreCase("true")) {
            String loginFilePath = reader.getSafeString("FILE_DOWNLOAD_LOGIN_FILE_PATH");
            String keyFilePath = reader.getSafeString("FILE_DOWNLOAD_KEY_FILE_PATH");
            String algorithm = reader.getSafeString("FILE_DOWNLOAD_CRYPT_ALGORITHM");

            CipherObject c = CipherObject.getInstance(keyFilePath, algorithm);
            File file = new File(ora.ml.asup.util.WrappedFile.pathWrapper(loginFilePath));
            FileInputStream fis = new FileInputStream(file);
            byte[] loginInfo = new byte[fis.available()];
            fis.read(loginInfo);
            String loginInfoStr = new String(c.decrypt(loginInfo));
            int semiColonPos = loginInfoStr.indexOf(';');
            username = loginInfoStr.substring(0, semiColonPos);
            password = loginInfoStr.substring(semiColonPos + 1);
            fis.close();
        }

        urlPath += "&username=" + username;
        urlPath += "&password=" + password;
        urlPath += "&quotation_nr=" + quotationNr;
        urlPath += "&owner_key=" + ownerKey;
        urlPath += "&generate_flag=" + generateFlag;
        urlPath += "&generate_file_path_flag=" + generateFilePathFlag;
        System.out.println("Url Path = " + urlPath);
Logger.logStat(this, "start " + quotationNr + " " + new Date());
        URL url = new URL(urlPath);
        InputStream in = url.openStream();
        //response.setContentType("application/pdf");
        response.setContentType("application/postscript");
        //response.setHeader("Content-disposition", "attachment; filename="+fileName );
        ServletOutputStream ostream = response.getOutputStream();
        copyFile(in,ostream);
Logger.logStat(this, "end " + quotationNr + " " + new Date());
Logger.logStat("[ID=" + session.getId() + "] end...action=quotation_aw054_01_get_report");
        response.flushBuffer();
    } catch (Exception e){
Logger.logStat(this, "end " + quotationNr + " " + new Date() + " " + e);
        // *** if the username and password is invalid or any other exception throw in file server. ***
        response.setContentType("application/pdf");
        //response.setHeader("Content-disposition", "attachment; filename=Error.txt");
        out.print("The file server is temporary unavailable, please try again later.");
    }
%>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics