• 文件上传和下载


    import org.apache.struts2.ServletActionContext;

    1、上传文件

    static boolean isWindows = false;
    private String uploadDir;
    private File uploadify;           //上传文件file对象
    private String uploadifyFileName; //上传文件名
    private String load;
    
    /** return true---是Windows操作系统 */
    public static boolean isWindowsOS() {
        boolean isWindowsOS = false;
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            isWindowsOS = true;
        }
        return isWindowsOS;
    }
    //上传文件
    public String upload(){  
        String newFileName=null;  
        //得到当前时间开始流逝的毫秒数,将这个毫秒数作为上传文件新的文件名  
        long now=new Date().getTime();  
        //得到保存上传文件的真实路径  
        isWindows = isWindowsOS(); // 判断是否是windows系统
        String path="";
        if (isWindows) {// windows
            uploadDir = "/upload/contrast";
            path=ServletActionContext.getServletContext().getRealPath(uploadDir);
        } else {// linux
            uploadDir = "/data/upload/contrast";
            path = uploadDir;
        }
        
        File dir=new File(path);  
        if (!dir.exists()) {    //如果这个目录不存在,则创建它 
            dir.mkdirs(); 
        }  
        int index=uploadifyFileName.lastIndexOf(".");  
        //判断上传文件是否有扩展名,以时间戳作为新的文件名  
        if (index!=-1) {  
            newFileName=now+uploadifyFileName.substring(index); 
            load=uploadDir+"/"+newFileName;
        }else {  
            newFileName=Long.toString(now);  
        }  
        
        BufferedOutputStream bos=null;  
        BufferedInputStream bis=null;  
              
        //读取保存在临时目录下的上传文件,写入到新的文件中  
        try {  
            //File f = new File("C:/java/hello");
            //InputStream out = new FileInputStream(f);   //该流用于从文件读取数据
            
            FileInputStream fis=new FileInputStream(uploadify);  
            bis=new BufferedInputStream(fis);  
              
            FileOutputStream fos=new FileOutputStream(new File(dir,newFileName));  
            bos=new BufferedOutputStream(fos);  
              
            byte [] buf=new byte[4096];  
            int len=-1;  
            while ((len=bis.read(buf))!=-1) {  
                bos.write(buf,0,len);  
            }  
        } catch (FileNotFoundException e) {  
             e.printStackTrace();  
        } catch (IOException e) {  
             e.printStackTrace();  
        }finally{  
            if (null!=bis) {  
                try {  
                    bis.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            if (null!=bos) {  
                try {  
                    bos.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
        return null;  
    }  

    2、下载文件

    //附件下载 
    public String download() {
        try {
            String path="";
            String realPath  = "/upload/contrast/test.java"; 
            realPath = realPath.replace('\', '/');
            isWindows = isWindowsOS(); // 判断是否是windows系统
            if (isWindows) {// windows
                path=ServletActionContext.getServletContext().getRealPath(realPath);
            } else {// linux
                path=realPath;
            }
            String load = path;
             
            File file = new File(load);
            InputStream inputStream = new BufferedInputStream(new FileInputStream(load));
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            Utils.getResponse().reset();
            String fileName = daoHelper.queryForObject("product.gms.bom.bomCheck.findfileVisteName", this).toString();
            Utils.getResponse().setCharacterEncoding("UTF-8");
            Utils.getResponse().setContentType("application/txt");
            Utils.getResponse().addHeader("Content-Disposition", "attachment;filename="" + URLEncoder.encode(fileName, "UTF-8") + """);
            Utils.getResponse().addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(Utils.getResponse().getOutputStream());
            outputStream.write(buffer);
            inputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  • 相关阅读:
    Microsoft SQL Server 2008 基本安装说明
    微软的招聘哲学——做微软人的五大核心素质
    大型网站性能优化的通用方法
    模型驱动的开发,回忆一年多前的一次开发
    远离客户陷阱小故事 转
    单例模式(Singleton)
    真的很高兴,就在今天“博客园团队”为我们开通了 “CSLA 团队”
    桥接模式(Bridge)与合成/聚合复用原则(CARP)
    2009年11月11日
    虚拟化的好处 随笔
  • 原文地址:https://www.cnblogs.com/leilei-y/p/12483432.html
Copyright © 2020-2023  润新知