Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3423724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:23:22+00:00 2026-05-18T06:23:22+00:00

I’m doing a simple file upload in jsp. And i seem to have been

  • 0

I’m doing a simple file upload in jsp. And i seem to have been stopped by this simple path issue. I’m developing on windows but will propably deploy on a linux machine. so i have the tmpdir and the finaldir under mywebsite/tempfiledir and mywebsite/finalfiledir

so i use this code (snippet of the servlet)

public class SyncManagerServlet extends HttpServlet {
 private static final String TMP_DIR_PATH = "/tempfiledir";
 private File tempDir;
 private static final String DESTINATION = "/finalfiledir";
 private File destinationDir;

 public void init(ServletConfig config){
    try {
        super.init(config);

        tempDir = new File(getAbsolute(TMP_DIR_PATH));

        if(!tempDir.isDirectory()){
            throw new ServletException(TMP_DIR_PATH + " is not a Directory");
        }

        destinationDir = new File(getAbsolute(DESTINATION));
        if(!destinationDir.isDirectory()){ 
            throw new ServletException(DESTINATION + " is not a Directory");
        }

    } catch (ServletException ex) {
        Logger.getLogger(OrliteSyncManagerServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
}


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String email = request.getParameter("email");
    String path = request.getContextPath();
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();

    fileItemFactory.setRepository(tempDir);
    ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);


    try {


        List items = uploadHandler.parseRequest(request);
        Iterator itr = items.iterator();
        while(itr.hasNext()){
            FileItem item = (FileItem) itr.next();

            if( item.isFormField() && item != null ){

                out.println("<html>");
                out.println("<head>");
                out.println("<body>");
                out.println("your email: " + item.getString() + " has been submited and context path is "+ request.getContextPath() );
                out.println("</body>");
                out.println("</head>");
                out.println("</html>");

            } else {
                out.println("the uploaded file name is  : " + item.getName());
                out.println("content type  is  : " + item.getContentType());
                out.println("Size  is  : " + item.getSize());
                File file = new File(destinationDir, FilenameUtils.getName(item.getName()));

                item.write(file);
            }

public String getAbsolute(String relativepath){
    return getServletContext().getRealPath(relativepath);
}


//......
}

i’m having this exception

java.io.FileNotFoundException: D:\WORK\java\netbeansProject\Projects\mywebsite-webapp\target\mywebsite\finalfiledir (Access is denied)

i can’t figure out why the relative path is failing.
I’ve noticed in a lot of samples online people uses full path for the tempdir.
So in my case where i have to worry about linux on deployment, what’s the workaround?But first i’ld like to understand why the path i’ve given is wrong.

Thanks for reading this!
so 2 issues here

1 how to solve this path immediate issue?

2 how to do it in a more portable way (with linux privileges in mind)?

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:23:22+00:00Added an answer on May 18, 2026 at 6:23 am

    1 how to solve this path immediate issue?

    Paths which starts with a leading slash are not relative to the current working directory. They are absolute and would only in Windows point to the current working disk (where the server is running on and is dependent on how the server is been started).

    In your case, the "/tempfiledir" would in Windows point to C:\tempfiledir when Windows and the server is installed on C:\, and in Linux to /tempfiledir. You need to add another check in init() method which does a File#exists() check as well to spot the absence of the folder timely.

    2 how to do it in a more portable way (with linux privileges in mind)?

    As long as you don’t use Windows-specific disk labels like C:\ and use / as path separator, you don’t need to worry about this.

    If you really insist in writing the files to the webcontent, then you need to keep two things in mind: 1) When deploying as WAR, it only works when the WAR is expanded by servletcontainer. 2) When redeploying the WAR, everything (all uploaded files) will be erased.

    This is how you can convert relative web paths to absolute disk file system paths so that you can use it further in File and consorts:

    String relativeWebPath = "/tempfiledir";
    String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
    File file = new File(absoluteDiskPath, filename);
    

    Unrelated to the problem: the item.getName() would return the full client side path in certain webbrowsers (specifically: the MSIE family). As per the Commons Fileupload FAQ you need to call FilenameUtils#getName() on it before using as filename in new File(), otherwise it will cause havoc there as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a reasonable size flat file database of text documents mostly saved in
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.