1

I am using Apache Commons File Uploader API. It is working successfully when I use absolute path like this c:\\my uploads\\. That is file will be uploaded to that place.

I am using NetBeans 7 for development. In my Project Tree, I have created another folder called uploads. So when I use relative paths like uploads/ or /uploads/pics/, it is not working. That is, there is no error. But file is not written to that folder. I am using a object of "File" and using its "write()" function.

My project folder structure is like:

-- C:\
-- -- my project\
-- -- -- school\
-- -- -- -- web\
-- -- -- -- -- index.jsp
-- -- -- -- -- fileupload.jsp
-- -- -- -- -- uploads\
-- -- -- -- -- -- pics\
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vpp Man
  • 2,384
  • 8
  • 43
  • 74

1 Answers1

2

You basically need to convert the relative web path to absolute disk file system path by ServletContext#getRealPath().

String relativeWebPath = "/uploads";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
String filename = FilenameUtils.getName(item.getName());
File file = new File(absoluteDiskPath, filename);
// ...

However, this approach of writing uploaded files to expanded WAR folder is absolutely not recommended. When you redeploy the WAR, all those uploaded files will get lost (simply because they are not contained in the original WAR!). It will also not work when the server is configured to expand WAR in memory instead of on disk. The getRealPath() would then return null.

Write uploaded files to a path outside the expanded WAR folder. You did it right at first place. You can always make the upload folder configureable by specifying it as a VM argument or a properties file setting.

Last but not least, Java code belongs in Java classes, not JSP files.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanku. I am going to use "C:\\my uploads\\" as the location. But how to check if folder exists ? If not exists, create one. Object of `File` contains `mkdir()`. But it do not accept a path. So I am confused – Vpp Man Feb 14 '12 at 12:18
  • Just call it on `new File("C:/my uploads")`. – BalusC Feb 14 '12 at 12:20
  • thanku. I am using it like this. But i did not know that it works like that way. – Vpp Man Feb 14 '12 at 12:22
  • The `File` class is indeed a bad name as most developers incorrectly assume that it can only refer files, not folders. That class should be named `Path`. They have fixed it in Java 7. – BalusC Feb 14 '12 at 12:26
  • thanku. I am using `File` like this: `File f = new File("c:\uploads\" + fileName)`. So this will an error when folder does not exists. – Vpp Man Feb 14 '12 at 12:44
  • 1
    I think you're totally missing the point. Just create `File uploadFolder = new File("C:/my uploads")` and call `mkdir()` (or better, `mkdirs()`) on it. You can just do it once during servlet's initialization or something. Then you can use `File uploadedFile = new File(uploadFolder, filename)`. – BalusC Feb 14 '12 at 12:46
  • one more question, how to display this image ? I am storing filename with extension in database. So i tried like this: ``. This will not work. Because when i checked page html, it still displays correct path `c:\my uploads\test.jpg`. But when hovered it shows target location as `localhost:8080/my project/c:\myuploads\test.jpg`. This is also not working: `../../../uploads/test.jpg`. It appends `localhost:8080/myproject/` in front of it. – Vpp Man Feb 14 '12 at 13:49
  • The `` must refer an URL, not a local disk file system path. If you're using Tomcat, just add another `` to `server.xml`. See also http://stackoverflow.com/questions/1502841/reliable-data-serving – BalusC Feb 14 '12 at 13:59
  • thanku. I am using GlassFish Server(NetBeans 7.1). For MySQL, I am using WAMP Server. When i checked `sun-web.xml`, there is `/myproject`. – Vpp Man Feb 14 '12 at 16:39
  • Add this line to `sun-web.xml`: ``. See also http://stackoverflow.com/questions/8885201/primeface-update-after-upload/8889096#8889096 for a related answer. – BalusC Feb 14 '12 at 16:41
  • thanku. I have done it like this: `` in xml file. Then used it like this: ``. But this did not worked. When i checked that link (by hovering on it, in Google Chrome's Inspect Element), it showed: `http://localhost:8080/uploads/test.jpg` – Vpp Man Feb 14 '12 at 17:29
  • Remove `/uploads` from `dir`. The `dir` must refer the parent location. – BalusC Feb 14 '12 at 17:30
  • Code now: `` JSP: ``. Still image is not showing. My project is situated at `http://localhost:8080/myproject/index.jsp`. So is this problem caused because of not including that project name in XML config? – Vpp Man Feb 14 '12 at 17:54