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.
答案 0 :(得分:0)
我感谢大家的回应。 最后,我找到了错误。它来自以下行:
File convFile = new File(file.getOriginalFilename());
要解决此问题,请通过以下方式更改它:
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);
当我查看getOriginalFilename文档时,它说:“返回客户端文件系统中的原始文件名。根据所使用的浏览器,其中可能包含路径信息,但Opera以外的任何其他浏览器都不会。
我认为这解释了我得到的行为: 当应用程序部署在Windows上并且自chrome以来我上传文件时,未发现任何错误。但是,当应用程序部署在linux上并且我从chrome上传文件时,发现了错误。正如我稍后所说的那样,我用concat file.getOriginalFilename()和容器文件夹的绝对路径修复了这个问题。