Set file permission in java 5

久未见 提交于 2019-12-24 15:11:20

问题


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);
    }

    %>

回答1:


You cannot change the file permission from inside java code.

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

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.

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




回答2:


An alternative solution is to change the permissions externally with a system command, chmod.

Example:

public static void runCmd (String[] cmd) {

    try {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader r = new BufferedReader(
            new InputStreamReader (
                p.getInputStream()
            )
        );
    } catch(Exception e) {
    }
}

    runCmd(new String[] {
        "/bin/chmod",
        "755",
        "/path/to/your/script"
    });

P.S. were you also trying to call Java from a stored proc in an Oracle database?



来源:https://stackoverflow.com/questions/11095728/set-file-permission-in-java-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!