title: Upload file Servlet on hardcoded server path
body: <p>I have a .jsp page that show a file browser and a upload button and a Servlet that should upload the file to server . The problem is that the file is uploaded to a temporary folder but I need it to be uploaded on a specific path on the server, but the path isn`t recognized.</p>
  
  <p>UploadServlet: the doPost method</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      try {
          // get access to file that is uploaded from client
          Part p1 = request.getPart("file");
          InputStream is = p1.getInputStream();
  
          String filename = getFilename(p1);
  
          // get temporary path - this works but it saves on a tmp folder...
          //String outputfile = this.getServletContext().getRealPath(filename);  // get path on the server
  
          //hardcoded path on which I have to save my file
          String outputfile = "http://localhost:8080/Tutorial2/Upload/" + filename;
  
          FileOutputStream os = new FileOutputStream (outputfile);
  
          // write bytes taken from uploaded file to target file
          int ch = is.read();
          while (ch != -1) {
               os.write(ch);
               ch = is.read();
          }
          os.close();
          out.println("&lt;h3&gt;File uploaded successfully!&lt;/h3&gt;");
      }
      catch(Exception ex) {
         out.println("Exception --&gt;" + ex.getMessage());
      }
      finally { 
          out.close();
      }
  }
  
  private static String getFilename(Part part) {
      for (String cd : part.getHeader("content-disposition").split(";")) {
          if (cd.trim().startsWith("filename")) {
              String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
              return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
          }
      }
      return null;
  }
  </code></pre>
  
  <p>The .jsp page</p>
  
  <p>
  </p>
  
  <pre><code>  &lt;form action="UploadServlet" method="post" enctype="multipart/form-data"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;Select File : &lt;/td&gt;
              &lt;td&gt;&lt;input  name="file" type="file"/&gt; &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;p/&gt;
      &lt;input type="submit" value="Upload File"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>I've seen many posts on this matter but neither helped me. When i run my servlet i get an exception at <code>FileOutputStream os = new FileOutputStream (outputfile);</code> - <code>Exception --&gt;http:\localhost:8080\Tutorial2\Upload\SvnBridge.exe (The filename, directory name, or volume label syntax is incorrect)</code>. Searched for this and didn't find anything relevant. I don't know if it helps but I use Servlet 3.0 and Apache Tomcat v7.</p>
  
  <p>Do you have any ideea why can`t I write my file to the path above?</p>
  
  <p>Thx,</p>