title: File upload in case of publicly hosted Apache Tomcat issue
body: <p>I am uploading a file to Amazon S3 using Java Servlet. I have a simple JSP web form that has a textbox and a submit button. I am entering the filepath in the textbox and when I click submit button it calls the servlet. This servlet has a java code to upload the file to Amazon S3.</p>
  
  <p>Case 1: Apache Tomcat on localhost (Installed using Eclipse IDE)
  This works smoothly. If I give the filepath /home/Videos/sample.avi. The file gets uploaded to Amazon S3.</p>
  
  <p>Case 2:Apache Tomcat hosted on Amazon EC2 instance that has a public IP. 
  After hosting the project publicly, when I hit the url from any client computer, it displays the textbox and the submit button. I enter a filepath for the file present on the client computer and click submit. Now the Apache Tomcat container searches for the file on the machine where it is hosted and hence it fails becuase no such file is present there. I verified this issue by manually keeping the file on the same path on hosted server where the file is present on the localpath of my local computer..it works in then.</p>
  
  <p>So how do we handle file upload scenarios in publicly hosted servers?</p>
  
  <pre><code>protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException{
      PrintWriter out = response.getWriter();
      String filepath = request.getParameter("filepath");
      String metaData = request.getParameter("metaData");
      uploadFileName = filepath;
  
  
  
      if ((!filepath.endsWith(".mp4")) &amp;&amp; (!filepath.endsWith(".avi"))
              &amp;&amp; (!filepath.endsWith(".avi"))
              &amp;&amp; (!filepath.endsWith(".mpeg"))) {
          out.println("alert('Not a valid file format')");
      } else {
  
          String amazonURLToRecording = "http://d33tbzutgyvlht.cloudfront.net/";
          keyName= bucketName + Math.random();
  
          try {
              System.out
                      .println("Uploading a new object to S3 from a file\n");
              File file = new File(uploadFileName);
  
  
              if(!s3Client.doesBucketExist(bucketName)){
                  CreateBucketRequest cbr=new CreateBucketRequest(bucketName);
                  s3Client.createBucket(cbr);
  
              }
              Thread.sleep(30000);
              //waitForBucketToBeCreated(bucketName);
              PutObjectRequest putObject = new PutObjectRequest(bucketName, keyName, file);
              putObject.setCannedAcl(CannedAccessControlList.PublicRead);
              ObjectMetadata meta = new ObjectMetadata();
              meta.setContentType("application/octet-stream"); // binary // data
  
              putObject.setMetadata(meta);
              s3Client.putObject(putObject);
  
              System.out.println("Video was successfully uploaded.");
  
  
          } catch (AmazonServiceException ase) {
              System.out
                      .println("Caught an AmazonServiceException, which "
                              + "means your request made it "
                              + "to Amazon S3, but was rejected with an error response"
                              + " for some reason.");
              System.out.println("Error Message:    " + ase.getMessage());
              System.out.println("HTTP Status Code: " + ase.getStatusCode());
              System.out.println("AWS Error Code:   " + ase.getErrorCode());
              System.out.println("Error Type:       " + ase.getErrorType());
              System.out.println("Request ID:       " + ase.getRequestId());
          } catch (AmazonClientException ace) {
              System.out.println("Caught an AmazonClientException, which "
                      + "means the client encountered "
                      + "an internal error while trying to "
                      + "communicate with S3, "
                      + "such as not being able to access the network.");
              System.out.println("Error Message: " + ace.getMessage());
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>