How to set the path to the "context path" for uploaded files using Apache Common fileupload?

I use a shared Apache file library with Netbeans 6.8 + Glassfish. I am trying to change the current boot path to be in the current servlet context path, something like this: WEB-INF / upload

so I wrote:

File uploadedFile = new File("WEB-INF/upload/"+fileName);
session.setAttribute("path",uploadedFile.getAbsolutePath());
item.write(uploadedFile);

but I noticed that the library saves the downloaded files to the Glassfish folder , this is what I get when I print the absolute path of the downloaded file:

C:\Program Files\sges-v3\glassfish\domains\domain1\WEB-INF\upload\xx.rar 

My question is:

  • How to make the general file upload save the downloaded file along the path relative to the current servlet path, so I don’t need to specify the whole path? is it possible?
+3
1

java.io.File , . , "" , -. , , . .

ServletContext#getRealPath() - .

String relativeWebPath = "/WEB-INF/uploads";
String absoluteFilePath = getServletContext().getRealPath(relativeWebPath);
File uploadedFile = new File(absoluteFilePath, FilenameUtils.getName(item.getName()));
// ...

, , , , . webapp. . / ?

+7

Source: https://habr.com/ru/post/1746949/


All Articles