美文网首页
2020-01-20

2020-01-20

作者: MR崔先生 | 来源:发表于2020-01-20 16:52 被阅读0次

    properties读取文件

    package portal.portalblo.utils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import java.io.*;
    import java.util.Properties;
    
    public class PropertyUtil {
        private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
        private static Properties props;
        static{
            loadProps();
        }
        synchronized static private void loadProps(){
            /*logger.info("开始加载properties文件内容.......");*/
            props = new Properties();
            InputStream in = null;
            try {
                in = PropertyUtil.class.getClassLoader().getResourceAsStream("portal/portalblo/utils/EAPCasUrl.properties");
                props.load(in);     
                
            } catch (FileNotFoundException e) {
                logger.error("EAPCasUrl.properties");
            } catch (IOException e) {
                logger.error("出现IOException");
            } finally {
                try {
                    if(null != in) {
                        in.close();
                    }
                } catch (IOException e) {
                    logger.error("EAPCasUrl.properties文件流关闭出现异常");
                }
            }
            /*logger.info("加载properties文件内容完成...........");*/
            logger.info("properties文件内容:" + props);
        }
    
        public static String getProperty(String key){
            if(null == props) {
                loadProps();
            }
            return props.getProperty(key);
        }
        public static String getProperty(String key, String defaultValue) {
            if(null == props) {
                loadProps();
            }
            return props.getProperty(key, defaultValue);
        }
    }
    

    properties文档

    driverClass=dm.jdbc.driver.DmDriver
    jdbcUrl=jdbc:dm://localhost:5236/ACAP
    user=ACAP
    password=HLWZWFWPT133
    

    批量下载

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URLDecoder;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.Response.ResponseBuilder;
    
    public static Response  batchDownload(List<TFileList> fList,HttpServletRequest request){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");  ;
            String dates = sdf.format(new Date());
            String zipFrontFile = "附件包-"+dates+".zip";
            String zipFile = request.getSession().getServletContext().getRealPath("/")+"/uploadFile/netPlus/zip/"+zipFrontFile;
            try{
                if(!new File(request.getSession().getServletContext().getRealPath("/")+"/uploadFile/netPlus/zip").exists())
                {
                    new File(request.getSession().getServletContext().getRealPath("/")+"/uploadFile/netPlus/zip").mkdirs();
                } 
                byte[] buf = new byte[2048];
                // 创建值班单zip文件
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
                // 循环压缩文件
                for(TFileList tList:fList){
                    File file = new File(request.getSession().getServletContext().getRealPath("/")+URLDecoder.decode(tList.getFilepath(), "UTF-8"));
                    FileInputStream in = new FileInputStream(file);
                    // 添加压缩包文件
                    out.putNextEntry(new ZipEntry(file.getName()));
                    int len = 0;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    // 关闭入口
                    out.closeEntry();
                    // 关闭文件流
                    in.close();
                }
                // 关闭文件流
                out.close();
                //下载zip
                File file = new File(zipFile);
                byte[] buffer = null;
                try{
                    FileInputStream fis = new FileInputStream(file);
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] b = new byte[1024];
                    int n;
                    while ((n = fis.read(b)) != -1){
                        bos.write(b, 0, n);
                    }
                    fis.close();
                    bos.close();
                    buffer = bos.toByteArray();
                }
                catch (FileNotFoundException e){
                    e.printStackTrace();
                }
                catch (IOException e){
                    e.printStackTrace();
                }
                ResponseBuilder builder = Response.ok(buffer, "application/octet-stream");
                builder.header("Content-Disposition", "attachment;filename="+new String(file.getName().getBytes("UTF-8"), "ISO8859-1"));
                Response response = builder.build();
                return response;
                
            } catch (Exception e) {
                // logger.error("异常", e);
            } finally {
                //删zip
                File file=new File(zipFile);
                if (!file.isDirectory()) {
                    file.delete();
                }
            }   
            return null;
        }
    

    下载

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.Response.ResponseBuilder;
    public static Response download(HttpServletRequest request,String fillPath,String fillName) throws UnsupportedEncodingException{
            String file_path = URLDecoder.decode(fillPath, "UTF-8");
            String att_name = ""; 
            if(isNotNull(fillName)){
                att_name = URLDecoder.decode(fillName, "UTF-8");
            }else{
                att_name = file_path.substring(file_path.lastIndexOf("/")+1,file_path.length());
            } 
            //从服务端读出
            file_path = request.getSession().getServletContext().getRealPath("/") + file_path;
            File file = new File(file_path);
            //ByteArrayOutputStream os = new ByteArrayOutputStream();
            byte[] buffer = null;
            try{
                FileInputStream fis = new FileInputStream(file);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] b = new byte[1024];
                int n;
                while ((n = fis.read(b)) != -1){
                    bos.write(b, 0, n);
                }
                fis.close();
                bos.close();
                buffer = bos.toByteArray();
            }
            catch (FileNotFoundException e){
                e.printStackTrace();
            }
            catch (IOException e){
                e.printStackTrace();
            }
            ResponseBuilder builder = Response.ok(buffer, "application/octet-stream");
            builder.header("Content-Disposition", "attachment;filename="+new String(att_name.getBytes("UTF-8"), "ISO8859-1"));
            Response response = builder.build();
            return response;
        }
    

    相关文章

      网友评论

          本文标题:2020-01-20

          本文链接:https://www.haomeiwen.com/subject/htwjzctx.html