Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4165387
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: Java

2019-03-13 17:57:39

因为ubuntu下的nginx使用www-data用户,所以要将tomcat的用户,加入到www-data用户组。

sudo usermod -aG www-data hongrui
sudo chown -R hongrui:www-data /opt/report
sudo chmod -R 0750 /opt/report

配置后nginx可以下载
-rw-r--r-- 1 hongrui hongrui 13 Mar 13 16:18 perm.log
这样的文件
但是无法下载
-rw-r----- 1 hongrui hongrui 13 Mar 13 17:47 perm.log
一下代码在使用java的Main函数可以调用

点击(此处)折叠或打开

  1. package test;
  2. import static java.nio.file.StandardOpenOption.*;
  3. import java.nio.*;
  4. import java.nio.channels.*;
  5. import java.nio.file.*;
  6. import java.nio.file.attribute.*;
  7. import java.io.*;
  8. import java.util.*;

  9. public class LogFilePermissionsTest {

  10.     public static void main(String[] args) {

  11.         // Create the set of options for appending to the file.
  12.         Set<OpenOption> options = new HashSet<OpenOption>();
  13.         options.add(APPEND);
  14.         options.add(CREATE);

  15.         // Create the custom permissions attribute.
  16.         Set<PosixFilePermission> perms =
  17.                 PosixFilePermissions.fromString("rw-r--r--");
  18.         FileAttribute<Set<PosixFilePermission>> attr =
  19.                 PosixFilePermissions.asFileAttribute(perms);

  20.         // Convert the string to a ByteBuffer.
  21.         String s = "Hello World! ";
  22.         byte data[] = s.getBytes();
  23.         ByteBuffer bb = ByteBuffer.wrap(data);

  24.         Path file = Paths.get("/opt/report/perm.log");

  25.         try (SeekableByteChannel sbc =
  26.                      Files.newByteChannel(file, options, attr)) {
  27.             sbc.write(bb);
  28.         } catch (IOException x) {
  29.             System.out.println("Exception thrown: " + x);
  30.         }
  31.     }
  32. }

但是在tomcat中出现出现异常,
经确认发现其上传目录下代码自动创建的目录权限是750,所上传文件权限是640。也就是说默认赋予的文件权限中其他用户的权限始终为0所导致。umask 002 对应文件权限664,文件夹权限775;umask 022对应文件权限644,文件夹权限755。可见都有读取访问权限的。在默认情况下,tomcat所建目录及文件应该用到的是umask 022.
在tomcat 8.5的catalina.sh中,UMASK=”0027”出现了!在按照目前的功能需求,将其改为UMASK=”0022”并重启tomcat 8.5后,文件访问恢复正常。
阅读(68198) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~