简体   繁体   中英

Permission denied when upload file in java web application deployed on linux server

I have a java web application with spring 4 and jstl deployed on wildfly installed on linux server. In this app, I have to upload Excel files to extrat data and inject the hole in some table. To do this I defined this configuration for multipart :

public class myAppServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
... 

private static final String TEMP_FOLDER_NAME = "tmp"; // Temporary location where files will be stored

private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size.
// Beyond that size spring will throw exception.
private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part.

private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk

private MultipartConfigElement getMultipartConfigElement() {
        StringBuilder uploadTempDirectoryUrl = new StringBuilder();
        uploadTempDirectoryUrl.append(System.getenv(XeryaConstants.HOME_ENV_VARIABLE_NAME)).append(File.separator)
                .append(TEMP_FOLDER_NAME);
        File tempDirectory = new File(uploadTempDirectoryUrl.toString());
        if (!tempDirectory.exists()) {
            log.info("XeryaServletInitializer - Create temp directory " + uploadTempDirectoryUrl);
            tempDirectory.mkdir();
        }

        log.info("XeryaServletInitializer - Multiplart temporal directory: " + uploadTempDirectoryUrl);
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement(System.getenv(AppConstants.HOME_ENV_VARIABLE_NAME),
                MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);
        return multipartConfigElement;
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(getMultipartConfigElement());
    }
}

This is the code of the method generale the error (line beetween **)

    public static File convert(MultipartFile file) throws IOException
{
    File convFile = new File(file.getOriginalFilename());
    **convFile.createNewFile();**
    FileOutputStream fos = new FileOutputStream(convFile);
    fos.write(file.getBytes());
    fos.close();
    return convFile;
}

In the linux sever, the tmp folder which is created in configuration floder out off wildfly has this credentials:

root@VM-XITS01-DEV:/opt/xerya_home# ls -ld
drwxr-xr-x 9 wildfly wildfly 4096 juil. 25 02:21

The problem is when I upload the excel file from windows OS, I have this error:

    2018-07-25 03:06:25,192 ERROR [stderr] (default task-47) java.io.IOException: Permission denied
2018-07-25 03:06:25,192 ERROR [stderr] (default task-47)        at java.io.UnixFileSystem.createFileExclusively(Native Method)
2018-07-25 03:06:25,193 ERROR [stderr] (default task-47)        at java.io.File.createNewFile(File.java:1012)
2018-07-25 03:06:25,193 ERROR [stderr] (default task-47)        at com.xerya.school.util.XeryaUtils.convert(XeryaUtils.java:103)
2018-07-25 03:06:25,193 ERROR [stderr] (default task-47)        at com.xerya.school.web.validator.rest.importdata.RestImportDataStudentController.uploadFile(RestImportDataStudentController.java:75)

Can some one help me plz to resolve this problem. In Windows all work right.

I thanks every body for thiers responses. Finally, I located the bug. It comes from this line:

File convFile = new File(file.getOriginalFilename());

To resolve it, I change it by:

String fileName = System.getenv(XeryaConstants.HOME_ENV_VARIABLE_NAME)
            .concat(File.separator)
            .concat(TEMP_FOLDER_NAME).concat(File.separator)
            .concat(file.getOriginalFilename());
    File convFile = new File(fileName);

When I look to getOriginalFilename documentation, it says: "Return the original filename in the client's filesystem. This may contain path information depending on the browser used, but it typically will not with any other than Opera.

I think that this explains the behavior that I get: When the application were deployed on windows, and I upload the file since chrome, no error was found. But when the application were deployed on linux and I upload the file from chrome the bug was found. I was fixed as I said later by concat file.getOriginalFilename() with the absolute path of the container folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM