简体   繁体   中英

getServletContext().getRealPath(“/”) omitting forward slash (/)

I'm trying to upload a file from an HTML file input.

I am using Apache Commons FileUpload and the file uploads successfully. However, when I try storing the file path in my MySQL, it is storing it without file path code:

String uploadFolder = getServletContext().getRealPath("/");
String fileName = new File(item.getName()).getName();
filePath = uploadFolder+"/"+fileName;
File uploadedFile = new File(filePath);

This is how I'm trying to store the file.

sample filepath stored

C:UsersLashDesktopworkspace3.metadata.pluginsorg.eclipse.wst.server.core    mp0wtpwebappsJavaECom/download doget.txt

I have no idea what this question is about, but the correct way to do the operations your posted code is doing is as follows:

File uploadFolder = new File(getServletContext().getRealPath("/"));
String fileName = new File(item.getName()).getName(); // not sure what's going on here
File uploadedFile = new File(uploadFolder, fileName);

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