在Java 5中设置文件权限 [英] Set file permission in java 5

查看:112
本文介绍了在Java 5中设置文件权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码上传图片.问题是,上传图片后,我无法更改文件权限.我的文件权限默认设置为rw-r--r--(0644).默认情况下是否可以更改文件许可或将其设置为0777?它在我的本地系统中运行良好.但是无法更改我的linux服务器中的权限.

I am using the below code to upload image. The problem is that after uploading the image i cant change the file permission. my file permission set by default is rw-r--r-- (0644). Is it possible to change the file permission or set it as 0777 by default? It works fine in my local system. But not able to change the permission in my linux server.

    <%
    try

    {

        int filesize=0;
        String fieldname="",fieldvalue="",filename="",content="",bookid="",bkdescription="";        

        try {
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : items) {
                if (item.isFormField()) {
                    fieldname = item.getFieldName();
                    fieldvalue = item.getString();                 
                    if(fieldname.equals("homeid")){
                        bookid=fieldvalue;
                    }

                    if(fieldname.equals("bkdescription")){
                        bkdescription=fieldvalue;
                    }             

                } else {
                    try{
                    fieldname = item.getFieldName();
                    filename = FilenameUtils.getName(item.getName());
                    InputStream filecontent = item.getInputStream();
                    filesize=(int)item.getSize();
                    filename="literal_"+bookid+".jpg";
                    if(filesize>0){                     
                    byte[] b=new byte[filesize];                  
                    int c=0;                                   

                    File f=new File(getServletConfig().getServletContext().getRealPath("/")+"/imagesX");
    String filePah=getServletConfig().getServletContext().getRealPath("/")+"/imagesX";

                    if(f.isDirectory())
                    {
                        String fl[]=f.list();
                        for(int i=0;i<fl.length;i++)

                            {

              File fd=new File(getServletConfig().getServletContext().getRealPath("/")+"/imagesX/"+fl[i]);
                             if(fd.getName().equals(filename))
                             fd.delete();

                        }

                    }

                    if(!f.exists())
    {
            new File(filePah).mkdir();      
f.mkdir()
    }                

   java.io.FileOutputStream fout=new java.io.FileOutputStream(getServletConfig().getServletContext().getRealPath("/")+"/imagesX/"+filename);    

                    while((c = filecontent.read(b)) != -1 )
                    {
                        fout.write(b, 0, c);

                    }

                    fout.close();
                    filecontent.close();
                    }

                    }catch (Exception e) {
                System.out.println("Exception in creation of file      :"+e);

                    }

                }

            }

        } catch (FileUploadException e) {
            throw new ServletException("Cannot parse multipart request.", e);
        }

    }

    catch(Exception exp)

    {
        out.println(exp);
    }

    %>

推荐答案

您不能从Java代码内部更改文件权限.

You cannot change the file permission from inside java code.

对于新文件,系统的默认umask设置为0644.更改默认umask并不是一个好主意.

Your system's default umask is set to 0644 for new file. It wouldn't be good idea to change the default umask.

您需要做的是将目录的权限设置为0777,然后将目录的ACL重新定义为递归,以便在其中创建的所有新文件都将继承相同的权限.

What you need is to do is set the permission of your directory to 0777 and then redefine your directory's ACL to recursive, so all new file created inside will inherit the same permission.

此处提供了显示操作方法的链接- https://superuser.com/questions/151911/如何从父目录继承新文件权限

Heres a link which shows how to go about - https://superuser.com/questions/151911/how-to-make-new-file-permission-inherit-from-the-parent-directory

这篇关于在Java 5中设置文件权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆