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 8662119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:39:29+00:00 2026-06-12T16:39:29+00:00

These are days that I’m banging my head on this problem and maybe you

  • 0

These are days that I’m banging my head on this problem and maybe you that certainly know more than me you can help me ….
Then I try to explain better.

I have a javascript file that through the library d3.js builds the html code pages and replaces it with the other code each part a different function … The page will not charge (Ajax).
At some point I need to allow the user to upload an image to their profile picture so I make sure that the html code bait

<input type="file" id="file">

and a

<input type = "button" onclick = "javaScript: performAjaxSubmit ()">

PerformAjaxSubmit function () sends the data to a Java Servlet via a xmlHttpRequest level 2, which, from what I understand, can send not only strings but also more complex things such as files.

The function is as follows:

function performAjaxSubmit() {
    var sampleFile = document.getElementById("file").files[0];
    var formdata = new FormData();
    formdata.append("sampleFile", sampleFile);
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "http://127.0.0.1:8080/Prova/Upload", true);
    xhr.send(formdata);
    xhr.onload = function(e) {
        if (this.status == 200) {
            alert(this.responseText);
        }
    };
}

The code in the Servlet instead is this:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // Check that we have a file upload request
    System.out.println(request.getAttribute("username"));
      isMultipart = ServletFileUpload.isMultipartContent(request);
      response.setContentType("text/html");
      java.io.PrintWriter out = response.getWriter( );

      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(new File("C:/Users/Marty/workspaceJEE/Prova/WebContent/imm/utenti"));
      filePath="C:/Users/Marty/workspaceJEE/Prova/WebContent/imm/utenti";
      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );

      try{ 
      // Parse the request to get file items.
      List fileItems = upload.parseRequest(request);

      // Process the uploaded file items
      Iterator i = fileItems.iterator();

      while ( i.hasNext () ) 
      {
         FileItem fi = (FileItem)i.next();
         if ( !fi.isFormField () )  
         {
            // Get the uploaded file parameters
            String fieldName = fi.getFieldName();
            String fileName = fi.getName();
            String contentType = fi.getContentType();
            boolean isInMemory = fi.isInMemory();
            long sizeInBytes = fi.getSize();
            // Write the file
            if( fileName.lastIndexOf("\\") >= 0 ){
               file = new File( filePath +"/"+ 
               fileName.substring( fileName.lastIndexOf("\\"))) ;
            }else{
                System.out.println(filePath + 
               fileName.substring(fileName.lastIndexOf("\\")+1));
               file = new File( filePath +"/"+  
               fileName.substring(fileName.lastIndexOf("\\")+1)) ;
            }
            fi.write( file ) ;
         }
      }

   }catch(Exception ex) {
       System.out.println(ex);
   }
}

Now (sorry if the question is a bit long) it works but the problem is that the images are saved in the path that I have provided me with the command:

factory.setRepository(new File("C:/Users/Marty/workspaceJEE/Prova/WebContent/imm/utenti"));

How do I then save it remotely? That is, once I load the site of such Altrevista, how do I make sure that they are not pià saved to C but in a folder in your project?

I hope I explained. I’m using Apache Tomcat v7.0.
Thanks in advance!

  • 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-06-12T16:39:30+00:00Added an answer on June 12, 2026 at 4:39 pm

    You can use ServletContext.getRealPath

    This code returns <context root>/upload (depends on your deployment configuration)

    request.getSession().getServletContext().getRealPath("/upload")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've been scratching my head these last few days on that problem so i
Apple is always in the news these days with i this and i that.
I am just learning python and Qt these days. So please consider that this
I've noticed that Google Maps is taking longer than usual to load these days,
I was under the impression that these days javascript can be used as a
I know that computers these days are rather fast and low efficiency tweaks, such
These days I come across several Google search results that contain sites with links
The general consensus these days seems to be that you do not store binary
I'm studying javascript these days and I have question. I have variable that contain
These days, we encountered a strange problem, some of our solr apps on tomcat

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.