fileDownload.jsp 2.1 KB
<%@ include file="/system/upgradetoe9/MigrationOperation.jsp"%>
<%@ page import="weaver.security.file.FileType" %>
<%@ page language="java" import="java.io.*,java.net.*,weaver.general.*"%>
<%@ page import="java.util.ArrayList" %><%

    String en_name = Util.null2String(request.getParameter("en_name"));
    String cn_name = Util.null2String(request.getParameter("cn_name"));
    String infofile  = GCONST.getRootPath() + "system"+File.separatorChar+"upgradetoe9"+ File.separatorChar+"resource"+File.separatorChar + en_name;
    ArrayList<String> types  = new ArrayList<String>();
    types.add(".zip");
    types.add(".doc");
    types.add(".docx");
    types.add(".xls");
    types.add(".xlsx");
    types.add(".txt");
    if(!FileType.validateFileExt(infofile,types)) {
        out.print("{\"status\":\"no\"}");
        return;
    }
    try {
        //获取文件路径
        String filePath = "";
        filePath = infofile;
        //设置向浏览器端传送的文件格式
        response.setContentType("application/msword");

        cn_name = URLEncoder.encode(cn_name, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="+ cn_name);
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            fis = new FileInputStream(filePath);
            byte[] b = new byte[1024 * 10];
            int i = 0;
            while ((i = fis.read(b)) > 0) {
                os.write(b, 0, i);
            }
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

%>