• java 移动文件并删除原文件


    需求是这样的,先上传文件,因为只传文件没有办法生成一级目录文件夹,传过来参数又不好取,所以就先上传,然后后面提交的时候移动文件到新的目录:

    public static String moveFile(String name, String path, String number) throws IOException {
            //移动文件夹
            File fromPath = new File(path + File.separator + name);
         //这个url不要new File,否则outStream的地方报错,拼出来的路径是:D:/resources/uploads/123/abc.png String url
    = path + File.separator + number + File.separator + name; FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(fromPath); File toPath = new File(path + File.separator + number); System.out.println("fileUrl: " + toPath); if (!toPath.exists()) { toPath.mkdirs(); } outStream = new FileOutputStream(url); in = inStream.getChannel(); out = outStream.getChannel(); IOUtils.copy(inStream, outStream); } catch (IOException e) { e.printStackTrace(); } finally { inStream.close(); in.close(); outStream.close(); out.close(); //删除原来文件,必须等流关闭后才能删除 fromPath.deleteOnExit(); }
           //这是存入数库的路径 String imagePath
    = "/uploads/" + number + "/"+ name; return imagePath; }
  • 相关阅读:
    [转].net批量导入数据
    IIS6 应用程序池配置详解附建议设置
    [转]SQLServer死锁问题
    VSS 版本管理
    非功能性需求介绍[转]
    javascript获取网页中HTML元素
    [转]如何做一流的研究
    多web并发测试问题解决
    jQuery 页面校验
    SqlServer排序规则错误致使不能查询
  • 原文地址:https://www.cnblogs.com/ly-gaoshuaige/p/13714315.html
Copyright © 2020-2023  润新知