`
cy729215495
  • 浏览: 127239 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

fileupload源码分析

阅读更多

  private static String getUniqueId()
  {
    int current;
    synchronized (DefaultFileItem.class)
    {
      current = counter++;
    }
    String id = Integer.toString(current);

    if (current < 100000000)
    {
      id = ("00000000" + id).substring(id.length());
    }
    return id;
  }
  private static String getUniqueId()
  {
    int current;
    synchronized (DefaultFileItem.class)
    {
      current = counter++;
    }
    String id = Integer.toString(current);

    if (current < 100000000)
    {
      id = ("00000000" + id).substring(id.length());
    }
    return id;
  }


1.不要用""+整数来拼接字符串。调用Integer.toString高效
2.公有数据加同步保护

protected File getTempFile()
  {
    File tempDir = this.repository;
    if (tempDir == null)
    {
      tempDir = new File(System.getProperty("java.io.tmpdir"));
    }

    String fileName = "upload_" + getUniqueId() + ".tmp";

    File f = new File(tempDir, fileName);
    f.deleteOnExit();
    return f;
  }
注意删除f.deleteOnExit在jvm退出的时候删除文件

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics