[英]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. 我有一个Spring 4和jstl部署在linux服务器上的wildfly上的Java Web应用程序。 In this app, I have to upload Excel files to extrat data and inject the hole in some table.
在此应用中,我必须上传Excel文件以提取数据并在某些表格中插入孔。 To do this I defined this configuration for multipart :
为此,我为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: 在linux服务器中,在配置文件中由wildfly创建的tmp文件夹具有以下凭据:
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: 问题是当我从Windows OS上传excel文件时,出现此错误:
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.
在Windows中,一切正常。
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. 当我查看getOriginalFilename文档时,它说:“返回客户端文件系统中的原始文件名。它可能包含路径信息,具体取决于所使用的浏览器,但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. 我认为这可以解释我得到的行为:当应用程序部署在Windows上并且从chrome开始上传文件时,未发现任何错误。 But when the application were deployed on linux and I upload the file from chrome the bug was found.
但是,当应用程序部署在linux上并且我从chrome上传文件时,发现了错误。 I was fixed as I said later by concat file.getOriginalFilename() with the absolute path of the container folder.
我后来通过concat file.getOriginalFilename()和容器文件夹的绝对路径进行了修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.